Today I was learning about the left shift bit operator (<<
). As I understand it the left shift bit operator moves bits to the left as specified. And also I know multiply by 2 for shifting. But I am confused, like what exactly is the meaning of "shifting bits" and why does the output differ when value is assigned with a different type?
When I call the function below, it gives output as System.out.println("b="+b); //Output: 0
And my question is: how does b become 0 and why is b typecasted?
public void leftshiftDemo()
{
byte a=64,b;
int i;
i=a << 2;
b=(byte)(a<<2);
System.out.println("i="+i); //Output: 256 i.e 64*2^2
System.out.println("b="+b); //Output: 0 how & why b is typecasted
}
Update (new doubt):
what does it mean "If you shift a 1 bit into high-order position (Bit 31 or 63), the value will become negative". eg.
public void leftshifHighOrder()
{
int i;
int num=0xFFFFFFE;
for(i=0;i<4;i++)
{
num=num<<1;
System.out.println(num);
/*
* Output:
* 536870908
* 1073741816
* 2147483632
* -32 //how this is -ve?
*/
}
}
When integers are casted to bytes in Java, only the lowest order bits are kept:
In this case the byte
64
has the following binary representation:The shift operator promotes the value to int:
then left shifts it by 2 bits:
We then cast it back into a byte, so we discard all but the last 8 bits:
Thus the final byte value is
0
. However, your integer keeps all the bits, so its final value is indeed256
.In java, ints are signed. To represent that, the 2's complement is used. In this representation, any number that has its high-order bit set to 1 is negative (by definition).
Therefore, when you left-shift a 1 that is on the 31st bit (that is the one before last for an int), it becomes negative.
in memory:
b = (byte)(a << 2);
in memory:
You should read about different data types and their ranges in Java.
Let me explain in easy terms.
'byte' in Java is signed 2's complement integer. It can store values from -128 to 127 both inclusive. When you do this,
you are left shifting 'a' by 2 bits and the value is supposed to be 64*2*2 = 256. 'i' is of type 'int' and 'int' in Java can represent that value.
When you again left shift and typecast,
you keep your lower 8 bits and hence the value is 0.
You can read this for different primitive types in Java. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
The exact meaning of shifting bits is exactly what it sounds like. :-) You shift them to the left.