understanding 3>>2 and -3>>2 results in Java -
when running code:
public class operatedemo18{ public static void main(string args[]){ int x = 3 ; // 00000000 00000000 00000000 00000011 int y = -3 ; // 11111111 11111111 11111111 11111101 system.out.println(x>>2) ; system.out.println(y>>2) ; } }; i output as:
x>>2 0 y>>2 -1 as understanding, since int x = 3, x>>2 equal (3/2)/2 0.75, integer, x>>2 0.
but don't understand why int y = -3, y>>2 -1. please explain it?
as understanding, since int x = 3, x>>2 equal (3/2)/2 0.75, integer, x>>2 0.
that's not entirely true; >> bitshift operation, nothing else. effect on positive integers division powers of two, yes. unsigned integers, it's not:
you conveniently supplied binary form of y == -3 yourself:
11111111 11111111 11111111 11111101 let's bitshift right two!
y == 11111111 11111111 11111111 11111101 y>>2== xx111111 11111111 11111111 11111111 now, fill in x?
java, reasonable languages, sign-extends, ie. uses original highest (leftmost) bit:
y == 11111111 11111111 11111111 11111101 y>>2== 11111111 11111111 11111111 11111111 it isn't hard see "biggest" negative integer (remember, negative integers represented "two's complement"!), i.e. -1.
Comments
Post a Comment