# Binary numbers, signed, unsigned.

• 11-11-2009
JOZZY& Wakko
Binary numbers, signed, unsigned.
I am trying to read up on binary numbers but I do not fully understand how I can see i a binary number is signed or unsigned.

Could someone explain this for me?
• 11-11-2009
zacs7
It all depends on how the signed number is represented in binary, common techniques include ones complement, two's complement, excess-k or signed magnitude.

The easiest being signed magnitude, where the most significant bit is used to for the sign (i.e. 1 is negative, 0 is positive). eg,
5 = 0101
-5 = 1101

See Signed number representations - Wikipedia, the free encyclopedia
[/edit]
• 11-11-2009
RockyMarrone
There is always a signed bit attached with the number it will depict that
• 11-11-2009
JOZZY& Wakko
Does that mean that if a number is stored in 8-bits for example "01000001" the 7th bit (0 to 7) refers to that it is a positive number?

Then how do you prevent mistakes? For example "10111111" is -65 in binary-2 but it also refers to the number 191 in decimal.

Edited:

Quote:

Originally Posted by RockyMarrone
There is always a signed bit attached with the number it will depict that

Ok that explains more so by requiring a signed bit this mistake can not be made?
• 11-11-2009
zacs7
Quote:

Originally Posted by RockyMarrone
There is always a signed bit attached with the number it will depict that

No there isn't. Most CPU's operate in unsigned magnitude, two's complement or excess-k ... usually.

The CPU will know how the signed numbers are represented (it will infact require them to be a certain representation). Usually there are math instructions provided for both unsigned and signed integer math.

Code:

`For example "10111111" is -65 in binary-2 but it also refers to the number 191 in decimal.`
Reading "10111111" as signed magnitude gives, (working from right to left)
2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5
= 63
And the MSB is "1", which gives a negative sign.
= -63

Using signed magnitude again, if you wanted to represent 63 then that's "00111111". Note that the MSB (left most bit) is now binary 0.
• 11-11-2009
JOZZY& Wakko
Quote:

Originally Posted by zacs7
Reading "10111111" as signed magnitude gives, (working from right to left)
2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5
= 63
And the MSB is "1", which gives a negative sign.
= -63

Using signed magnitude again, if you wanted to represent 63 then that's "00111111". Note that the MSB (left most bit) is now binary 0.

Weird my book tell me to reverse "01000001" and add 1 to it to create the negative number of 65 (according to the book "01000001" is 65).

Maybe I am missing out on something here. Is there a difference to the decimal number 65 "01000001" and the binary one?

Beside that I found this.
Quote:

In the C programming language, you can designate numbers as either signed integers or unsigned integers (they are signed by default if you do not specify otherwise).
• 11-11-2009
zacs7
Quote:

Originally Posted by JOZZY& Wakko
Weird my book tell me to reverse "01000001" and add 1 to it to create the negative number of 65 (according to the book "01000001" is 65).

Maybe I am missing out on something here. Is there a difference to the decimal number 65 "01000001" and the binary one?

Beside that I found this.

That is two's complement representation -- see http://en.wikipedia.org/wiki/Two%27s...27s_complement . Not signed magnitude as discussed above.

Your quote really has nothing to do with any of this, other than you cannot use bitwise operators on signed integers in C. And, unsigned integers are stored as unsigned magnitude in C -- as per the standard.
• 11-11-2009
JOZZY& Wakko
Quote:

Originally Posted by zacs7
That is two's complement representation -- see Two's complement - Wikipedia, the free encyclopedia . Not signed magnitude as discussed above.

Your quote really has nothing to do with any of this, other than you cannot use bitwise operators on signed integers in C. And, unsigned integers are stored as unsigned magnitude in C -- as per the standard.

Thanks you for the link. This stuff kind of confuses me at this moment especially because it is not in my native language so I will try to find some more information on it in Dutch and come back on the subject if I do not understand something after that.
• 11-11-2009
SlyMaelstrom
Quote:

Originally Posted by JOZZY& Wakko
Thanks you for the link. This stuff kind of confuses me at this moment especially because it is not in my native language so I will try to find some more information on it in Dutch and come back on the subject if I do not understand something after that.

Does this not explain it correctly?

Two's complement - Wikipedia (Nederlands)
• 11-11-2009
JOZZY& Wakko
Quote:

Originally Posted by SlyMaelstrom
Does this not explain it correctly?

Two's complement - Wikipedia (Nederlands)

Yes it does but I am getting kind of confused with all the different systems mentioned to do calculations.
• 11-11-2009
MK27
Quote:

Originally Posted by zacs7
you cannot use bitwise operators on signed integers in C.

Of course you can, just remember they are two's compliment numbers.
• 11-11-2009
zacs7
Quote:

Originally Posted by MK27
Of course you can, just remember they are two's compliment numbers.

They don't have to be, that's implementation defined (See ISO 9899 Annex J Portability issues) -- discusses ISO 9899 6.2.6.2 Integer types. Even if the implementation specifies that the signed integers are two's complement. It must also specify what, and how the bitwise ops will work along with padding and signed bits. Even then, there is still some room for undefined behaviour.
• 11-19-2009
nonoob
I think the point of the question was being missed here.

There is no way of telling if a given binary representation is signed or unsigned.

Quote:

Originally Posted by JOZZY& Wakko
Does that mean that if a number is stored in 8-bits for example "01000001" the 7th bit (0 to 7) refers to that it is a positive number?

Then how do you prevent mistakes? For example "10111111" is -65 in binary-2 but it also refers to the number 191 in decimal.

Exactly.
Whether the number is 2s complement, or 1s, or whatever the others have written about above, is not the issue if I understand the question.

The point is it's up to the interpretation that's used. The stored value could be an ASCII character, or some arbitrary flags - neither of which make sense if treated as an integer. All that's required is that the meaning of the data is agreed upon by both the writer/creator and the reader/user.
• 11-19-2009
Mario F.
It's funny you said it nonoob. I've followed the discussion and all the time I felt the same as you. I decided to not say anything because... well, the OP was answering the arguments and no mention was being made that the answers weren't addressing his actual question.

...

To make it more clear (I think):

Just by looking at a series of binary digits there's is no way you can tell what they mean. Binary representation lacks structure and meaning. It's just a bunch of ones and zeros. The actual structure and meaning is given by providing those 1s and 0s with some sort of structure and a meaning.

From nonoobs quote:

01000001 has no meaning whatsoever. It gains a meaning when you read it within a given context. For example

A byte representing an 'ascii' char: 01000001 = 'A'
An integer: 01000001 = 65

Conversely:
10111111 can mean a few different things:

If read into a signed byte: -65
if read into a unsigned int: 191
• 11-20-2009
JOZZY& Wakko
Thanks I was already getting pretty lost on that one until I found a good explanation yesterday in "C by example". Up to that moment I just thought I was going crazy. :D