Solved

Endianness Concern with Increment

Posted on 2007-11-25
4
216 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now