Re: division by 7 without using division operator



On Feb 1, 9:03 am, krypto.wiz...@xxxxxxxxx wrote:
Last month I appeared for an interview with EA sports and they asked
me this question.

How would you divide a number by 7 without using division operator ?

quotient = ( value >> 3 ) & 0x1FFFF;

The assumption is that of 4 byte integer and signed types. & is to be
used when using signed integers because if the most significant bit of
value was filled right shift will append 1's at the beginning of your
number.

I did by doing a subtraction and keeping a counter that kept a tab on
how many times I subtracted.

Later, the EA sport guy told me that of course there are can be better
technique by using bit operator.

Since 7 has a binary representation 111, my guess is that a left shift
operation of 3 bits should give the answer, but I couldn't get it to
work.
The right shift will work. This technique works only when the divisor
is of the bit pattern 11...1 in other words it has to be 1 less than a
power of 2. 7,15,31 will work.

This technique is used a lot in linux kernel/related system software
to create circular buffers which have number of elements as a power of
2 and then use this technique to wrap around the buffer when index
reaches end (though here remainder calculation is done which is simple
x = x & 0x0003 )

Any comments ?

Bit masking techniques take lesser MIPS in general than iteration
based techniques. Loop based techniques also need loop index
variables. But bit masking techniques churn out cryptic code and often
non portable so one must use them with utmost caution.

.



Relevant Pages

  • Re: Minimizing squeaks in VL Et. 1 middle section
    ... pause a second, then lift the LH fingers off the string, straight up, ... play the 'a', IOW, just behind the 7th fret, fourth string. ... Continue the scale and do this technique at every place there is ... When you play scales do this at every shift ...
    (rec.music.classical.guitar)
  • Re: Highlighting an entire page (or more) in one step.
    ... You don't need to Shift while dragging. ... On Feb 21, 11:06 pm, Highlighter ... selection mode (or double-click the letters EXT in the status bar in Word ... this is not the most effective technique. ...
    (microsoft.public.word.docmanagement)
  • Re: coax....
    ... The non-existance of god wrote: ... 'as said nowt incorrect in your's entire life. ... That's a simplistic attempt to shift ground. ... technique. ...
    (uk.radio.amateur)
  • Re: Last word on Carlevaro DVD
    ... A wide legato shift, either on the same string or another, can be made to ... There is a little "technique" plus having done ... to the f# on the second string. ...
    (rec.music.classical.guitar)