问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

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

这个问题有必要深入研究吗? 本末倒置了吧 兄弟
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
QQ密码有密保,密保要忘了,密码会被盗吗?密码怎么能改? dj_男声_azha_azha_hei_a_a_bei 看图把单词补充完整牛奶m一lk talk有几个音节 小学作业读一读,再用音节连成一句通顺的话,题目是1.洗 妈妈 衣服怎么... 把音节连成一句话ye duo mei se li 用下面的音节连成一句话zài , zhī ,jiào ,tóu ,niǎo 我是E520用户怎么进入BIOS模式?开机说按Enter 没反应 F1 F2都试了,没... 过去现在时 have /has been doing和have/ has +过去分词的区别_百度知 ... ...forty years. C,has been open D,has been opened详细疑问如下_百度... 鸡蛋黄瓜卷 c语言 用一个符号或者是* for循环 输出一个字母 c语言 输出用*组成的输入数(输入什么,就输出用*拼成的该数或该字母) 用*号输出字母C的图案。 C语言:用*号输出大写字母“C”的图案 音问[yīn wèn]什么意思?近义词和反义词是什么?英文翻译是什么? 水浒传世说新语聊斋志异的语言风格异同 水浒传的语言风格 异地男友因为我很晚才睡,他现在都用,"滚去睡觉"这词来说,这是怎么口... 喝普洱茶生茶的不良反应症状 ...当法人有百分之八股份说年底给我分红结果又不给了 口袋妖怪白金版化石究竟在地下的哪里? ...公司拖欠我3月工资,并且一年未分红,我收货款抵扣违法吗?_百度... 关于口袋妖怪钻石的几个问题 公司让员工投钱获取分红权但无实际股份涉嫌非吸吗 口袋妖怪地下挖的化石问题 口袋妖怪黑白明明给了我化石怎么没有啊 win10 Microsoft Edge浏览器如何添加插件 金毛卷曲睡觉什么原因 ...预订明年一月份到三月份的学生火车票吗?我怎么没法预定呢_百度知 ... 蒙海金的个性签名怎么写好看 我叫张蒙 能不能给我个个性签名 我喜欢一个女孩叫梁蒙蒙。怎么表达在个性签名里。不要太露骨了。最好... 蒙永学个性签名怎么写 蒙永奇个性签名怎么写 帮‘蒙珊珊‘’’做个一笔画个性签名 梦见爸爸从学校接我回家 全日制大专自己报名途径 22款奔达金吉拉300 ps快捷键在哪里设置,怎么设置? 关于白银市迎凤湖的作文 机械电表的快慢与安数的大小有何美系 14200w的用电量应匹配多少安倍电表 魅族20pro配置是什么? 光大集团董事长助理是谁 问下车质汇的刹车是假货吗? 怎样应对会场中的尴尬事情? 想问abbr是什么词性 请问一下abbr是什么词性 经常聊天的女生特地发信息和我说,她要睡觉了,到底是什么意思?&...