简体   繁体   English

为什么偏向负数?

[英]Why biasing towards negative numbers?

If on my compiler, int is of 16 bits, then it's range is -32768 to 32767(in a 2's complement machine). 如果在我的编译器中,int为16位,则范围为-32768至32767(在2的补码机中)。
I want to know why negative numbers have 1 extra no. 我想知道为什么负数有1个额外的数字。 ie positive numbers go to 32767 but negative go to one more ie-32768. 即,正数为32767,但负数为一个,即32768。

How -32768 is represented on a 2's complement m/c? -32768如何在2的补码m / c上表示?

On 16 bits you can fit pow(2,16) (2 to the power of sixteenth) different combinations to represent 65536 numbers. 在16位上,您可以拟合pow(2,16)(2等于16的幂)个不同的组合,以表示65536个数字。 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). 决定零看起来最好用本机表示为000 ... 000,并且“二进制补码”系统中的正数通常是可读的(它们等于所谓的“自然二进制”表示,例如0000 0000 0000 0101 = 5个小数,等等) 。

Negative numbers in two's complement start with 1111 1111 1111 1111 to represent -1. 二进制补码中的负数以1111 1111 1111 1111开头,表示-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. 可以将其想象为带有997、998、999的计数器转盘,突然当它必须代表1000时,它会溢出并显示000。原理在这里是相同的,但是方向却相反-从... 000至... 111。 -2 is represented as 1111....1110 and so on. -2表示为1111 .... 1110,依此类推。

Lowest possible number in two's complement will have 1 on front and zeroes on the rest of digits. 二进制补码中可能的最低数字将在前面带有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. 正负数相等,正数从0开始,负数从-1开始,因此差为1。 :) :)

There isn't really any 'biasing'. 确实没有任何“偏见”。 The number is negative when the most significant bit is set. 当最高有效位被设置时,该数字为负。 For "positive number space" (ie, MSB is not set), as well as 1-32767, you have zero, hence the apparent lack of 32768. 对于“正数空间”(即设置MSB)以及1-32767,您为零,因此显然缺少32768。

-32768 would be represented by 0b1000000000000000. -32768将由0b1000000000000000表示。 See link text 查看链接文字

There is no negative zero. 没有负零。 (-0). (-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. 字节中还有另外7位可以在正负范围内设置。

Another way to think of it is making a 1 bit signed variable. 另一种思考的方式是制作一个1位有符号变量。 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? 因此,您可以有1个正数和1个负数,您要记录两个数字? now lets make it 2 bits, what 2 positive and negative do you pic? 现在让它变成2位,您拍出2个正数和负数? really the secret is the last bit represents negative. 真正的秘密是最后一位代表负面。

Theres extra work if we want to consider 0 a negative number. 如果我们想将0视为负数,则需要做更多的工作。 and it would be wasteful to have a negative 0. 而负数为0会很浪费。

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

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