File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# doubt in >>> operator in negative numbers

Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

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!

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15094

27

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

Joined: Apr 20, 2006
Posts: 3381

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

Joined: Aug 16, 2005
Posts: 15094

27

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

Joined: Apr 20, 2006
Posts: 3381

Thank you Jesper for a nice explanation
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8898

5
Hey Ranchers!

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

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Thats fine Bert. Thank you

I agree. Here's the link: http://aspose.com/file-tools

subject: doubt in >>> operator in negative numbers