• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Reg. >>

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For >> the operand n by s times we can get result as :
n>>s = n / 2 ^ s
Now this works fine for even numbers
eg : 16 >> 2 = 16 / 4 = 2
But if n is odd number then how do i get the result ?
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO it is better to convert the given number to its binary format & do shift oprn.
Also, what I have learnt from this site is, that in JCP they wont ask very big numbers to shift it right/left and confuse the candidate.
It is fine if we have grasped the concept and apply it correctly.
HTH
tvs sundaram
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Angella,
Your formula is correct and it also works with odd numbers.Suppose you have:
17>>2 it will give you 2 bcoz in actual calculations 17/4 is 2.5 but when you represent it in int the result will be 2.
I hope it will help.
For more details about shift operators please visit: http://www.go4java.20m.com/usefulPages.htm
Regards
Gurpreet Sachdeva
 
crispy bacon. crispy tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic