C - Get a bit from a byte [duplicate]

落爺英雄遲暮 提交于 2019-12-03 08:56:31

Firstoff, 0b prefix is not C but a GCC extension of C. To get the value of the bit 3 of an uint8_t a, you can use this expression:

((a >> 3)  & 0x01)

which would be evaluated to 1 if bit 3 is set and 0 if bit 3 is not set.

First of all C 0b01... doesn't have binary constants, try using hexadecimal ones. Second:

uint8_t byte;
printf("%d\n", byte & (1 << 2);

Use the & operator to mask to the bit you want and then shift it using >> as you like.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!