c - 从中​​字节得到位 [英] C - Get a bit from a byte

查看:84
本文介绍了c - 从中​​字节得到位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2249731/how-to-get-bit-by-bit-data-from-a-integer-value-in-c\">how从在C整数度日位数据位?

我有一个8位字节,我想从这个字节变得有点像
    了getByte(0b01001100,3)= 1

I have a 8-bit byte and I want to get a bit from this byte, like getByte(0b01001100, 3) = 1

推荐答案

Firstoff, 0B preFIX不是C,但C的GCC扩展
为了得到位的值3 uint8_t有一个,你可以使用这个前pression:

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)

如果第3位被设置,这将进行评估,以1

0,如果第3位没有设置。

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

这篇关于c - 从中​​字节得到位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆