• Post Reply Bookmark Topic Watch Topic
  • New Topic

bitwise operation question  RSS feed

 
Howard Ting
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I basically understand the rule of bitwise operation, but still have a question.
Can't a byte variable be put into the bitwise operation?
e.g. byte b = 20;
b = b >> 1;
and get a compile error with "possible loss of precision."
Is there any way to make the code work?
Thanks in advance.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The byte value is promoted to integer when doing the shift operation, and when you try to assign the integer value into a byte you get a compile time error.
To solve this, try:
1) b >>= 1 -- Implicit Cast
2) b = (byte) (b >> 1) -- Explicit Cast
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!