二进制补码二进制形式

发布于 07-15 09:22 字数 378 浏览 18 评论 0原文

在 TC++ 编译器中,5 的二进制表示为 (00000000000000101)。 我知道负数存储为2的补码,因此二进制中的-5(111111111111011)。 最高有效位(符号位)为 1,表示它是负数。

那么编译器怎么知道它是-5呢? 如果我们将上面给出的二进制值(1111111111111011)解释为无符号数,结果会完全不同吗?

另外,为什么1的补语是5 -6 (1111111111111010)

In a TC++ compiler, the binary representation of 5 is (00000000000000101).
I know that negative numbers are stored as 2's complement, thus -5 in binary is (111111111111011). The most significant bit (sign bit) is 1 which tells that it is a negative number.

So how does the compiler know that it is -5? If we interpret the binary value given above (111111111111011) as an unsigned number, it will turn out completely different?

Also, why is the 1's compliment of 5 -6 (1111111111111010)?

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

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

发布评论

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

评论(8

遥远的她2024-07-22 09:22:02

编译器不知道。 如果将 -5 转换为 unsigned int,您将得到 32763

The compiler doesn't know. If you cast -5 to unsigned int you'll get 32763.

独行侠2024-07-22 09:22:02

编译器知道,因为这是 CPU 本身使用的约定。 您的计算机有一个 CPU 以二进制补码表示法存储负数,因此编译器也会这样做。 如果您的 CPU 支持补码表示法,编译器将使用它(顺便说一下,IEEE 浮点数就是这种情况)。

关于该主题的维基百科文章解释了二进制补码表示法的工作原理。

The compiler knows because this is the convention the CPU uses natively. Your computer has a CPU that stores negative numbers in two's complement notation, so the compiler follows suit. If your CPU supported one's complement notation, the compiler would use that (as is the case with IEEE floats, incidentally).

The Wikipedia article on the topic explains how two's complement notation works.

桃酥萝莉2024-07-22 09:22:02

处理器实现有符号和无符号指令,它们将以不同的方式对二进制数表示进行操作。 编译器根据所涉及操作数的类型(即intunsigned int)知道要发出哪些指令。

编译器不需要知道数字是否为负数,它只是为所涉及的类型发出正确的机器或中间语言指令。 这些指令的处理器或运行时实现通常不太关心数字是否为负数,因为二进制补码算术的公式对于正数或负数是相同的(事实上,这是主要的)二进制补码算术的优点)。 需要知道一个数字是否为负数,类似于 printf(),正如 Andrew Jaffe 指出的,设置的 MSBit 表示负数二进制补码。

The processor implements signed and unsigned instructions, which will operate on the binary number representation differently. The compiler knows which of these instructions to emit based on the type of the operands involved (i.e. int vs. unsigned int).

The compiler doesn't need to know if a number is negative or not, it simply emits the correct machine or intermediate language instructions for the types involved. The processor or runtime's implementation of these instructions usually doesn't much care if the number is negative or not either, as the formulation of two's complement arithmetic is such that it is the same for positive or negative numbers (in fact, this is the chief advantage of two's complement arithmetic). What would need to know if a number is negative would be something like printf(), and as Andrew Jaffe pointed out, the MSBit being set is indicative of a negative number in two's complement.

初心2024-07-22 09:22:02

第一个位仅针对负数设置(称为符号位)

详细信息可在 此处< /a>

The first bit is set only for negative numbers (it's called the sign bit)

Detailed information is available here

神爱温柔2024-07-22 09:22:02

二进制补码的核心部分是机器语言的加法和减法指令可以忽略所有这些,只进行二进制算术,它就可以工作......

即,二进制 2 的补码中的 -3 + 4

   1111 1111 1111 1101   (-3)
+  0000 0000 0000 0100   ( 4)
   -------------------
   0000 0000 0000 0001   ( 1)

The kewl part of two's complement is that the machine language Add, and Subtract instructions can ignore all that, and just do binary arithmetic and it just works...

i.e., -3 + 4

in Binary 2's complement, is

   1111 1111 1111 1101   (-3)
+  0000 0000 0000 0100   ( 4)
   -------------------
   0000 0000 0000 0001   ( 1)
流殇2024-07-22 09:22:02

让我们举个例子:
我们在二进制的两个字节中有两个数字:
A = 10010111
B=00100110
(注意,机器在这个级别不知道有符号或无符号的概念)

现在当你说“添加”这两个时,机器会做什么? 它只是添加:

R = 10111101(并进位:1)

现在,我们作为编译器需要解释该操作。 我们有两个选择:数字可以有符号或无符号。

1-无符号情况:在c中,数字的类型为“unsigned char”,值为151和38,结果为189。这很简单。

2 - 带符号的情况:我们编译器根据最高有效位解释数字,第一个数字是 -105,第二个数字仍然是 38。所以 -105 + 38 = -67。 但 -67 是 10111101。但这就是我们在结果 (R) 中已经得到的! 结果是相同的,唯一的区别是编译器如何解释它。

结论是,无论我们如何考虑数字,机器对数字执行相同的操作。 但编译器会依次解释结果。

请注意,机器并不知道 2 的补码的概念。 它只是将两个数字相加,而不关心内容。 然后,编译器查看符号位并做出决定

说到减法,这一次的操作也是独一无二的:取第二个数字的 2 补码,然后将两者相加。

let us give an example:
we have two numbers in two bytes in binary:
A = 10010111
B = 00100110
(note that the machine does not know the concept of signed or unsigned in this level)

now when you say "add" these two, what does the machine? it simply adds:

R = 10111101 (and carry bit : 1)

now, we -as compiler- need to interpret the operation. we have two options: the numbers can be signed or unsigned.

1- unsigned case: in c, the numbers are of type "unsigned char" and the values are 151 and 38 and the result is 189. this is trivial.

2 - signed case: we, the compiler, interpret the numbers according to their msb and the first number is -105 and the second is still 38. so -105 + 38 = -67. But -67 is 10111101. But this is what we already have in the result (R)! The result is same, the only difference is how the compiler interprets it.

The conclusion is that, no matter how we consider the numbers, the machine does the same operation on the numbers. But the compiler will interpret the results in its turn.

Note that, it is not the machine who knows the concept of 2's complement. it just adds two numbers without caring the content. The compiler, then, looks at the sign bit and decides.

When it comes to subtraction, this time again, the operation is unique: take 2's complement of the second number and add the two.

じ违心2024-07-22 09:22:02

如果该数字被声明为有符号数据类型(而不是类型转换为无符号类型),那么编译器将知道,当符号位为 1 时,它是一个负数。 至于为什么使用 2 的补码而不是 1 的补码,你不希望能够得到 -0 的值,而 1 的补码允许你这样做,所以他们发明了 2 的补码来解决这个问题。

If the number is declared as a signed data type (and not type cast to an unsigned type), then the compiler will know that, when the sign bit is 1, it's a negative number. As for why 2's complement is used instead of 1's complement, you don't want to be able to have a value of -0, which 1's complement would allow you to do, so they invented 2's complement to fix that.

深巷少女2024-07-22 09:22:02

这正是最重要的位——如果您知道一个数字有符号,那么如果 MSB=1,编译器(和运行时!)就知道将其解释为负数。 这就是为什么类 C 语言同时具有整数(正数和负数)和无符号整数——在这种情况下,您将它们全部解释为正数。 因此,有符号字节的范围是 -128 到 127,而无符号字节的范围是 0 到 255。

It's exactly that most significant bit -- if you know a number is signed, then if the MSB=1 the compiler (and the runtime!) knows to interpret it as negative. This is why c-like languages have both integers (positive and negative) and unsigned integers -- in that case you interpret them all as positive. Hence a signed byte goes from -128 to 127, but an unsigned byte from 0 to 255.

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