hi friends , can anybody will explain me how shift operators works with byte and short data type . thanks in advance rahul
vkswamy venkatachalam
Greenhorn
Joined: Feb 18, 2001
Posts: 21
posted
0
Originally posted by Rahul Pawar: hi friends , can anybody will explain me how shift operators works with byte and short data type . thanks in advance rahul
Hi,Ragul, The byte type and short are promoted to int type and shift take place. regards vkswami
vkswami
Mafalda Alabort
Greenhorn
Joined: Mar 02, 2001
Posts: 24
posted
0
Hi Rahul, I think they work the same way as with int or long. The only difference is that the byte or short that you are shifting will get first converted to an int (arithmetic promotion of operands) and then the shifting will occur. So when you cast the result back to the original type you may get strange results.
public class Q1{ public static void main(String [] fred){ byte b = -1; System.out.println("byte b is " + b); int i = b >>> 2; System.out.println("int a is " + i); byte b2 = (byte)(b >>> 2); System.out.println("byte b2 is " + b2); } } b: 1111 1111 a: 0011 1111 1111 1111 1111 1111 1111 1111 1111 b2: 1111 1111 and not 0011 1111 So the output is: byte b is -1 int a is 1073741823 byte b2 is -1 I hope this helps