can u please explain it in detail
On 2/2/07, peternilsson42 <[EMAIL PROTECTED]> wrote:
shubhrajyoti datta <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > Parul Puri wrote:
> > >
> > > how to divide a number by 3 without using * / %
> >
> > use shift operators
>
> How ?Multiples of 2 is understandable but 3
> please explain..
#include <stdio.h>
/* divide 8-bit unsigned number by 3 */
unsigned divu3(unsigned n)
{
return (n + (n << 1) + (n << 3) + (n << 5) + (n << 7)) >> 9;
}
int main(void)
{
unsigned i;
for (i = 0; i < 256; i++)
printf("%3u / 3 == %3u\n", i, divu3(i));
return 0;
}
--
Peter
--
--
Thanks and Regards
Parul Puri
AMDOCS Development Centre India (DVCI)
Revenue Management - A&F Line India