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?
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?
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
[edit]
See Signed number representations - Wikipedia, the free encyclopedia
[/edit]
Last edited by zacs7; 11-11-2009 at 05:39 AM. Reason: link
There is always a signed bit attached with the number it will depict that
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:
Ok that explains more so by requiring a signed bit this mistake can not be made?
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.
Reading "10111111" as signed magnitude gives, (working from right to left)Code:For example "10111111" is -65 in binary-2 but it also refers to the number 191 in decimal.
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.
Last edited by zacs7; 11-11-2009 at 06:15 AM.
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.
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).
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.
Last edited by zacs7; 11-11-2009 at 06:21 AM.
Does this not explain it correctly?
Two's complement - Wikipedia (Nederlands)
Sent from my iPadŽ
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
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.
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.
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.
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
Last edited by Mario F.; 11-19-2009 at 09:49 PM.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
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.