Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
why right bit shifting not producing negative number!

C#
Console.WriteLine(1 << 2 );//result is 4-true i understand;
Console.WriteLine(4 >> 4);//result is 0-why??????????????
Posted

Simple. Just look at the values in binary:
1 ==   00000001
2 ==   00000010
3 ==   00000011
4 ==   00000100
5 ==   00000101
When you use a shift operator, you move the binary value "n" bits to the left or right.
So "1" left shifted 2 places:
0001  becomes 0100
   1     "       8

And "4" right shifted 4 places:
0100 Becomes 0000
   4    "       0
because all the "1" bits are shifted out of teh value and discarded.
A "shift" is just that - it does not "wrap" bits back to teh top or bottom of the value.
 
Share this answer
 
Comments
Muhamad Faizan Khan 16-Jul-15 4:55am    
you mean there is no negative bits exists. so what is the way to represent negative value using these bits??
OriginalGriff 16-Jul-15 5:13am    
Negative numbers are a different subject: there is no "negative bit" per se, but the top bit can be interpreted as indicating a negative number, depending on teh operation being performed.

A right shift can produce a negative number, but only if the number started out negative - when you shift the top bit value is propagated down the value. A left shift can produce a negative number, but only if the shift results in the top bit being set to one afterwards.

If we pretend that our values are only four bits, then you have 16 possible values:
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 -8 or -0 depending. Don't ask - this is complicated! :laugh:
1001 -7
1010 -6
1011 -5
1100 -4
1101 -3
1110 -2
1111 -1

This works the same way for 16/32/64/128 bit numbers (but takes a lot more typing)
When the top bit is set, it's a negative number (unless it's an unsigned value)

Does that make sense?
Muhamad Faizan Khan 16-Jul-15 5:23am    
i am managing it by keeping remember negative representation in bits understanding is not easy and that start from top bits.
any ways thanks
OriginalGriff 16-Jul-15 5:35am    
You're welcome!

If in doubt, try "playing" with it on paper using four bit numbers - it's easier to visualize what is happening and then mentally extend it to 32 bit number or more.
This is particularly true when you start to meet bitwise OR, AND, XOR and so forth.
That's the way right shift works, namely (from MSDN[^]):

Shift right:
int operator >>(int x, int count);
uint operator >>(uint x, int count);
long operator >>(long x, int count);
ulong operator >>(ulong x, int count);
The >> operator shifts x right by a number of bits computed as described below.
When x is of type int or long, the low-order bits of x are discarded, the remaining bits are shifted right, and the high-order empty bit positions are set to zero if x is non-negative and set to one if x is negative.


So, decimal 4, that is (binary) 100b, becomes 0:
C++
100b >> 1 = 010b
100b >> 2 = 001b
100b >> 3 = 000b
100b >> 4 = 000b
...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900