为什么偏向负数?
如果在我的编译器上,int是16位,那么它的范围是-32768到32767(在2的补码机器中)。
我想知道为什么负数多了1个数字。即正数为 32767 但负面的又是一个 ie-32768。
-32768 如何在 2 的补码 m/c 上表示?
If on my compiler, int is of 16 bits, then it's range is -32768 to 32767(in a 2's complement machine).
I want to know why negative numbers have 1 extra no. i.e positive numbers go to 32767
but negative go to one more i.e.-32768.
How -32768 is represented on a 2's complement m/c?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 16 位上,您可以使用 pow(2,16)(2 的十六次方)不同的组合来表示 65536 个数字。我们决定,零看起来最好原生表示为 000...000,而“补码”系统中的正数通常是可读的(它们等于所谓的“自然二进制”表示形式,如 0000 0000 0000 0101 = 5 十进制等) 。
二进制补码中的负数以 1111 1111 1111 1111 开头,表示 -1。将其想象为一个带有数字 997、998、999 的计数器表盘,突然当它必须代表 1000 时,它会溢出并显示 000。这里的原理是相同的,但方向是相反的 - 从 ...000到...111。 -2 表示为 1111....1110 等等。
二进制补码中可能的最低数字的前面为 1,其余数字为 0。
On 16 bits you can fit pow(2,16) (2 to the power of sixteenth) different combinations to represent 65536 numbers. It was decided that zero looks best represented natively as 000...000 and positive numbers in "two's complement" system are normally readable (they're equal to so called "natural binary" representation like 0000 0000 0000 0101 = 5 decimal etc).
Negative numbers in two's complement start with 1111 1111 1111 1111 to represent -1. Think about it as a counter dial with numbers that goes 997, 998, 999 and suddenly when it has to represent 1000 it overflows and shows 000. The principle is the same here, but the direction is other way around - from ...000 to ...111. -2 is represented as 1111....1110 and so on.
Lowest possible number in two's complement will have 1 on front and zeroes on the rest of digits.
如果您正在寻找一个简单、务实的答案:
没有任何偏见。正数和负数的数量相等,正数从 0 开始,负数从 -1 开始,因此相差 1。 :)
If you're looking for a simple, down to earth answer:
There isn't any bias. There are equal amount of numbers on positive and negative side, positive numbers just start from 0 and negative from -1, so hence the difference of one. :)
确实不存在任何“偏见”。当最高有效位被设置时,该数字为负数。对于“正数空间”(即,未设置 MSB)以及 1-32767,您的值为零,因此明显缺少 32768。
-32768 将由 0b1000000000000000 表示。请参阅链接文本
There isn't really any 'biasing'. The number is negative when the most significant bit is set. For "positive number space" (i.e, MSB is not set), as well as 1-32767, you have zero, hence the apparent lack of 32768.
-32768 would be represented by 0b1000000000000000. See link text
没有负零。 (-0)。这就是为什么它看起来是一种偏见。实际上,如果设置了最后一位,则将其视为负数。一个字节中还有另外 7 位可以在正负范围内设置。
There is no negative zero. (-0). Thats why it appears to be a bias. Really it is considered negative if the last bit is set. There is still another 7bits in a byte that can be set in both the positive and negative range.
另一种思考方式是创建一个 1 位有符号变量。有符号表示必须有负数,当然也会有正数。所以你可以有 1 个正数 1 个负数,你画出哪两个数字?现在让我们把它变成 2 位,你画出哪 2 个正值和负值?真正的秘密是最后一位代表负数。
如果我们想将 0 视为负数,则需要额外的工作。负 0 会造成浪费。
Another way to think of it is making a 1 bit signed variable. Signed means there must be negative numbers and of course there will be positive numbers. So you can have 1 positive one negative, what two numbers do you pic? now lets make it 2 bits, what 2 positive and negative do you pic? really the secret is the last bit represents negative.
Theres extra work if we want to consider 0 a negative number. and it would be wasteful to have a negative 0.