什么X>>;=N确实如此

本文关键字:gt 什么 | 更新日期: 2023-09-27 18:29:13

我有这个代码:

tmp = (uint)len;
writer.CurPosition = value + 1;
do
{
    value++;
    writer.dest.WriteByte((byte)((tmp & 0x7F) | 0x80));
} while ((tmp >>= 7) != 0);

但是我不明白tmp >>= 7是怎么工作的?

什么X>>;=N确实如此

>>被称为右位移位运算符。由于在>>之后还有一个额外的=(形成复合赋值运算符>>=),因此赋值分配器变量(tmp)将被共享

或者换句话说,使用给定的例子,

tmp >>= 7; //actually you use tmp both to assign and to be assigned

相当于

tmp = tmp >> 7; //actually you use tmp both to assign and to be assigned

现在关于逐位移位操作,我认为最好用一个例子来说明它。

假设tmp的值是0xFF00(二进制表示中的1111 1111 0000 0000),那么如果我们在逐位级别中看到,>>=的操作将看起来像这个

1111 1111 0000 0000 //old tmp
------------------- >> 7
0000 0001 1111 1110 //Shifted by 7 -> this is going to be the new value for tmp

因此,tmp的新值将是0x01FE(即0000 0001 1111 1110

>>是位移算子

CCD_ 15将CCD_ 16向右移位7位并将其设置为该值。

循环将继续,直到tmp

这实际上是C和C++的一部分,称为复合赋值运算符。

tmp >>= 7

相当于

tmp = tmp >> 7

CCD_ 18作为逐位右移