Nukodi
Nukodi

Reputation: 357

Bitwise masking

Assuming an environment where long int is a 64-bit type, suppose I have an long int = 0x0123456789ABCDEF and I want to get the byte that represents 89. Would this line work?

n = (n >> (b << 3)) & 0xFF;

where n is the long int and b is the byte I want. So b would be 3 and shifting it left 3 would multiply it by 8 changing it into a byte so shifting should look like this 0x0123456789. Is using & 0xFF the right way to mask to get the last byte?

Upvotes: 1

Views: 183

Answers (1)

nwellnhof
nwellnhof

Reputation: 33658

Yes, this is the correct approach. This online example on ideone.com prints 89 as expected.

Upvotes: 1

Related Questions