• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Right Shift and Left shift operators

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone tell me an eay way of doing the right shift and left shift operations on ints and bytes ? (>>, << and >>>)
For example, if i want to calculate
279 >> 3 or -53 >>> 2
Is there a simpler way of doing it ?

Thanks.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really don't know an easy way to do it, but I can tell you I didn't see anything nearly as complicated as your examples on the exam.
I recommend just trying to understand the concept. Understand what happens when you shift the bits. That's what the test is after.
Good Luck!

------------------
Brian Hoff
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Brian Hoff (edited August 30, 2001).]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very simple, if you can understand what I wrote here.
Say we have
"answer = M shift_operator N"
where M is the value to be shifted
N is the number of times to be shifted
"answer" is where the result gets stored
To make it simple let us divide the shift
operators into 2 categories
Category1 (right shift) will have
case1: M >> N
case2: -M >> N
case3: M >>> N
Formula:
answer = M/2^N
(In words, m divided by 2 to the power of N
(I don't know how to write power of N here)
Sign of "answer" = sign of M
if answer is not an integer
** take integer part only
(for ex: answer = 123.45, consider 123 only)
** add "1" to answer in case2.

Category2 (left shift) will have:
case4: M << N<br /> case5: -M << N<br /> Formula:<br /> answer = M * (2^N) <br /> (In words, M multiplied by 2 to the power of N)<br /> Sign of "answer" = sign of M<br /> <br /> Covering my explanation with an example:<br /> <br /> M N >> ->> >>> << -<<
-----------------------------------------------------------

129 4 8 -9 8 2064 -2064
272 2 68 -68 68 1088 -1088
745 1 372 -373 372 1490 -1490
123456 3 15432 -15432 15432 987648 -987648

I did not mention about case -M >>> N. I have not worked on it.
It always resuls in large numbers.
Does the exam centres permit to bring calculators?
The solution here is very simple with the use of a calculator.
You can tell the answer in less than 10 seconds.
Hope it is clear now. If it confuses just ignore.
 
Usha Damarla
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put indentation in message, but it does not show up after submitting it.
Anyway copy the table to some editor and put some spaces.
 
badal manuj
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Usha,
That helped me a lot.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic