Popular Posts

Friday, July 22, 2011

LeftShift fiddle in java

int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.
     When we shift a variable beyond it's width,then output becomes dependent on the compiler,so it is undefined behavior.
public class BitFiddle {
public static void main(String[] args) {
Integer i = 3;
i = i << 31;
System.out.println(i);// -2147483648
System.out.println(Integer.toBinaryString(i));//10000000000000000000000000000000


int j = 3;
j = j << 32;
System.out.println(j);// 3
System.out.println(Integer.toBinaryString(j));//11

int k = 3;
k = k << 33;
System.out.println(k);// 6
System.out.println(Integer.toBinaryString(k));//110
}
}

No comments:

Post a Comment