• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg Operators

 
Vishnu Munnangi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic