Sure, let's look at 3 different examples.

First lets take the easiest one:

Taking it in steps,

1. convert i=57 to binary: 00000000 00000000 00000000 00111001

2. perform the right unsigned shift: 0000000 00000000 00000000 00000111

3. convert it back to decimal: 2^0 + 2^1 + 2^2 = 1+2+4 = 7

Now let's look at one that is slightly harder:

1. convert -57 to binary. This times it is a bit harder. The easiest thing I have seen is to take maximum unsigned number an int can hold and subtract the number you want. That will be the number you need to convert into binary, with the highest order bit being 1 to represent a negaive.

So for an int, the range is 0 to (2^32) - 1 or 0 to 4,294,967,295

4,294,967,296 - 57 = 4,294,967,239 is the number we want in binary (which I will call x here).

So take

Take all of those remainders and put them in reverse order: 11111111 11111111 11111111 11000111 and there is your bit

pattern.

2. Do your unsigned shift: 00011111 11111111 11111111 11111000

3. Convert it back to decimal

Add everything up = 536,870,904 which is your decimal value of -57>>>3;

Now let's look at the trickiest one:

1. Convert it to binary: 11000111

2. Since shifts are only applied to int or longs, convert the byte to an int. All you have to do here is find the highest order bit (1 in our example) and add that for all the new bits: 1111111 11111111 11111111 11000111 (just like in our other example)

3. Do your unsigned shift: 00011111 1111111 11111111 11111000 (just like in our other example)

4. But since this is a byte, and >>>= has an implicit cast back to byte, you have to cast it back which gives you 11111000

5. Convert that to decimal (following the steps above) and you get 248.

6. You know this is a negative because of the highest order bit, so take that maximum for a byte, 256, and take away 248 from it or 248 - 256 = -8

Which means byte -57 >>> 3 = -8

Hope that helps you some.

[This message has been edited by bill bozeman (edited November 28, 2000).]