This week's book giveaway is in the Cloud/Virtualization forum.We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Liutauras Vilda
• Knute Snortum
• Bear Bibeault
Sheriffs:
• Devaka Cooray
• Jeanne Boyarsky
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Carey Brown
• salvin francis
Bartenders:
• Tim Holloway
• Piet Souris
• Frits Walraven

# doubt in >>> operator in negative numbers

Ranch Hand
Posts: 3389
Hi Ranchers,

Just got a doubt when working on the >>> operators with negative number. Having read that ">>> operator works the same way as >> operator except it does NOT retain the sign bit and instead it fills it with zeroes".

When i try with an example for int datatype it works. But it does not seem to work with byte.

See this example:

The work out is :

the decimal equivalent of -121 is : 1000 0111
-121 >>> 2 is : 1000 0111 >>> 2 -> 0010 0001 which should be 32+1 = 33.

This should give the output as +33. But it gives the output as "-31".

What am i missing here? It would be great if any one could help here!

Java Cowboy
Posts: 16084
88
Look at this line:

byte y = (byte) (x >>> 2);

What happens here: x is first converted to an int, in which the sign is extended. In binary, the byte to int conversion looks like this:

1000 0111 -> 1111 1111 1111 1111 1111 1111 1000 0111

Now the shift is done on the converted int:

1111 1111 1111 1111 1111 1111 1000 0111 -> 0011 1111 1111 1111 1111 1111 1110 0001

That's converted back to a byte by chopping off bits 31 to 8:

0011 1111 1111 1111 1111 1111 1110 0001 -> 1110 0001

So you end up with 1110 0001, which is -31.

Note that the bit shift operators are only on SCJP 1.4; they were removed from SCJP 5.
[ January 04, 2008: Message edited by: Jesper Young ]

Raghavan Muthu
Ranch Hand
Posts: 3389
Thank you very much Jesper.

But it is NOT the case with other << and >> operators right?

In this case, then how can i deal with the byte values as such without they are converted into int internally?

Jesper de Jong
Java Cowboy
Posts: 16084
88
The << and >> operators work in exactly the same way as the >>> operator. The only difference between >>> and >> is that >>> shifts zeroes in on the left side, while >> copies the leftmost bit of the original value (sign extension). When doing >>> or >> on a byte and then casting the result back to a byte, the result will be the same, because the leftmost bits of the intermediate int are thrown away.

For the exact details, see section 15.19 of the Java Language Specification. According to the specification, the value is first converted to either an int or a long using unary numeric promotion. So I don't think there is a way to shift a byte directly. You could ofcourse use the & (bitwise AND) operator to mask off the top bits after the shift:

byte y = (byte) ((x >>> 2) & 0x3f);
[ January 04, 2008: Message edited by: Jesper Young ]

Raghavan Muthu
Ranch Hand
Posts: 3389
Thank you Jesper for a nice explanation

author
Posts: 8998
19
Hey Ranchers!

Remember this topic is only on the old, old 1.4 exam!

Raghavan Muthu
Ranch Hand
Posts: 3389
Thats fine Bert. Thank you

 They weren't very bright, but they were very, very big. Ad contrast: Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java