wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shift operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift operator" Watch "Shift operator" New topic
Author

Shift operator

shaan nimaz
Greenhorn

Joined: May 27, 2006
Posts: 5
Shift operator
hi everybody,
just try 2 compile this.

class A{
public static void main(String [] args){

byte x,y,z;
x=2<<5;
System.out.println(x);
}
}

when you run the above code x will return 64. if 2<<10 then it will not run b'cos of compile time error(possible loss of precision). then i 2<<32. but this time it runs perfectly & return 2. so can anyone know why dis happen?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I believe the issue here is a narrowing primitive conversion as part of an assignment conversion.

2 << 5 is known at compile-time, and will fit into a byte.

2 << 10 is known at compile-time, and will not fit into a byte.

The second operand to the shift operator will be reduced modulo 32 before the shift is carried out. So 2 << 32 is converted to 2 << 0 before the shift is carried out.
[ August 13, 2006: Message edited by: Keith Lynn ]
shaan nimaz
Greenhorn

Joined: May 27, 2006
Posts: 5
thankx buddy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shift operator