Shift Operators in C++

前端 未结 6 1991
傲寒
傲寒 2020-12-09 12:17

If the value after the shift operator is greater than the number of bits in the left-hand operand, the result is undefined. If the left-hand operand is

6条回答
  •  借酒劲吻你
    2020-12-09 13:02

    If the value after the shift operator is greater than the number of bits in the left-hand operand, the result is undefined.

    It means (unsigned int)x >> 33 can do anything[1].

    If the left-hand operand is unsigned, the right shift is a logical shift so the upper bits will be filled with zeros.

    It means 0xFFFFFFFFu >> 4 must be 0x0FFFFFFFu

    If the left-hand operand is signed, the right shift may or may not be a logical shift (that is, the behavior is undefined).

    It means 0xFFFFFFFF >> 4 can be 0xFFFFFFFF (arithmetic shift) or 0x0FFFFFFF (logical shift) or anything-allowed-by-physical-law, i.e. the result is undefined.

    [1]: on 32-bit machine with a 32-bit int.

提交回复
热议问题