can someone tell me why the following code is throwing the compile error "cannot convert from int to byte"?
byte x = 2;
byte y = (x >> 1);
I mean I clearly declared both x and y as bytes, and 'x >> 1' will evaluate to 1 i.e. still be in the range of a byte.
Also when I do something like
byte x = -2;
System.out.println(x >>> 1);
I would expect 126 to be the outcome, because I shifted a 0 in the leftmost bit of b1111 1101, which is 0111 1110. But the console is printing '2147483647', so it looks like my byte has been converted to an integer before the 0 has been shifted in. Why is that? Please help me out.
x >> 1isint. Why? Because the language specification says so.