質問

I was reading about little endian-big endian and came across these concept

   long long number 

(((number & 0x00000000000000ff) << 56) + 
 ((number & 0x000000000000ff00) << 40) + 
 ((number & 0x0000000000ff0000) << 24) +
 ((number & 0x00000000ff000000) << 8) + 
 ((number & 0x000000ff00000000) >> 8) + 
 ((number & 0x0000ff0000000000) >> 24) +
 ((number & 0x00ff000000000000) >> 40) +
 ((number & 0xff00000000000000) >> 56 ))

what basically it does ?

When i compile it using( gcc -lrt -lm program_name) .i get a warning saying

   integer constant is too large for ‘long’ type
役に立ちましたか?

解決

That code performs a toggle between little endian and big endian for an unsigned 64 bit data type.

Look at the first sub-expression:

(number & 0x00000000000000ff) << 56

This masks out bits 0-7, and shifts them to the left by 56. So they now occupy bits 56-63.

And the next sub-expression:

(number & 0x000000000000ff00) << 40

This masks out bits 8-15, and shifts them to the left by 40. And so on, and so on.

If was writing the code I'd use bitwise or, |, rather than arithmetic addition, +, since that reads better in my view.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top