简体   繁体   English

对负数进行逐位运算

[英]Bitwise operations on negative numbers

I was reading a book on C, where in some section it says: " The bitwise operations are typically used with unsigned types.". 我正在读一本关于C的书,在某些章节中它说:“按位操作通常与无符号类型一起使用。”

Question: why? 问题:为什么?

Simply because it is not immediately clear what bit operations on the sign bit of a signed number should mean. 仅仅因为没有立即清楚签名号的符号位上的位操作应该是什么意思。

  • Unsigned types have no special bits, everything works straight forward. 无符号类型没有特殊位,一切都很直接。
  • Signed types have a special sign bit and can interpret that with three different encodings to represent negative values (ones and two's complement or sign and magnitude). 有符号的类型有一个特殊的符号位,可以用三种不同的编码来解释它,以表示负值(一和二的补码或符号和幅度)。

From the programming languages' point of view the unsigned does not mean, that it cannot be negative. 从编程语言的角度来看,无符号并不意味着,它不能是否定的。 It means, that the first bit of the number is not used for determining if the value is negative or positive. 这意味着,该数字的第一位不用于确定该值是负还是正。

So, for an 8 bit value the bitwise operators consider all the 8 bits, thus working on the range 0..255 (while the mathematical operators can consider the first bit as being the signedness indicator, thus working on a range of -128 to +127). 因此,对于8位值,按位运算符考虑所有8位,因此工作在0..255范围内(而数学运算符可以将第一位视为符号指示符,因此工作在-128到-128的范围内+127)。

Unsigned operands do not have any special bits for sign representation. 无符号操作数没有用于符号表示的任何特殊位。

For signed operands, the special bit for sign representation may interrupt operations which we want to perform. 对于带符号的操作数,符号表示的特殊位可能会中断我们想要执行的操作。

Bitwise operations are typically used with unsigned types or signed types with non-negative values. 按位运算通常用于无符号类型或带有非负值的带符号类型。

The C Standard supports 3 different representations for negative values: applying bitwise operators on such values has at best an implementation defined behavior. C标准支持负值的3种不同表示:对这些值应用按位运算符最多只能实现实现定义的行为。

For example -1 is represented: 例如-1代表:

  • as |1000|0000|0000|0001| as |1000|0000|0000|0001| on a 16-bit system with sign magnitude. 在具有符号幅度的16位系统上。
  • as |1111|1111|1111|1110| as |1111|1111|1111|1110| on a 16-bit system with one's complement. 在具有一个补码的16位系统上。
  • and more commonly as |1111|1111|1111|1111| 更常见的是|1111|1111|1111|1111| on a 16-bit system with two's complement. 在具有二进制补码的16位系统上。

As a consequence, -1 & 3 can have 3 different values: 因此, -1 & 3可以有3个不同的值:

  • 1 on sign-magnitude systems, 1上符号-幅度系统,
  • 2 on one's complement systems, 2在一个补体系统上,
  • 3 on two's complement systems, 3对二进制补码系统,

Other operations may behave in even more unexpected ways on negative numbers, such as << and >> , especially on the DS9K . 其他操作可能会以更加意想不到的方式表现在负数上,例如<<>> ,尤其是在DS9K上

Conversely, the bitwise operations are fully defined on unsigned types, which are mandated to have binary representation (with some restrictions on the right operand of shift operators). 相反,按位运算完全定义在无符号类型上,这些类型被强制要求具有二进制表示(对移位运算符的右操作数有一些限制)。

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

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