Solved

Endianness Concern with Increment

Posted on 2007-11-25
4
226 Views
Last Modified: 2010-04-15
I often use 64-bit integers to store aggregate values to be stored on disk.  

Suppose I use the last 8-bits of a 64-bit integer to store some value from 0 to 255.  Then suppose I want to increment that value, without affecting the rest of the 64 bit integer.

This could be done in 2 ways.  I could use bitwise operations like this:

val = (val & (0xFFUL << 56)) + (1UL << 56) | (val &  0xFFFFFFFFFFFFFFUL);

Or I could simply address the last byte directly and increment it:

++(*(  ((unsigned char*) &val) + 7));

The latter method is probably more efficient, but is it portable and safe?  As far as I can see, this should be fine on both big and little endian computers, and also should not cause any alignment issues because I am addressing a single byte.  Still, I'm not sure.  Am I correct?  Is the latter method portable/safe?
0
Comment
Question by:chsalvia
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 20347395
Remember, you have byte Endianness as well as bit Endianness!
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20347397
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20347404
>>  Or I could simply address the last byte directly and increment it:
>>  ++(*(  ((unsigned char*) &val) + 7));
Casting an integer as an array pointer is not portable considering the big/little endian organization, it will return the LSB or the MSB depending on platform.
Better you use the first method.

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 20347414
indeed you can increment by simply do this:
val += 0x0100000000000000UL;
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question