This week's book giveaway is in the Cloud forum.We're giving away four copies of Terraform in Action and have Scott Winkler on-line!See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Rob Spoor
• Bear Bibeault
Saloon Keepers:
• Jesse Silverman
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Al Hobbs
• salvin francis

# Left shift problem

Ranch Hand
Posts: 149
• Number of slices to send:
Optional 'thank-you' note:
Hi,
Can anybody tell me how can i get the answer

byte b=12;
b <<= 5; //the answer must be -128.
i know the meaning of b <<=5 i.e b=(byte) b<<5, but how will compute on the paper in the exam.
One more question.
Do we have any easy way to calculate >>> operation result , as we have with >> and <<.

Pls help

------------------

Ranch Hand
Posts: 1070
• Number of slices to send:
Optional 'thank-you' note:
Do it in steps.
First take 12 and make it binary: 0000 1100
Second make it an int, since shifts cast to ints (for this problem, this doesn't matter, but does for >>>): 0000 0000 0000 1100
Third, do the shift: 0000 0001 1000 0000
Fourth, cast it back to a byte: 1000 0000
Fifth, convert it back to decimal, since the highest order bit determines the sign for bytes, this is -128.
If you need help converting decimals to binary and vice versa, check out this link. Great explanation here. http://webhome.idirect.com/~jgriscti/oper/binhex.html

As for you second question, yes you do need to know about >>>
Bill

Ranch Hand
Posts: 149
• Number of slices to send:
Optional 'thank-you' note:
Thanks Bill.

Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
Bill,
Thanks for an excellent explanation. Would be helpful if you cud explain the >>> operation too. I am having some trouble understanding the same.
~Kaushik

bill bozeman
Ranch Hand
Posts: 1070
• Number of slices to send:
Optional 'thank-you' note:
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).]

bill bozeman
Ranch Hand
Posts: 1070
• Number of slices to send:
Optional 'thank-you' note: