Lets say I have an int variable n = 8. On most machines this will be a 32 bit value. How can I only get the lower 8 bits (lowest byte) of this in binary? Also how can I access each bit to find out what it is?
unsigned n = 8; unsigned low8bits = n & 0xFF; Note a few things:
unsigned types& operator0xFF because in binary it has its low 8 bits turned on and the rest 0To access a certain bit of a number, say the kth bit:
unsigned n = ...; unsigned kthbit = (1 << k) & n; Now, kthbit will be 0 if the kth bit of n is 0, and some positive number (2**k) if the kth bit of n is 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With