are bit manipulation guaranteed to work endian independent

S

senthilrag

Provided a unsigned integer say (32bits b31-b0 bits) and its required
to get (b19-b10) .

One approach is have a mask and right shift it

#define BIT_MASK 0x000FFC00

uint32 variable;

variable &= BIT_MASK
variable >>= 10

My question is, does the above code guarantee to do the same in big
and little endian machines? Or should i make sure to check the
endianess and manipulate in accord?
What is the general approach to guarantee bit manipulations on a
software works fine irrespective to endianess of h/w underneath?
 
V

Vladimir S. Oka

Provided a unsigned integer say (32bits b31-b0 bits) and its required
to get (b19-b10) .

One approach is have a mask and right shift it

#define BIT_MASK 0x000FFC00

uint32 variable;

variable &= BIT_MASK
variable >>= 10

My question is, does the above code guarantee to do the same in big
and little endian machines? Or should i make sure to check the
endianess and manipulate in accord?
What is the general approach to guarantee bit manipulations on a
software works fine irrespective to endianess of h/w underneath?

The above will work irrespective of endinanness. Shifts and bit
manipulations are guaranteed to work as you'd expect.

The endianness only comes into play when it comes to storage (e.g.
reading/writing files).
 
R

Rod Pemberton

Provided a unsigned integer say (32bits b31-b0 bits) and its required
to get (b19-b10) .

One approach is have a mask and right shift it

#define BIT_MASK 0x000FFC00

uint32 variable;

variable &= BIT_MASK
variable >>= 10

My question is, does the above code guarantee to do the same in big
and little endian machines?

Yes and no. It depends on where the value of 'variable' came from.

If the value of variable originated in your program, yes.
variable=0xFFAA55EE;
It's endianess _within_ your program transparent to you. Both little and
big endian will function correctly and identically, although the byte order
is different.

If the value of variable originated in elsewhere, say a jpeg, a zip, or
network data, no.
The values will be swapped if the transfer of data was between a big and
little endian machine.
The values will be the same if the transfer of data was from a big to a big,
or from a little to a little endian machine (with a couple of rare
exceptions).
Or should i make sure to check the
endianess and manipulate in accord?

Are the values in variable being transfered between different type machines?
(jpeg, zip, tcp/ip)
If so, you'll need to correct for endianess.
What is the general approach to guarantee bit manipulations on a
software works fine irrespective to endianess of h/w underneath?

Usually, a number of macros are or a union is defined in order to switch
around the byte order of the data.


Rod Pemberton
 
S

stathis gotsis

Thanks for your replies.
So files are the major concern since its a stored data in memory and
needs to be ensured about endianess.
I know network data are network byte ordered(i think little endian)
so i remember using network to host order conversion function to patch
up.

network byte order is "big-endian".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top