Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sonu sharma
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx Brian and Valentin i looked thru some material and 'm clearer abt it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic