二进制负整数

发布于 2024-11-05 05:18:38 字数 205 浏览 0 评论 0原文

 5 (decimal) in binary  00000101
-5 (two's complement) in binary 11111011

但 11111011 也是 251(十进制)!

计算机如何区分一个与另一个? 它怎么知道是-5还是251?

这是相同的 11111011

提前致谢!

 5 (decimal) in binary  00000101
-5 (two's complement) in binary 11111011

but 11111011 is also 251 (decimal)!

How does computer discern one from another??
How does it know whether it's -5 or 251??

it's THE SAME 11111011

Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云雾 2024-11-12 05:18:38

有符号字节的最大值为 127

无符号字节不能为负数。

编译器知道保存该值的变量是有符号类型还是无符号类型,并适当地处理它。

Signed bytes have a maximum of 127.

Unsigned bytes cannot be negative.

The compiler knows whether the variable holding that value is of signed or unsigend type, and treats it appropriately.

呆头 2024-11-12 05:18:38

如果您的程序选择将字节视为有符号,则运行时系统会根据高位决定该字节是正数还是负数。该高位(从低位 0 开始计算的第 7 位)为 1 时表示该数为负数;该位中的 0 表示该数字为正数。因此,在 11111011 的情况下,第 7 位设置为 1,并且该数字相应地被视为负数。

由于符号位占用一位位置,因此数字的绝对大小范围可以从 0 到 127,如前所述。

另一方面,如果您的程序选择将字节视为无符号,则符号位将包含在幅度中,幅度范围为 0 到 255。

If your program chooses to treat the byte as signed, the run-time system decides whether the byte is to be considered positive or negative according to the high-order bit. A 1 in that high-order bit (bit 7, counting from the low-order bit 0) means the number is negative; a 0 in that bit position means the number is positive. So, in the case of 11111011, bit 7 is set to 1 and the number is treated, accordingly, as negative.

Because the sign bit takes up one bit position, the absolute magnitude of the number can range from 0 to 127, as was said before.

If your program chooses to treat the byte as unsigned, on the other hand, what would have been the sign bit is included in the magnitude, which can then range from 0 to 255.

临风闻羌笛 2024-11-12 05:18:38

二进制补码的设计目的是允许有符号数以与无符号数相同的方式相互加/减。因此,只有两种情况数字的符号性会在低级别影响计算机。

  1. 出现溢出
  2. 当对混合执行操作时

:一个有符号,一个无符号不同的处理器对此采取不同的策略。例如,WRT 或 verflows(MIPS RISC 架构)使用陷阱来处理溢出。请参阅http://en.wikipedia.org/wiki/MIPS_architecture#MIPS_I_instruction_formats

以获得最佳效果据我所知,需要在程序级别避免混合签名和未签名。

Two's complement is designed to allow signed numbers to be added/substracted to one another in the same way unsigned numbers are. So there are only two cases where the signed-ness of numbers affect the computer at low level.

  1. when there are overflows
  2. when you are performing operations on mixed: one signed, one unsigned

Different processors take different tacks for this. WRT orverflows, the MIPS RISC architecture, for example, deals with overflows using traps. See http://en.wikipedia.org/wiki/MIPS_architecture#MIPS_I_instruction_formats

To the best of my knowledge, mixing signed and unsigned needs to avoided at a program level.

硬不硬你别怂 2024-11-12 05:18:38

如果您问“程序如何知道如何解释该值” - 一般来说,这是因为您已经告诉编译器您为其赋值的变量的“类型”。该程序实际上并不关心00000101是否为“5进制”,它只是有一个值为00000101的无符号整数,它可以执行对无符号整数合法的操作,并且如果您尝试比较或转换为不同“类型”的变量,将以给定的方式表现。

归根结底,编程中的所有内容都归结为二进制——所有数据(字符串、数字、图像、声音等),而编译后的代码最终只是一个大的二进制 blob。

If you're asking "how does the program know how to interpret the value" - in general it's because you've told the compiler the "type" of the variable you assigned the value to. The program doesn't actually care if 00000101 as "5 decimal", it just has an unsigned integer with value 00000101 that it can perform operations legal for unsigned integers upon, and will behave in a given manner if you try to compare with or cast to a different "type" of variable.

At the end of the day everything in programming comes down to binary - all data (strings, numbers, images, sounds etc etc) and the compiled code just ends up as a large binary blob.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文