?
Solved

Endianness Concern with Increment

Posted on 2007-11-25
4
Medium Priority
?
230 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 1000 total points
ID: 20347414
indeed you can increment by simply do this:
val += 0x0100000000000000UL;
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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 opening and reading files in the C programming language.
Suggested Courses

762 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