Solved

Endianness Concern with Increment

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to connect an iphone application to mysql database 10 205
posix semaphore deadlock 13 123
Why does module make constant click noise ? 9 130
Handling string inputs in C/Linux 23 184
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

809 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