C言語<<、>>、&、|実用
他の人がプロジェクトを参照するか、サードパーティのライブラリのソースコードを参照しながら、あなたは、多くの場合、子供の靴のいくつかの新規参入のため、このようなA >> 4&0x0Fに、このような文言が表示されることが開発者として、それはかなり何この意味を理解しないかもしれません。ここでは、これらの演算子の実用化を説明する必要があります。
まず、意味のオペレータ
演算子 | 意味 | 説明 |
---|---|---|
<< | 左 | すべてのNビット左シフト、高い廃棄、低0のビット数のそれぞれについて。 |
>> | 右 | 数の各Nビットのバイナリビット右、低の右端に符号なしの数、ハイ0ため、破棄されます。 |
& | ビットAND | 対応する二つのビットが1である場合、ビットの結果は1であり、そうでなければ0。 |
リットル | ビットごとのOR | 2つのそれぞれのビット限り1が存在するように、結果は、他の1ビット、及び0です。 |
第二に、使用
の使用によるビット
1.クリア
メモリセルが消去されるため、全体のビットが0である場合であってもあれば、メモリセルの電流値は、両方(すなわち、元の番号が0,0である1になる)ようにネゲート&演算子を実行するには、明確な目標を達成することができます。
例えば23 =、B =〜 :
<span class="token property" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">a<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">:00010111
<span class="token property" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">b<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">:11101000
a&<span class="token property" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">b<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">:00000000
<span aria-hidden="true" class="line-numbers-rows" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">
ビットの特定の指定された数を取る2。
例えば、4 1 0は、ビットbの数になった後、= 23、Iはバック4桁のバイナリを利用したいと思い、あなたは残りの部分を見つけることができる、すなわちB = 0x0Fの(16進、バイナリを00001111に変換されます) 、&bが最後の4を取得します。
<span class="token property" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">a<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">:00010111
<span class="token property" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">b<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">:00001111
a&<span class="token property" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">b<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">:00000111
<span aria-hidden="true" class="line-numbers-rows" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">
3.指定されたビットを残します
例えば、= 23(8ビットで表される)、私は、そのバイナリビット4,6(左端のビットが1である)、休止位置0を維持したいです。あなたが4見つけることができ、及び図6は、0 B残りのビットの数とビット単位のAND演算であります
<span class="token property" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">a<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">:00010111
<span class="token property" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">b<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">:00010100
a&<span class="token property" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">b<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">:00010100
<span aria-hidden="true" class="line-numbers-rows" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">
位置またはを使用することにより
一般的に図1は、位置の特定の数の他のビットは変化しない、使用しました。例えば、A = 23、Iは、その第二及び第三の位置1,2したい、3ビット1を残りの部分に場所を見つけることができ、0またはビット単位操作の数です。
a:00010111
b:01100000
a|b:01110111
<span aria-hidden="true" class="line-numbers-rows" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">
第三に、プロジェクトの例
在实际项目中,很多时候&和|都是和>>、<<配合使用的。比如在蓝牙项目开发中,2个设备之间进行日期数据传输时,我们先定一个协议,用4个byte来表示一个日期,其中byte0表示年份的高位数,byte1表示年份的低位数,byte2表示月份,byte3表示日期。
设备端现在收到另外一台设备传过来的日期数据00010100 00010011 00000110 00011101(为了阅读方便我将每个byte之间用空格分开)。那么我要如何解析这个数据来得到实际日期呢?
<span class="token comment" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">/*
*第一步,获取日期。
*日期是最后一个byte,也就是最后8位,用一个后8位是1其余位是0的数(11111111,十六进制表示是0xff)与数据进行按位与计算即可
*/
date <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">= <span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00010100 <span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00010011 <span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00000110 <span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00011101<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">;
day <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">= date <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">& <span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">0xff<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">;<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">(计算结果是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00011101,十进制表示是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">29,也就是日期是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">29<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">)。
<span class="token comment" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">/*
*第二步,获取月份。
*月份是倒数第2个byte,此时需要先将最后一个byte砍掉(也就是右移8位),然后再和0xff进行按位与运算
*下面代码可以简写成date=date>>8&0xff;
*/
date <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">= date<span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">>><span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">8<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">; <span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">(计算结果是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00010100 <span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00010011 <span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00000110<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">)
month <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">= date <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">& <span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">0xff<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">;<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">(计算结果是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00000110,十进制表示是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">6,也就是月份是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">6月<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">)。
<span class="token comment" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">/*
*第三步,获取年份低位。
*先将最后一个byte砍掉(也就是右移8位),然后再和0xff进行按位与运算
*下面代码可以简写成date=date>>8&0xff;
*/
date <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">= date<span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">>><span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">8<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">; <span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">(计算结果是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00010100 <span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00010011<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">)
year_low <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">= date <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">& <span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">0xff<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">;<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">(计算结果是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00010011,十进制表示是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">19<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">)。
<span class="token comment" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">/*
*第四步,获取年份高位。
*先将最后一个byte砍掉(也就是右移8位),然后再和0xff进行按位与运算
*下面代码可以简写成date=date>>8&0xff;
*/
date <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">= date<span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">>><span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">8<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">; <span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">(计算结果是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00010100<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">)
year_heigh <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">= date <span class="token operator" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">& <span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">0xff<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">;<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">(计算结果是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">00010011,十进制表示是<span class="token number" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">20<span class="token punctuation" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">)。
<span aria-hidden="true" class="line-numbers-rows" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">
最后将4个数拼起来就得到日期为2019年6月29日。