c++中负数如何编码
发布网友
发布时间:2023-09-19 21:43
我来回答
共5个回答
热心网友
时间:2024-08-12 06:24
首先,我们看一下这个数轴,8位二进制表示数值型数据,它应该是落在0~256(不含 256)区间上,当这个数落在0~128(不含128)区间上时,我们说它是一个正数,至于正多少取决于这个数从0点往右走了多少。 如:+33对应的二进制是00010001 如果这个数落在后半轴上,也就是128~256(不含256)区间上时,我们说它是一个负数,至于负多少取决于它离终点256还差多少。 如:156表示就不再是+156,而是表示-100,二进制编码为:10011100 稍微细心的朋友可能会发现,凡是落在后半轴的数,也就是负数,都有一个共同点,那就是这个数第1位二进制编码一定是1,而落在前半轴的数第1位一定是0,这样就造成很多人误认为1就是仅仅简单的表示负号,而0表示正号,这是不科学的。但反过来说是可以的:负数的最高位一定是1,正数的最高位一定是0 可能有朋友会问了,156表示不是+156,那+156该怎么表示呢,它的二进制编码又是多少呢? 在这里我要强调一下,我们现在是假设只用1个字节(也是8位二进制)来表示带符号的整数,那么它能表示的数的范围只能是-128~+127之间,156是表示不下的,明白吗。 好的,到这里相信各位基本了解了负数的表示原理了,在这里我还想沟通一下求负数表示的另外一种方法,这就是很多书上提到的采用补码的方法 要求一个负数的二进制表示方式,可以先求出它的原码,然后每位取反得出它的反码,反码再加1得出补码 如:假设还是以8位二进制来表示带符号的整数 -56的原码是:00111000 那么它的反码是:11000111 补码为:11000111+1=11001000 得出,-56在计算机内部是用11001000这串二进制来表示的
vae.la
热心网友
时间:2024-08-12 06:24
负数在表达式中还是加个负号‘-’就可以了,但是计算机中存储负号用的是二进制补码,这个和C是一样的,参见谭浩强C语言
热心网友
时间:2024-08-12 06:25
Representing Negative Numbers
It’s all well and good that we can represent quantities using bits and perform basic arithmetic on them. But we realize that numbers can be signed. That is, they could be negative. How does one go about representing a negative quantity using bits? After all, a bit can exist in only two states - 1 and 0. No intermediate state is possible, so how does one represent the minus sign we’re so used to seeing before negative numbers? How would you store a -10, for instance, in a byte? Again, think about this and come up with a strategy of your own before reading on.
Sign Magnitude Notation for Negative Numbers
Here’s how we do it: Or more accurately, here’s one (not the best) way to do it: We realize that a byte has 8 bits, and agree to use its left-most (or right-most) bit as the signature of a number stored in that byte. If the sign bit (signature) is1, we take it that the represented quantity is negative. If it’s 0, then it’s non-negative.
The compromise here is that we’ve sacrificed a whole bit for the ability to represent negative numbers. This only leaves the 7 other bits to store the actual value of the number (its magnitude), which implies that the maximum value that can be stored in a signed byte is 27 -1 which is only 127.
Note that 7 bits still gives us 128 distinct bit patterns. It’s just that we use one of those to represent 0, leaving 127 patterns for the positive integers).
This way of storing numbers is called the Sign-Magnitude notation for the simple reason we store the signature and the magnitude in two explicitly distinct locations.
There is an obvious drawback with this technique. Can you think of what it is? There’s a rendancy in that a certain number can be stored in two different ways. In other words, we end up wasting one bit pattern by making it the same value represented by another bit pattern - What value is this?
Chances are you guessed right - both 10000000 and 00000000 represent 0, since -0 = 0. Ideally, we’d like to make efficient use of the space of possible patterns available to us. And besides, arithmetic with numbers stored in sign magnitude notation is slightly more cumbersome than with another technique which we will now discuss.
热心网友
时间:2024-08-12 06:25
负数:
原码就是原来的表示方法
反码是除符号位(最高位)外取反
补码=反码+1
1111 1111 1111 1111,即每4位隔一空格。
双字节数最大值为:
1 * 215 + 1 *214 + 1* 213 + 1 * 212 + 1 * 211 + 1 * 210 + …… + 1 * 22 + 1 * 21 + 1* 20 = 65535
热心网友
时间:2024-08-12 06:26
这个问题有必要深入研究吗? 本末倒置了吧 兄弟