Solved

Endianness Concern with Increment

Posted on 2007-11-25
4
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

717 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