This week's book giveaway is in the Python forum.
We're giving away four copies of High Performance Python for Data Analytics and have Tiago Rodrigues Antao on-line!
See this thread for details.
Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Bit Shifting Hell

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to come up with a good approach to the problem: I need feedback.
how to calculate
x [operator] y
where operator is >>> , >> , <<

stated that:

a, x , y are ints
a = (int)Math.pow(2,y)
x>0
y>0 and y < 32
if ( y > 32 )
subtract 32 until y < 32 and proceed
if ( y == 32 )
the result is x (with sign unchanged)
because any shift by 32
gives back x by definition.


-------------------------

x>>y or x >>>y

RESULT: x / a
-x>>y

if x <= a
RESULT: -1
if ( x > a && (x % a != 0 ))
RESULT: (-x / a) -1
if ( x > a && (x % a == 0))
RESULT: -x / a

-x>>>y

RESULT: A POSITIVE NUMBER -->

convert to binary* and shift to
the right of Y places filling in with
zeros from the left
* to get a negative number in binary:

1) forget the sign and convert to bin
2) invert the bits
3) add 1 to the result
(if x is small expect a very big result)
x << y

RESULT : x * a
-x << y

RESULT : -x * a
-------------------------------
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it much easier just to think in terms of the bit pattern rather than the result of multplying or dividing.
You can use the static Integer method toBinaryString to see the bit pattern for any int.
Bill
 
andrea fare
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you, the only problem is that
I need a rule that I can use WHILE I am taking the exam.

Andrea
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
andrea,
during the exam you'll have a piece of paper and a pen on which you can write your numbers as bit sequences and do the shifting accordingly. I think it is much more easier that way than trying to learn a set of rules to apply in each different case.
Just learn how to convert numbers to a binary sequence (Campfire Story: Cat and Mouse Games with Bits with a nice bit shifting applet at the bottom of the page) and then apply the shifting to them, you'll see it will be easier to move bits around than trying to remember your rules during the exam...
[ February 13, 2002: Message edited by: Valentin Crettaz ]
 
andrea fare
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good I didn't know you could use paper.
Thanks.
Andrea
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic