用5位二进制补码表示有符号数的最高和最低整数是多少? [英] Whats the highest and the lowest integer for representing signed numbers in two's complement in 5 bits?

查看:447
本文介绍了用5位二进制补码表示有符号数的最高和最低整数是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解二进制的工作原理,可以计算二进制到十进制,但是我迷失在带符号的数字周围.我已经找到了进行转换的计算器.但是我不确定如何找到最大和最小数字,或者如果没有给出二进制数字,怎么转换,而StackO中的问题似乎是关于转换特定数字还是不包括带符号数字到特定位.>

具体问题是:

 我们只有5位代表二进制补码中的带符号数字:什么是最高符号整数?写下其十进制值(仅在负数时包括符号).最低有符号整数是多少?写下其十进制值(仅在负数时包括符号). 

似乎我不得不在二进制概念上加重,我只有2个月的编程时间,我以为我知道二进制转换.

解决方案

从逻辑上讲:

签名中的边界

您有5位,因此有32种不同的组合.这意味着您可以用5位数字制作32个不同的数字.对于无符号整数,有意义的是在5位上存储0到31(含)之间的整数.

但是,这是关于无符号整数的.含义:我们还必须找到一种表示负数的方法.含义:我们必须存储数字的值,还必须存储其符号(+或-).所使用的表示形式是2的补数,并且是到处都学会的一种表示形式(也许存在其他形式,但我不知道它们).在此表示形式中,符号由第一位给出.也就是说,在2的补码表示中,正数以0开头,负数以1开头.

问题就出现了:0是正数还是负数?不能两者兼有,因为这意味着对于给定的数字a位,可以用两种方式表示0(对于5:00000和10000),这就是说我们失去了再放置一个数字的空间.我不知道他们是如何决定的,但事实是0是一个正数.对于任何数量的有符号或无符号位,仅用0表示0.

太好了.这为我们提供了第一个问题的答案:2的补码表示的十进制数的上限是多少?我们知道第一位是符号,因此我们可以表示的所有数字都必须由4位组成.我们可以有16种不同的4位字符串值,其中0是其中之一,因此上限为15.

现在,对于负数,这变得很容易.我们已经在5位上可以填充的32个值中填充了16个值.还剩16个.我们也知道0已经被表示了,所以我们不需要包含它.然后,我们从0:-1之前的数字开始.因为我们有16个数字表示,从-1开始,所以可以在5位上表示的最低带符号整数是-16.

更一般地说,使用 n 位,我们可以表示 2 ^ n 个数字.对于带符号整数,其中一半为正,一半为负.也就是说,我们有 2 ^(n-1)个正数和 2 ^(n-1)个负数.我们知道0被认为是正数,我们可以在 n 位上表示的最大有符号整数是 2 ^(n-1)-1 ,而最低的是-2 ^(n-1)

2的补码表示法

现在我们知道哪些数字可以用5位表示,问题是要知道我们如何表示它们.

我们已经看到符号显示在第一位,并且0被认为是正数.对于正数,其工作方式与处理无符号整数相同:00000为0,00001为1,00010为2,依此类推,直到01111为15.这是我们停止使用正号整数的原因,因为我们已经占用了所有我们有16个值.

对于带负号的整数,这是不同的.如果我们保持相同的表示形式(10001是-1,10010是-2,...),那么我们最终会得到11111为-15和10000未归因.我们可以决定说它是-16,但是每次使用负整数时,我们都必须检查这种特殊情况.另外,这搞乱了所有的二进制操作.我们还可以决定10000是-1,10001是-2,10010是-3,依此类推.但是它也弄乱了所有二进制运算.

2的补码按以下方式工作.假设您有一个带符号的整数10011,您想知道十进制是什么.

  1. 翻转所有位:10011-> 01100
  2. 添加1:01100-> 01101
  3. 将其读取为无符号整数:01101 = 0 * 2 ^ 4 + 1 * 2 ^ 3 + 1 * 2 ^ 2 + 0 * 2 ^ 1 + 1 * 2 ^ 0 = 13.

10011表示-13.这种表示非常方便,因为它可以双向工作.如何将-7表示为二进制有符号整数?从7的二进制表示形式开始,即00111.

  1. 翻转所有位:00111-> 11000
  2. 添加1:11000-> 11001

就是这样!在5位上,-7表示为11001.

我不会介绍它,但是2的补码的另一个巨大优点是加法的工作方式相同.也就是说,在添加两个二进制数字时,您不必关心它们是带符号的还是无符号的,这与后面的算法相同.

有了这个,您应该能够回答问题,但更重要的是要理解答案.

该主题非常适合理解2的补码:calculator that does the conversion. But I'm not sure how to find the maximum and the minumum number or convert if a binary number is not given, and question in StackO seems to be about converting specific numbers or doesn't include signed numbers to a specific bit.

The specific question is:

We have only 5 bits for representing signed numbers in two's complement:

What is the highest signed integer? 
Write its decimal value (including the sign only if negative).

What is the lowest signed integer? 
Write its decimal value (including the sign only if negative).

Seems like I'll have to go heavier on binary concepts, I just have 2 months in programming and I thought i knew about binary conversion.

解决方案

From a logical point of view:

Bounds in signed representation

You have 5 bits, so there are 32 different combinations. It means that you can make 32 different numbers with 5 bits. On unsigned integers, it makes sense to store integers from 0 to 31 (inclusive) on 5 bits.

However, this is about unsigned integers. Meaning: we have to find a way to represent negative numbers too. Meaning: we have to store the number's value, but also its sign (+ or -). The representation used is 2's complement, and it is the one that's learned everywhere (maybe other exist but I don't know them). In this representation, the sign is given by the first bit. That is, in 2's complement representation a positive number starts with a 0 and a negative number starts with an 1.

And here the problem rises: Is 0 a positive number or a negative number ? It can't be both, because it would mean that 0 can be represented in two manners for a given number a bits (for 5: 00000 and 10000), that is we lose the space to put one more number. I have no idea how they decided, but fact is 0 is a positive number. For any number of bits, signed or unsigned, a 0 is represented with only 0.

Great. This gives us the answer to the first question: what is the upper bound for a decimal number represented in 2's complement ? We know that the first bit is for the sign, so all of the numbers we can represent must be composed of 4 bits. We can have 16 different values of 4-bits strings, and 0 is one of them, so the upper bound is 15.

Now, for the negative numbers, this becomes easy. We have already filled 16 values out of the 32 we can make on 5 bits. 16 left. We also know that 0 has already been represented, so we don't need to include it. Then we start at the number right before 0: -1. As we have 16 numbers to represent, starting from -1, the lowest signed integer we can represent on 5 bits is -16.

More generally, with n bits we can represent 2^n numbers. With signed integers, half of them are positive, and half of them are negative. That is, we have 2^(n-1) positive numbers and 2^(n-1) negative numbers. As we know 0 is considered as positive, the greatest signed integer we can represent on n bits is 2^(n-1) - 1 and the lowest is -2^(n-1)

2's complement representation

Now that we know which numbers can be represented on 5 bits, the question is to know how we represent them.

We already saw the sign is represented on the first bit, and that 0 is considered as positive. For positive numbers, it works the same way as it does for unsigned integers: 00000 is 0, 00001 is 1, 00010 is 2, etc until 01111 which is 15. This is where we stop for positive signed integers because we have occupied all the 16 values we had.

For negative signed integers, this is different. If we keep the same representation (10001 is -1, 10010 is -2, ...) then we end up with 11111 being -15 and 10000 not being attributed. We could decide to say it's -16 but we would have to check for this particular case each time we work with negative integers. Plus, this messes up all of the binary operations. We could also decide that 10000 is -1, 10001 is -2, 10010 is -3 etc. But it also messes up all of the binary operations.

2's complement works the following way. Let's say you have the signed integer 10011, you want to know what decimal is is.

  1. Flip all the bits: 10011 --> 01100
  2. Add 1: 01100 --> 01101
  3. Read it as an unsigned integer: 01101 = 0*2^4 + 1*2^3 + 1*2^2 + 0*2^1 + 1*2^0 = 13.

10011 represents -13. This representation is very handy because it works both ways. How to represent -7 as a binary signed integer ? Start with the binary representation of 7 which is 00111.

  1. Flip all the bits: 00111 --> 11000
  2. Add 1: 11000 --> 11001

And that's it ! On 5 bits, -7 is represented by 11001.

I won't cover it, but another great advantage with 2's complement is that the addition works the same way. That is, When adding two binary numbers you do not have to care if they are signed or unsigned, this is the same algorithm behind.

With this, you should be able to answer the questions, but more importantly to understand the answers.

This topic is great for understanding 2's complement: Why is two's complement used to represent negative numbers?

这篇关于用5位二进制补码表示有符号数的最高和最低整数是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆