This week's book giveaway is in the Android forum.We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Head First Android this week in the Android forum!

# Reg Operators

Vishnu Munnangi
Ranch Hand

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

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

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