I´m new in the C language, and I´m trying to get the 4 least significant bits from an unsigned char. So far I have no idea how to get them. I´ll appreciate any help
Asked
Active
Viewed 733 times
2
-
2Well, `c & 0xF`. But what does this have to do with *arrays* – Antti Haapala -- Слава Україні Mar 23 '19 at 18:53
-
https://stackoverflow.com/questions/295279/what-is-the-fastest-way-to-get-the-4-least-significant-bits-in-a-byte-c – Antti Haapala -- Слава Україні Mar 23 '19 at 18:56
1 Answers
5
It's simple bitwise logic:
unsigned char c = 120;
unsigned char lsb4 = c & 0x0F;
Where 0x0F
represents the binary value 00001111
.
If you're using GCC, it's even more literal:
unsigned char lsb4 = c & 0b00001111;
Technically the leading 0
s are not required here but they're included to help illustrate which bits are being selected.

tadman
- 208,517
- 23
- 234
- 262