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.
posted 13 years ago
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
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!