Signed and Unsigned binary number

binary

Can someone explain me how to see if a binary number is signed or unsigned and explain me about signed and unsigned numbers.

Best Answer

In general, for the computer a string of bits is just a string of bits - you need to tell the computer what is represented there using what method. This is an important concept to understand. This is why no one can say for sure what is represented by 0xFF without the proper context.

For negative numbers there are a few common systems, with 2's complement being the most popular one.

Sign & Magnitude You use the MSB to represent the sign of the number. 0 is for positive, 1 is for negative. 0b00000010 is decimal 2, 0b10000010 is -2.

1's Complement You represent a binary number by the binary number you need to add to it in order to get all 1's (which means flipping all the bits). 0b0000010 is decimal 2, its 1's complement is 0b1111101. Now, do the same thing with the MSB as in S&M (sorry) and you get 0b00000010 for decimal 2 and 0b11111101 for -2. One of the issues with this system is having two representations for 0 (all 1's and all 0's).

2's Complement You represent a binary number by the binary number you need to add to it in order to get all 0's. You convert to 1's complement and add 1 - simple! MSB is used as sign indicator. so 0b00000010 is decimal 2, and 0b11111110 is -2.

Offset Binary This is long gone (I think) but basically you do 2's complement and invert the MSB used for the sign representation.

There are good reasons why 2's complement is the most commonly used system but I think that is outside the scope of this question.