Hello
I am very confused with how negative numbers are represented in binary form.
They say it is done in 2s complement form.
Now won't this 2s complement of a negative number clash with an actual +ve number having the same binary representation?
And negation of a +ve number.....
We must 1st negate the bits individually and then the left most bit is going to be 1(mostly).
Now isn't that how -ve numbers are represented?

So if the negated +ve number is a -ve number should I now find its 2s complement and say that is how it is represented or should I just just leave it like that?Why so?

Also please provide me with links if you any that deal with bitwise opeartors on both +ve and -ve numbers.

Thank You
Please help me.

They say it is done in 2s complement form.

There are several methods of representing signed values in binary. Two's complement is one of the more common on PCs. Others include one's complement and sign magnitude.

Now won't this 2s complement of a negative number clash with an actual +ve number having the same binary representation?

No. Two's complement uses the most significant bit as a sign bit, which effectively cuts the positive range in half. If the sign bit is set, the value is negative, otherwise it's positive. This guarantees that all numbers in the range will have a unique binary representation.

you can check the limits for your system in limits.h
To get through your confusions, try to put those limits in binary form and you will yourself find that they are not clashing..

Okay.Thank You.
So we cut the +ve range in half and use that for negative number representation.
I have confusion with bitwise shifting and negation.
Let us consider a negative number -5.In binary 2s complement it is,11111011.Right?
So say

int a=-5;
int b=~a;

Now b must become 00000100.And that is 4.Right?
Now,a positive number like 6.

int a=6;
int b=~a;

6 is 00000110.
so b should become 11111001.
Now,the sign bit is 1.So b is negative.
And now,is this 11111001,the 2s complement of some number we have to find?
2s complement of that is -7.Is that how it works?

And the shift operators,

int a=-5;
int b=a<<2;

-5 in 2s complement form is,11111011.
After the left shift,it must become,11101100.
That is a -ve number in 2s complement form.
what if b were a<<5.
Then it must become 01100000.Isn't that a +ve number?Each left shift is supposed to multiply the number by 2.But that isn't happening here!
And with right shift,

int a=-5;
int b=a>>2;

should b become 00111110 or 10111110 or 11111110(adding 1s to the left)?

Please help me.
I hope I haven't made errors in binary representation.I am really sorry if I have.
Thank You

Then it must become 01100000.Isn't that a +ve number?Each left shift is supposed to multiply the number by 2.But that isn't happening here!

No, that's a positive number. Don't confuse bit shifting with multiplication, especially when it comes to negative quantities.

should b become 00111110 or 10111110 or 11111110(adding 1s to the left)?

That depends on whether the left hand side of the expression is signed or unsigned. If it's signed, the vacated bits are filled with the sign bit. Otherwise, they're filled with 0.

No, that's a positive number. Don't confuse bit shifting with multiplication, especially when it comes to negative quantities.


That depends on whether the left hand side of the expression is signed or unsigned. If it's signed, the vacated bits are filled with the sign bit. Otherwise, they're filled with 0.

Actually sign bit of positive number is zero, so more simply:

vacated bits are filled with the sign bit, which is 0 in case of unsigned numbers.

Of course being unsigned means that the sign bit is not physically there, but implied by the type.

Thank You Narue and pyTony!

Actually sign bit of positive number is zero, so more simply:

vacated bits are filled with the sign bit, which is 0 in case of unsigned numbers.

Note that the part of my post which you quoted refers to one of the OP's left shift examples. The arithmetic shift on signed and logical shift on unsigned rule only applies to a right shift in C.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.