The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes need help Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "need help" Watch "need help" New topic
Author

need help

Rahul Pawar
Ranch Hand

Joined: Mar 13, 2001
Posts: 38
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
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
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

Rahul Pawar
Ranch Hand

Joined: Mar 13, 2001
Posts: 38
thanks mafalda
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help