How do you convert little Endian to big Endian with bitwise operations?

1k Views Asked by At

I get that you'd want to do something like take the first four bits put them on a stack (reading from left to right) then do you just put them in a register and shift them x times to put them at the right part of the number?

Something like

1000 0000 | 0000 0000 | 0000 0000 | 0000 1011

Stack: bottom - 1101 - top shift it 28 times to the left

Then do something similar with the last four bits but shift to the right and store in a register.

Then you and that with an empty return value of 0

Is there an easier way?

2

There are 2 best solutions below

0
On

You could do this way.. For example I/p : 0010 1000 and i want output 1000 0010

input store into a variable x int x; i = x>>4 j = x<<4 k = i | j print(K) //it will have 1000 0010.

0
On

Yes there is. Check out the _byteswap functions/intrinsics, and/or the bswap instruction.