简体   繁体   English

这是什么意思?

[英]What is the meaning of this?

hi guys can you explain what does that greater than sign do in the follwing code 嗨,大家好,您能否解释以下代码中的大于符号的作用是什么?

 if (header->mode > forceMode)
      {
        *rate >>= (header->mode - forceMode); //What does this mean >>=
         *granule_frame_size <<= (header->mode - forceMode); //What does this mean <<=
      }

While all the answers above are correct, it's possible you might still not understand it. 尽管以上所有答案都是正确的,但您可能仍然不了解。 You will need to understand binary. 您将需要了解二进制。

Imagine you have the expression 想象你有表达

7 << 2 7 << 2

That means "Convert 7 into binary, then shift all the bits left 2 times." 这意味着“将7转换为二进制,然后将所有位左移2次”。

So 7 is 00000111 所以7是00000111

shifted left twice (and inserting zeros) yeilds: 向左移动两次(并插入零),结果:

00011100 00011100

This is equal to 28. 这等于28。

So if the variable A is 7, and B is 2, then: 因此,如果变量A为7,而B为2,则:

A <<= B;

Leaves A equal to 28; 离开A等于28;

>>= is the right-shift assignment operator. >>=是右移赋值运算符。 x >>= y is equivalent to x = x >> y (with the caveat that @bdonlan points out below). x >>= y等效于x = x >> y (@bdonlan指出以下警告)。

The << and >> operators are traditionally thought of bitwise because that's how they're implemented, but as far as the C language is concerned, they are arithmetic operators (not bitwise operators) equivalent to multiplication and division by the corresponding power of 2. That is, x<<y is x*2^y and x>>y is x/2^y (with integer truncation), where by ^ I mean "raised to the power" and not "xor". 传统上认为<<>>运算符是按位的,因为这是它们的实现方式,但是就C语言而言,它们是算术运算符(不是按位运算符),相当于用2的相应乘方除以乘积也就是说, x<<y是x * 2 ^ y, x>>y是x / 2 ^ y(具有整数截断),其中^表示“提高到幂”而不是“ xor”。 They are only defined for positive values of x , and like all arithmetic operators, << has undefined behavior for signed operands if the result overflows. 它们仅针对x正值进行定义,并且像所有算术运算符一样,如果结果溢出, <<对于带符号的操作数具有未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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