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 num开发者_如何学Cber, 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?
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?
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:
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
i.e. 10100100
could represent:
4 + 32 + 128
= 164
(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:
–2147483648 to 2147483647
Whereas an unsigned 32 bit integer can represent numbers from
0 to 4294967295
It depends on the representation, of course. In two's complement, which is widely used, you simply look at the most significant bit.
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.
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 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).
精彩评论