aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg Operators" Watch "Reg Operators" New topic
Author

Reg Operators

Vishnu Munnangi
Ranch Hand

Joined: Sep 28, 2004
Posts: 114
Hi Everyone,

Please go through the code...
class A1{
public static void main(String args[]){
System.out.println(1<<31);
}
}
After compiling and executing this class A1, I am getting a value
= -2147483648.

class A2{
public static void main(String args[]){
System.out.println(2<<31);
}
}
After compiling and executing this class A2, I am getting a value = 0.
oddNumbervalue << 31 = -2147483648.
evenNumberValue <<31 = 0.
when I am placing odd numbers it is giving me minimum value of integer
and when i am placing even numbers it is giving me value =0.
Can any one explain me why this is happening

Thanks in advance.
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76

when I am placing odd numbers it is giving me minimum value of integer
and when i am placing even numbers it is giving me value =0.


1 << 31

00000000000000000000000000000001
shift that little one on the end to the left and ya get

10000000000000000000000000000000


2 << 31
00000000000000000000000000000010
shift that little 1 to the left 31 times and ya get

00000000000000000000000000000000


odd-ness and even-ness has nothing to do with it.
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
odd-ness and even-ness has nothing to do with it.


Odd-ness and even-ness has something to do with it. In Java, Any odd number int left shifted 31 bits will have bit pattern 10000000000000000000000000000000, which evaluates to -2147483648, and any even number int left shifted 31 bits will have a bit pattern that evaluates to 0.
[ October 24, 2004: Message edited by: J Borderi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg Operators
 
Similar Threads
Comparing Two ArrayLists
Master Exam Question - Incorrect answer?
exam lab gc question
Garbage collection
Array increment