简体   繁体   中英

C - How to get the 4 least significant bits in a Char

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM