# Thread: bitwise shift operators in -ve numbers

1. ## bitwise shift operators in -ve numbers

hi,
Can anyone explain.....how the compiler computes the output for this ????
insert
Code:
```int main()
{
printf("%x\n",-1>>4);
}```
according to me....-1 is represented as

1000 0000 0000 0000 0000 0000 0000 0001
High order bits lower order bits

I am using microsoft VC++ compiler so int occupies 4 bytes.

so when u right shift by 4 it should display 0.

But it displays ffff....

Can anyone explain how.....????

Regards,
Rohit

2. according to me....-1 is represented as

1000 0000 0000 0000 0000 0000 0000 0001
High order bits lower order bits
Your assumption is false. In C, either a one's or two's complement representation is used (not sign and magnitude, if I remember correctly), so the 32 bit representation of -1 in binary would either be:
1111 1111 1111 1111 1111 1111 1111 1110
or:
1111 1111 1111 1111 1111 1111 1111 1111

EDIT:
Oh, I did not remember correctly: sign and magnitude representation is allowed, but your implementation most likely uses two's complement anyway.

3. You have the negative numbers back to front. A negative number is indeed beginning with 1, but the "formula" for createing a negative number is "invert all bits, then add one" [1], which makes -1 the same as "all ones".

[1] This is ONE of several techniques, called "two's complement", which is by far the most common method. It makes addition and subtraction of negative numbers fairly easy.

--
Mats

4. Originally Posted by laserlight
Your assumption is false. In C, either a one's or two's complement representation is used <snip}
Is that actually defined in the C standard, or just "by convention C uses ...". I personally haven't ever used a machine that uses one's complement, never mind any other variation of negative numbers.

--
Mats

5. Is that actually defined in the C standard, or just "by convention C uses ...". I personally haven't ever used a machine that uses one's complement, never mind any other variation of negative numbers.
It is defined, or rather, the options for representing signed integers are enumerated.

6. hi,
Thanks.But i still don't understand why i get the answer 256 for this program...
insert
Code:
```int main()
{
unsigned char i=0x80;
printf("&#37;d",i<<1);
}```
the decimal equivalent of 0x80 is 128...its bit representation is 1000 0000

when this is left shifted by 1 all the bits will become zero 0000 0000

But i get the output 256....

Can you please explain me how????

7. That is because i << 1 when sent to printf is turned into an int. If you do "i <<= 1" or some other operation where you actually STORE the value as a char.

--
Mats

8. Admittedly, I tend to get confused as well on such things but my guess is that i<<1 returns an int, so indeed 128<<1 gives 256.

9. Originally Posted by laserlight
Admittedly, I tend to get confused as well on such things but my guess is that i<<1 returns an int, so indeed 128<<1 gives 256.
Yes, my first post doesn't explain very well - the expression is "promoted" to integer, because one side is int (1) and the other unsigned char, so the unsigned char is extended to the corresponding unsigned int before the shift is done. Since printf takes any integer type value (less than long) as int, it does never convert back to char.

--
Mats

10. hi,
Thanks great explanation.

Regards,
Rohit

11. >> on negative numbers is implementation-defined.
Sometimes, you get zeros.
Other times, you get the sign bit.

Only one of them is arithmetically equivalent to divide by 2.

12. this is happening because of type promotion in C,
before the shift operation it will convert it to unsigned int than afterword calculation it scale down it in the unsigned or signed char depends on previous nature of object.
because we are using %d is a type specifier so it will print unsigned int value of the operation.