Help coderanch get a
new server
by contributing to the fundraiser
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:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# shift operator on byte

Ranch Hand
Posts: 32
• Number of slices to send:
Optional 'thank-you' note:
hi all,
there is a small piece of code
byte b = -4;
b=(byte)(b>>>2)
i fail to undestand that can shift operators be applied on byte type and how the output will come???

srijan

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

Originally posted by srijan sharma:
hi all,
there is a small piece of code
byte b = -4;
b=(byte)(b>>>2)
i fail to undestand that can shift operators be applied on byte type and how the output will come???

srijan

Hello Srijan,
Let see if I can make sense of this. Here what we all learn:
<< - means shifting left, filling with zero from the right.<br /> >> - means shifting right, propagating the sign bit from the left.
>>> - means shifting right, filling with zero from the left.
With that, we expect a >>> unsigned-right-shift operator would change negative sign into positive. And -4 >>> 2 would be equal to 1.
However if -4 is a value of a byte or a short, the answer would be -1 ... What happens? The sign is still negative!
Here is what I think happens, please correct me if I am wrong. First, b will be widen to an int. Therefore -4 has the following form:
11111111111111111111111111111100
Now b>>>2 will yield this
00111111111111111111111111111111
Then you cast it into a byte, so this is what you get
11111111
And that is -1

Now try this code:
short s = -4;
s >>>= 2;
Well, again the answer is -1 because if you take the last 16 bits of the following pattern
00111111111111111111111111111111
you will get
1111111111111111
and that is a -1 too.
However with this code:
int i = -4;
i >>>= 2;
you will get 1073741823
and that is 00111111111111111111111111111111
Can you guess the answers of the following code for b,s, and i?
byte b = -4;
short s = -4;
int i = -4;
b >>>= 30; s >>>= 30; i >>>= 30;
Your guesses are right; They all yield 3. Well they all change sign and that is the magic.
What do you think? Does that explain it?
Lam

[This message has been edited by Lam Thai (edited April 20, 2001).]

srijan sharma
Ranch Hand
Posts: 32
• Number of slices to send:
Optional 'thank-you' note:
hi
thanks for the reply. but is that true that the exam would be having a question on shift operators or byte operators.
also do we need to do and ,or xor operation for the exam.
i am planning to appear for the exam in july
regards
srijan

Ranch Hand
Posts: 61
• Number of slices to send:
Optional 'thank-you' note:
hi
as lam thai said is partly correct,
i.e widening to integer is correct one.
in general a -ve number is stored in 2's complement form and the sign bit is represented as one. which is the most significant bit. in our case the the 2's comp of -4 is represented as 100 and with msb with 1. it means the 32 bit is the msb which is one. as u know with unsigned right shift zeros are filled from the left side as bits are shifted to right side. so the -4>>>2 will gets u 1*2 to the power of 30. which gives the result for you.
thankyou
srinivas
------------------

 You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad? We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser