On 2/2/07, Parul Puri <[EMAIL PROTECTED]> wrote:
>
>
> can u please explain it in detail
>
n << x is the same as n * 2^x,
n + (n << 1) + (n << 3) + (n << 5) + (n << 7)
becomes
n + 2n + 8n + 32n + 128n = 171n
result >> 9 is the same as division by 2^9 = 512
And thus (171/512)*n = 0.334*n
>
> 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