Reputation:
I know that we can represent binary Numbers in several ways, but I am really not sure how to distinguish a positive binary number from a negative one.
If we have the number +13
, then its binary representation looks like this:
1101
and its negative representation looks like this:
11101
What I understand is that if you need to distinguish them, then the presence of 0
is important in number +13
:
01101
Even so, I still cannot distinguish between:
11101 ///Here is the representation of -13
and:
11101 ///Here is the representation of +29
I understand that here is used another scheme called "two's complement" which I need to apply it.
How can I distinguish those two binary representations?
Upvotes: 1
Views: 2400
Reputation: 649
Let's say you have 5 bit computer. 5 bits divided into 1 sign bit + 4 number bits. Limits are 2^4-1 to -2^4 i.e. 15 to -16. 13=01101 -13= 10010 : 2's complement representation. It won't be 18 because 18 can't fit in the range.
Upvotes: -1
Reputation: 123448
The same sequence of bits can have radically different meanings based on context.
Integer types have a fixed number of bits - the C language definition mandates that the signed int
type must be able to represent at least the range [-32,767...32,767]
, meaning an int
must be at least 16 bits wide1.
There are several different ways to represent signed integers. The most common is 2's complement, but some architectures may use 1's complement or sign-magnitude.
To flip the sign on a 2's complement number, do a bitwise negation and add 1 (this example assumes 8 bit integers):
00001101 == 13
11110010 + 1 == 11110011 == -13
11110011 == -13
00001100 + 1 == 00001101 == 13
One of the chief advantages of 2's complement is that you have a single representation for 0, and it gives you a slightly wider range of values - [-2n-1..2n-1-1]
To flip the sign on a 1's complement number, you simply do a bitwise negation:
00001101 == 13
11110010 == -13
11110010 == -13
00001101 == 13
With 1's complement, you have positive and negative representations for 0 - 00000000
and 11111111
- and the range is [-2n-1-1..2n-1-1]
With sign-magnitude, you leave the value bits as-is and flip the sign bit:
00001101 == 13
10001101 == -13
Like 1's complement, you get two encodings for positive and negative 0 - 00000000
and 10000000
.
Unsigned integer types have the same width as their signed counterparts, and their range is [0..2n-1]
.
So, the bit sequence 11110011
can mean -13 in 2's complement, -12 in 1's complement, -115 in sign-magnitude, or 243 unsigned.
Upvotes: 1
Reputation: 153338
The binary value is open to the encoding.
1101
1101
2 has the value of 1310 when there is no encoded sign bit.
When a sign-bit is used, the width N
is also important.
When N > 4
,
using 2's complement, 1s' complement, sign magnitude, the value is still 1310.
11101
If 11101
2 was saved in unsigned
field of size 5 or more, the value is 2910.
If 11101
2 was saved in a 5-bit signed int
field, the value is depends on the int
encoding (which is certainly 2's complement).
2's complement: -310
1s' complement: -210
Sign magnitude: -1310 (Apparently OP's initial view)
Upvotes: 0
Reputation: 32586
As the other guys say the question can I distinguish those two binary representations as no sense. A sequence of bits is neutral, when you transform it in a number you do the transformation for a given representation. It can represent an int, an unsigned int, a float or anything you want. It is like if I ask you what is a coin, this word exists (at least ?) in English and in French with a totally different meaning (the french word coin means corner in English), to answer me you need to choose a language, you cannot answer without.
About the "two's complement", it is the way compatible for the standard representation used in our CPU to change the sign of signed int. The first complement is to replace all 0 by 1 and all 1 by 0, the second complement is to add 1 to the previous result.
Supposing the word has 5 bits, the int value 13 is 01101 in binary. If we want -13 the first complement on 13 gives 10010, adding 1 that gives 10011.
But still having 5 bits words, 10011 for an unsigned int correspond to the value 19.
For an int on 5 bits the lower negative number is by definition 10000, if we try to change its sign : first complement = 01111, adding 1 = 10000 ! In fact there is an overflow, 5 bits are not enough.
Upvotes: 3