C语言 如何知道二进制整数是否代表负数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7794653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know if a binary integral number represents a negative number?
提问by ipkiss
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.
我正在阅读一些 C 文本。在负值和正值会议中,作者提到了几种以二进制形式表示负数的方法。
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 10100100can we say that is -92, and not other non-negative number?
例如,-92 具有 8 位二进制形式:10100100. 但是如果给定10100100我们可以说是-92,而不是其他非负数吗?
采纳答案by Tom Zych
It depends on the representation, of course. In two's complement, which is widely used, you simply look at the most significant bit.
当然,这取决于代表。在广泛使用的二进制补码中,您只需查看最高位。
回答by StuartLC
For example, the (number) -92 has the binary form: 10100100 (in an 8 bit byte representation) . But if we are given 10100100, can we say that is -92, and not other non-negative number?
例如,(数字)-92 具有二进制形式: 10100100 (以 8 位字节表示)。但是如果给我们10100100,我们能说它是-92,而不是其他非负数吗?
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 encodingused 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.
如果 8 位整数(即字节)是有符号的,那么根据 Tom 和 32bitkid,有符号整数通常存储在2 的补码中,其中最高有效位 (MSB)将确定一个数字是否为负。
e.g. In your example, the byte 10100100could either represent the signed byte-92, since:
例如,在您的示例中,字节10100100可以表示有符号字节-92,因为:
MSB : 1 means negative
Other 7 Bits 0100100
Flip and add 1 => 1011011 + 1 = 1011100
From powers of two, right to left :
0*2^0 + 0*2^1 + 1*2^2 + 1*2^3 + 1*2^4 + 0*2^5 + 1*2^6
= 4 + 8 + 16 + 64
= 92 (and thus -92 because of the MSB)
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
或如果该值是一个无符号字节,则 MSB 仅被视为 2 的下一个幂,与所有低位相同
i.e. 10100100could represent:
即10100100可以代表:
4 + 32 + 128
= 164
(again, powers of two, right to left, and omitting the 0powers of two)
(再次,2 的幂,从右到左,并省略 2 的0幂)
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:
关于一个整数是否应该有符号以及所需的位数的决定通常取决于您需要存储在其中的值的范围。例如,一个 32 位有符号整数可以表示范围:
–2147483648 to 2147483647
Whereas an unsigned 32 bit integer can represent numbers from
而一个无符号的 32 位整数可以表示来自
0 to 4294967295
回答by bw1024
You want to read up on two's complementnumbers. 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.
我重读了你的问题,你说你已经理解了二进制补码。在处理负数时,必须知道位数才能确定该数是否为负数。负数必须被符号扩展到所需的位数。您以 32 位存储时的 -92 示例将是 111111111111111111111111110100100。
回答by user3427258
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 .
您必须知道数字的类型(有符号/无符号)才能确定负数/正数。如果未提及类型,则默认情况下它是已签名的。如果它是有符号的,那么您可以查看 MSB 位以确定正数或负数 no 。如果它被提及为 unsigned 那么你必须计算 MSB 位才能使十进制 no 。
回答by Davislor
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 ((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).
如果您尝试从字符串解析二进制常量,则需要知道数字的格式。然而,二元补码已经普及了五十年。(一个例外是对某些仍在使用的旧 Unisys 大型机的二进制兼容支持。)为此,您只需要查看第一位(如已接受的答案所述)。

