Lefteris
Lefteris

Reputation: 3256

Bit manipulation of integers depending on endianess

My question concerns bit manipulation when the endianess changes. In particular I have some code that reads individual bits of a uint32_t value and performs bit manipulation on them. The purpose was UTF-8 encoding. It works perfectly for my little endian machine.

Revisiting the code recently it dawned on me that I was not considering endianess of the machine as far as the uint32_t value's bit representation is concerned. So I have some questions regarding that respect.

Let's assume an example code that just requires bits 7-10 of an uint32_t saved in a different byte.

uint32_t v;
v = 18341;
char c = (v &(uint32_t) 0x3C0)>>6;

For little endian the number 18341 is represented as 0x47A5 or in binary:

0100 0111 1010 0101

and the above code should give us 1110 stored in the char

Now the question is how would we achieve this in a Big Endian machine? The same number would be represented quite differently 0xA5470000 or in binary:

1010 0101 0100 0111 0000 0000 0000 0000

with the bits we seek being in totally different positions and not even consequent.

Instead of using 0x3C0 at the other side of & we would have to use something else since the byte order is different. And especially since we need consequent bits of a byte we would require multiple boolean & operations like below right?

char c = ((v&(uint32_t)0xc0)>>6) | ((v&(uint32_t)0x300)>>6)

Summing up. Is my understanding correct that in the cases where we need to get sequential bits of an integer value represented in binary we would need to perform different manipulations for the two endianess cases?

Finally is there a better way to achieve the same thing than the one I showed above? Maybe I am missing something totally obvious.

Upvotes: 3

Views: 407

Answers (1)

perreal
perreal

Reputation: 97948

No. If you are using values (like 0x300) and language operators (<<, |, &) it does not matter because the value will be represented according to the machine. So in your case you do not need to worry about this problem. You should worry, for example, when you are copying bytes from file into the memory.

If you are dealing with the memory representation directly, you can convert the representation before manipulation:

#if defined (BENDIAN)
   val = makelittle(val);
#endif
   manip_lendian(val);
#if defined (BENDIAN)
   val = makebig(val);
#endif

Also see this answer

Upvotes: 1

Related Questions