There is a good explanation of bit shift operators on Marcus Green's site at
http://www.jchq.net/tutorial/BitShift.htm When I was trying to figure this out, I always set things up for myself as follows:
Bits are set up on base 2.
32>>1(answer is 16)
keep in mind that the 32 will be promoted to an int before evaluating the expression - I'm only putting in enough bits to show where the 32 is coming from:
32 16 8 4 2 1
------------------------
32 in in bits: 1 0 0 0 0 0
right shift 1: 0 1 0 0 0 0 = 16
2<<32(answer is 2)<br /> 2 will be promoted to an int before evaluating the expression. Marcus has the accurate explanation about how shifting uses the modulo operator - but I just think of it as "wrapping around." Left shifting by 32 inserts 32 0s to the right of it - for a total of 64 bits. However, an int can only hold 32 bits, so it will take the most significant 32 bits - which is exactly the bit pattern you started with.<br /> -32>>>1(answer is 16)
Actually - when I run this I get a super long positive number. Negatives are represented differently than positives. When you do an unsigned right shift by one - it inserts a 0 in the beginning of the number, changing it to a positive number. Because it used to be a negative (with alot of ones because of the way negatives are stored in bits), the number becomes huge. Marcus has a detailed explanation for this.
Hope this helps.