1

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?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    you cannot, unless you know what the exact type used in the program. note that `11011` is probably not negative, since the `1` is the most significant bit of the size of the type used. There's ambiguity for -1 and 255 for instance because it can be written `11111111` – Jean-François Fabre Jan 08 '19 at 14:11
  • 2
    it depends on how it is used, so for instance what machine instructions are used to process it. Is `11011` an unsigned number, a twos complement number or a sign/magnitude number? Just going by the fact that its binary representation is `11011`, you can't know. Heck, it might not even represent a number, but something entirely different. – Blaze Jan 08 '19 at 14:13
  • Have a look at this related post: https://stackoverflow.com/questions/2931630/how-are-negative-numbers-represented-in-32-bit-signed-integer – Jonny Schubert Jan 08 '19 at 14:18
  • `11011` would mean "signed magnitude", which is one way (uncommon) to represent a negative sign. Other ways are two's complement (most common) and one's complement (uncommon). To make things worse, C allows all three forms. But real-world computers almost exclusively use two's complement. – Lundin Jan 08 '19 at 14:20
  • 2
    `1011` binary is 11 decimal, not 13. – chux - Reinstate Monica Jan 08 '19 at 14:22
  • @Lundin So this means that if on my System the size of `int` is `4` and there is the number `13`, then its negative representation would be `10000000 00000000 00000000 00001101` because left bit it is set to `1`. Am I right ? –  Jan 08 '19 at 14:23
  • @Chux You right. Corected :d –  Jan 08 '19 at 14:24
  • @MichaelB. If you have somehow found an exotic computer which uses signed magnitude, then yes. An x86 PC would use 2's complement: `11111111 11111111 11111111 11110011`. Which happens to be the bitwise inverse + 1. – Lundin Jan 08 '19 at 14:27
  • @chux I tought that the signed `11111111.11111111.11111111.11111111` means `-1` in 2's Complement and `10000000 00000000 00000000 00001101` means `-13`. –  Jan 08 '19 at 14:30
  • @MichaelB. `10000000 00000000 00000000 00001101` as a binary number is 2,147,483,661. As a 32-bit 2's complement binary number, it is -2,147,483,635 – chux - Reinstate Monica Jan 08 '19 at 18:18

4 Answers4

3

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.

bruno
  • 32,421
  • 7
  • 25
  • 37
1

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.


  1. Note that some architectures may use padding bits, such that it takes more than 16 bits to represent 32,767.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Nice answer. MIsc: Its `1s` _ones’ complement_ not `1's`, but `2's` is OK for _two’s complement_. The `n values bits` of `unsigned` could be one less than `n` of `int` due to padding, yet I doubt a system will every try that again. – chux - Reinstate Monica Jan 08 '19 at 21:55
0

The binary value is open to the encoding.

1101

11012 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 111012 was saved in unsigned field of size 5 or more, the value is 2910.

If 111012 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)

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1

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.

Ben Tennyson
  • 637
  • 6
  • 18