Can someone explain me how to see if a binary number is signed or unsigned and explain me about signed and unsigned numbers.
-
7\$\begingroup\$ Signed vs. unsigned is generally just in how you interpret the bit pattern--there's nothing in the bits themselves to tell you whether they were supposed to be signed or unsigned. \$\endgroup\$– Jerry CoffinCommented Jan 14, 2015 at 16:15
-
1\$\begingroup\$ A signed binary stream will also look exactly the same as an unsigned binary stream. 1111. Is that -1 or 15? \$\endgroup\$– user16222Commented Jan 14, 2015 at 16:54
-
\$\begingroup\$ Can you edit your question to be more specific? "Explain to me about ____" posts, while I'm sure it could be an interesting discussion, are not in the scope of the StackExchange as they are just that: discussions. \$\endgroup\$– Adam HeadCommented Jan 14, 2015 at 18:24
1 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.
-
\$\begingroup\$ I've seen some accelerometers use offset binary. \$\endgroup\$ Commented Jan 14, 2015 at 16:52