• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pls tell me why?

 
qunlbao leon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(32>>32);
the result show : 32
System.out.println(32>>31);
the result show : 0
why? I only know the result from 32>>5 to 32>>0
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that you know what the >> operator do.
In fact if the right hand side operator is 32, it just do nothing.
The explanation come from the modulo.
32 modulo 32 =0 so do nothing.
32 modulo 31 =31 so do 31
32 modulo 33 =1 so do 1.
So if you do
int >> 33 or int >>1 it just do the same thing.
I hope my explanation is helpfull!!!
If not,do not hesitate to contact me.
Sorry for my english
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just the wrong
it's 32 modulo 32=0
33 modulo 32=1
31 modulo 32=31
 
Raghav Sam
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

System.out.println(32>>32);//case1
the result show : 32
System.out.println(32>>31);//case2
the result show : 0


For shift operators, 'only the five lowest-order bits of the right-hand operand are used as the shift distance, if the left-hand operand is int'.
In case1, the 5 bits are: 00000. So, the left-hand operand (32) gets shifted by 0 bits and hence the result is the same: 32.
In case2, the 5 bits are: 11111. So, the left-hand operand (32) gets shifted by 31 bits and hence the result is: 0.

I see that your are new to this forum. Plz do a search for your question before you post. This has been discussed many times before.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic