23

Please, help me to understand binary presentation of negative integers.

For example we have 5. Binary presentation of 5 is 00000000.00000000.00000000.00000101.

And as I understand binary presentation of -5 should be like 10000000.00000000.00000000.00000101.

But output is 11111111.11111111.11111111.11111011.

I have 2 question:

1) Why here is so much 1 bits.

2) What I really cant understand it last 3 bits 011. It looks like 3. Even +1 or -1 it'll be 100 or 010

Thanks

1

6 Answers 6

53

Your understanding of what those negative numbers should look like is flawed. Java uses two's complement for negative numbers and the basic rule is to take the positive, invert all bits then add one. That gets you the negative.

Hence five is, as you state:

0000...00000101

Inverting that gives you:

1111...11111010

Then adding one gives:

1111...11111011

The bit pattern you have shown for -5 is what's called sign/magnitude, where you negate a number simply by flipping the leftmost bit. That's allowed in C implementations as one of the three possibilities(a), but Java uses two's complement only (for its negative integers).


(a) But keep in mind there are current efforts in both C and C++ to remove the other two encoding types and allow only two's complement.

Sign up to request clarification or add additional context in comments.

Comments

7

And as I understand binary presentation of -5 should be like 10000000.00000000.00000000.00000101.

That would be right if Java used a Sign and Magnitude representation for integers. However, Java uses Two's Complement representation, so the rest of the bits are changed in accordance with the rules of that representation.

The idea behind two's complement representation is that when you add a number in such representation to another value dropping the extra bit on the most significant end, the result would be as if you subtracted a positive number of the same magnitude.

You can illustrate this with decimal numbers. In a two-digit representation, the value of 99 would behave like -1, 98 would be like -2, 97 like -3, and so on. For example, if you drop the top digit in 23 + 99 = [1]22, so 99 behaved like -1. 23 + 98 = [1]21, so 98 behaved like -2.

This works the same way with two's complement representation of binary numbers, except you would drop the extra bit at the top.

Comments

4

Here is an example for 2's compliment:

If you have -30, and want to represent it in 2's complement, you take the binary representation of 30:

0000 0000 0000 0000 0000 0000 0001 1110

Invert the digits.

1111 1111 1111 1111 1111 1111 1110 0001

And add one.

1111 1111 1111 1111 1111 1111 1110 0010

Converted back into hex, this is 0xFFFFFFE2. And indeed, suppose you have this code:

#include <stdio.h>

int main() {
    int myInt;
    myInt = 0xFFFFFFE2;
    printf("%d\n",myInt);

    return 0;
}

That should yield an output of -30. Try it out if you like.

Comments

3

http://en.wikipedia.org/wiki/Two%27s_complement

The way negative numbers are stored is that the most significant bit (e.g. the bit representing 2^31 for a 32 bit number) is regarded as negative. So if you stored all 1s, you would add up

(-2^31) + 2^30 + 2^29 + ... + 2^1 + 2^0

which makes -1.

Small negative numbers will be mostly ones under this representation.

Comments

1

With two's complement it's true that a MSB of 1 indicates a negative number. But the remaining bits are not the binary representation of its value then.

On the other hand, if the MSB is 0 the remaining bits represent the binary value. But it cannot be said that the number is positive then. Zero is neither positive nor negative.

This picture, a number circle, helped me to understand the principle when I started to learn that there are more representations of numbers than with 0..9:

           0
    -1    000    1
       111   001  
-2  110         010  2
       101   011
    -3    100    3
          -4

Or as linked above:

Comments

0

https://stackoverflow.com/a/26315812/16313677 this explains how to get a two's complement. But I am writing this answer to give an intuitive insight to two's complement. How it represents negative of a number.

  1. Lets assume we are working with 32 bit integers and the first bit ( that is the most significant bit) has a direction of -1 instead of usual one. What do I mean that. If you are given binary number 101 it means (+1)*(2^2) + (0)*(2^1) + (+1)*(2^0). However if the most significant bit is set, it represents (-1)*(2^31)
  2. lets x be our original positive number. Lets assume we flip all its bits except the most significant bit. We get a number y. Now x + y = 2^31 - 1
  3. By rearranging we get -x = -(2^31) + y + 1

Now look carefully at the RHS of the last equation. We will get -(2^31) through the most significant bit. y is received by flipping all the bits. And we add 1. This is exactly how we do two's complement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.