Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in >>> operator in negative numbers

 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Thanks in advance!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jesper for a nice explanation
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ranchers!

Remember this topic is only on the old, old 1.4 exam!
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats fine Bert. Thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic