• Use the unsinged type as operands of shift operator in C language


    K&R中如是说:

    Right shifting a signed quantity will fill with bit signs (``arithmetic shift'') on some machines and with 0-bits (``logical shift'') on others.

    也就说对于负数的右移位,首位补的1 or 0是实现相关的。

    我们再来看C99标准:

    Image(2)

    简而言之,

    对于<<, 如果左操作数是signed type, 负数是undefined behavior, 正数如果移位溢出后也是undefined behavior

    对于>>, 如果左操作数是singed type, 负数的话其值是implementation-defined.

    对于C++2003标准

    Image(3)

    简而言之,

    对于<<, 标准似乎没有定义对于有符号数的情况。

    对于>>,标准定义了负数的话,是一个implementation-defined

  • 相关阅读:
    ssh 远程命令
    POJ 2287
    POJ 2376
    hihoCoder1488
    POJ1854
    HDU 5510
    HDU 4352
    CodeForces 55D
    HDU 1517
    CodeForces 1200F
  • 原文地址:https://www.cnblogs.com/aoaoblogs/p/1918354.html
Copyright © 2020-2023  润新知