如何知道一个二进制整数是否代表负数?
我正在读一些 C 文本。在负值和正值环节中,作者提到了用二进制形式表示负数的几种方法。
我完全明白了,想知道是否给定一个二进制数,我们可以确定它是否为负数?
例如,-92 具有 8 位二进制形式:10100100
。但是如果我们给出10100100
,我们可以说它是-92,而不是其他非负数吗?
I am reading some C text. In the Negative and Positive Values session, the author mentioned several ways of representing a negative number in binary form.
I understood all of the way and was wondering if with a give binary number, can we determine if it is negative?
For example, the -92 has the 8-bit binary form: 10100100
. But if we are given 10100100
can we say that is -92, and not other non-negative number?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,你需要提前知道是否使用了有符号或无符号表示/约定,即使如果您知道它已签名,那么您还需要知道用于存储该号码的编码。
如果8位整数(即字节)是有符号的,那么根据Tom和32bitkid,有符号整数通常存储在2的补码,其中 最高有效位(MSB) 将确定一个数字是否为负数。
例如,在您的示例中,字节
10100100
可以表示有符号字节-92
,因为:或者如果该值是无符号字节,那么 MSB 就被视为 2 的下一个幂,与所有较低位相同
,即
10100100
可以表示:(同样,2 的幂,对向左,并省略
0
2 的幂)关于整数是否应该带符号以及所需位数的决定通常由值的范围决定您需要将其存储在其中。例如,一个 32 位有符号整数可以表示范围:
而一个无符号 32 位整数可以表示以下范围的数字:
No, you will need to know in advance whether a signed or unsigned representation / convention was used, and even if you know it is signed, then you will also need to know the encoding used to store the number.
If the 8-bit integer (i.e. byte) is signed, then as per Tom and 32bitkid, signed integers are usually stored in 2's complement, where the Most Significant Bit (MSB) will determine whether a number is negative or not.
e.g. In your example, the byte
10100100
could either represent the signed byte-92
, since:OR if the value is an unsigned byte, then the MSB is just treated as the next power of 2, the same as all the lower bits
i.e.
10100100
could represent:(again, powers of two, right to left, and omitting the
0
powers of two)The decision as to whether an integer should is signed or not, and the number of bits required, is generally determined by the range of values that you need to store in it. For example, a 32 bit signed integer can represent the range:
Whereas an unsigned 32 bit integer can represent numbers from
当然,这取决于代表性。在广泛使用的二进制补码中,您只需查看最高有效位即可。
It depends on the representation, of course. In two's complement, which is widely used, you simply look at the most significant bit.
您想要了解二进制补码数字。简而言之,最高有效位可以用来判断该数是否为负数。
我重读了你的问题,你说你已经理解了补码。处理负数时,必须知道位数才能确定该数是否为负数。负数必须符号扩展至所需的位数。以 32 位存储时,-92 的示例将为 11111111111111111111111110100100。
You want to read up on two's complement numbers. In short, the most significant bit can be used to determine if the number is negative.
I reread your question and you said you already understand two's complement. When dealing with negative numbers, the number of bits must be known to determine if the number is negative or not. A negative number must be sign extended to the required number of bits. Your example of -92 when stored in 32 bits would be 11111111111111111111111110100100.
您必须知道数字的类型(有符号/无符号)才能确定负数/正数。如果未提及类型,则默认情况下它是有符号的。如果它有符号,那么您可以查看 MSB 位来确定正号或负号。如果它被提到为无符号,那么你必须计算 MSB 位才能得到十进制 no 。
You must be required to know the type(signed/unsigned) of the number to determine negative/positive number. If type is not mentioned then by default it is signed . If it is signed then you can look MSB bit to determine positive or negative no . If it is mentioned as unsigned then you have to count MSB bit to make decimal no .
如果内存中有该值,请将其转换为相同大小的有符号数,并测试它是否小于零。因此,
if ((int)value < 0)
。如果您尝试从字符串中解析二进制常量,则需要知道数字的格式。然而,二进制补码已经普及了五十年了。 (一个例外是对某些仍在使用的旧 Unisys 大型机的二进制兼容支持。)为此,您只需要查看第一位(正如公认的答案所述)。
If you have the value in memory, cast it to a signed in the same size and test if it’s less than zero. So,
if ((int)value < 0)
.If you’re trying to parse a binary constant from a string, you need to know the format of the number. However, two’s-complement has been universal for fifty years now. (The one exception is binary-compatible support for certain old Unisys mainframes that are still being used.) For that, you just need to look at the first bit (as the accepted answer says).