What does the operator >>= mean?

4 answers 4

Operator >>= refers to a group of compound assignment operators. The behavior of an expression with a composite assignment operator E1 op= E2 equivalent to the expression E1 = E1 op E2 . With the only exception that E1 calculated only 1 time.

Operator >> is a bitwise right shift operator. May be applicable only to integer arguments (or enum ). And if E1 is an unsigned type or a signed type, but with a positive value, then the result of the expression E1 >>= E2 is the integer part of dividing E1 by 2 to the power of E2 . Those. each shift to 1 bit to the right is interpreted as dividing a number by 2.

In this case, if the type E1 signed, and the value is negative, the result of such a shift is implementation dependent. Although, most likely, it will also be interpreted as arithmetic (that is, the sign bit will remain in place). However, it is possible to rely on this only with proper mentioning in the documentation for the compiler.

    >>= division by 2

     a = a >> 3; 

    the same in short form

     a >>= 3; 

    division by 2 to degree 3, that is, by 8.

    • A right shift is not always a division by a power of two. Just as a shift to the left is not the same as multiplying by 2 ^ n. - freim

    This is a bitwise shift to the right. If we shift a number, it is divided by two to a power.

      << = bitwise left shift
      >> = bitwise right shift
      Detailed description here