Re: division by 7 without using division operator
- From: "quarkLore" <agarwal.prateek@xxxxxxxxx>
- Date: 6 Feb 2007 00:43:26 -0800
On Feb 1, 9:03 am, krypto.wiz...@xxxxxxxxx wrote:
Last month I appeared for an interview with EA sports and they askedquotient = ( value >> 3 ) & 0x1FFFF;
me this question.
How would you divide a number by 7 without using division operator ?
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 onThe right shift will work. This technique works only when the divisor
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.
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.
.
- Follow-Ups:
- Re: division by 7 without using division operator
- From: Keith Thompson
- Re: division by 7 without using division operator
- From: Ben Bacarisse
- Re: division by 7 without using division operator
- References:
- division by 7 without using division operator
- From: krypto . wizard
- division by 7 without using division operator
- Prev by Date: Re: Read from file into array
- Next by Date: Re: division by 7 without using division operator
- Previous by thread: Re: division by 7 without using division operator
- Next by thread: Re: division by 7 without using division operator
- Index(es):
Relevant Pages
|