• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pls tell me why?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's just the wrong
it's 32 modulo 32=0
33 modulo 32=1
31 modulo 32=31
 
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic