Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a binary integral number represents a negative number?

Tags:

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?

like image 460
ipkiss Avatar asked Oct 17 '11 13:10

ipkiss


People also ask

What is the most common way to represent negative integers in binary form?

Negative numbers can also be represented in binary. The name of the system most commonly used to represent and handle negative numbers is 'Two's complement'.

How do you know if a number is negative in 2's complement?

It's first (leftmost) bit is 1, which means that this represents a number that is negative. That's just the way that things are in two's complement: a leading 1 means the number is negative, a leading 0 means the number is 0 or positive. To see what this number is a negative of, we reverse the sign of this number.

Do negative binary numbers exist?

Signed Binary Numbers We need an extra step to determine the sign of the result of an addition. Why? Both positive and negative zero exist in this representation.


2 Answers

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 
like image 123
StuartLC Avatar answered Sep 28 '22 02:09

StuartLC


It depends on the representation, of course. In two's complement, which is widely used, you simply look at the most significant bit.

like image 25
Tom Zych Avatar answered Sep 28 '22 02:09

Tom Zych