File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes int , byte , long ,short , char ,  + shifting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "int , byte , long ,short , char ,  + shifting " Watch "int , byte , long ,short , char ,  + shifting " New topic

int , byte , long ,short , char , + shifting

sonu sharma

Joined: May 17, 2003
Posts: 24
hi all,
i 'm not sure about how signed and unsigned values in the primitives are stored and thus used for shifting operations could some one plz throw some light on this issue.
thanx in advance.
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
byte, short, int, and long's values are signed two's complement integers.
Only char is unsigned.
When a shift operation is performed unary numeric promotion is performed on each operand separately.
If the left-hand operand is a byte, short or char, it is promoted to int. If the left-hand operand is int or long, there is no promotion.

[Blog] [Blogroll] [My Reviews] My Linked In
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Make sure you find some material about how bytes and shorts are treated with the shift operator. Because a byte and short are promoted to an int before the operation, the results you can get from shifting a byte or short can be unexpected.
sonu sharma

Joined: May 17, 2003
Posts: 24
thanx Brian and Valentin i looked thru some material and 'm clearer abt it.
I agree. Here's the link:
subject: int , byte , long ,short , char , + shifting
It's not a secret anymore!