有符号和无符号整数——为什么字节的处理方式不同?
我目前正在学习高级汇编语言,并且正在研究有符号和无符号整数的概念。这看起来很简单,但是签署延期让我很困惑。
取字节 10011010,我将其视为十进制的 154。事实上,使用二进制计算器选择除单词以外的任何内容都会将其显示为十进制 154。
但是,如果我选择单位为字节,并输入 10011010,那么它会突然被视为十进制的 -102。每当我从一个字节开始增加它时,它就会被符号扩展,并且始终是十进制的-102。
如果我使用大于一个字节的任何值,那么它仍然是十进制的 154。
有人可以解释一下这种看似不一致的现象吗?
I am learning High Level Assembly Language at the moment, and was going over the concept of signed and unsigned integers. It seems simple enough, however getting to sign extension has confused me.
Take the byte 10011010 which I would take to be 154 in decimal. Indeed, using a binary calculator with anything more than word selected shows this as 154 in decimal.
However, if I select the unit to be a byte, and type in 10011010 then suddenly it is treated as -102 in decimal. Whenever I increase it starting from a byte then it is sign extended and will always be -102 in decimal.
If I use anything higher than a byte then it remains 154 in decimal.
Could somebody please explain this seeming disparity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您选择单位为字节时,
10011010
的 MSB 将被视为有符号位,这使得该单字节有符号整数等效于 -102(2 的补码)。对于大于 8 位的整数,例如 16 位,数字将为:
0000000010011010
,其 MSB 中没有 1,因此它被视为正整数,其整数表示形式为十进制 154。当您将 8 位字节转换为更高类型时,符号扩展也会在更大长度的存储中保留 -ve 解释。When you select the unit as a byte the MSB of
10011010
is treated as the signed bit, which makes this one byte signed integer equivalent interpretation to -102 (2's complement).For integers sized large than 8 bits, say 16 bits the number will be:
0000000010011010
which do not have 1 in MSB therefore it is treated as a positive integer whose integer representation is 154 in decimal. When you convert the 8 bit byte to a higher type the sign extension will preserve the -ve interpretation in the larger length storage too.