# relations/identities

Discussion in 'C Programming' started by aarklon@gmail.com, Dec 7, 2007.

1. ### Guest

Hi all,

the following are the relations involving modulus operator that i have
found working

1) a = a %b + (a/b) * b ; for integral values of a and b

2) w % n = w & (n-1);

can any one give more examples for relations like this ...????

, Dec 7, 2007

2. ### Richard TobinGuest

In article <>,
<> wrote:

>2) w % n = w & (n-1);

You need a constraint on n here. 5 % 3 = 2 but 5 & 2 = 0.

-- Richard
--
:wq

Richard Tobin, Dec 7, 2007

3. ### Guest

On Dec 7, 1:42 pm, (Richard Tobin) wrote:
> In article <>,
>
> <> wrote:
> >2) w % n = w & (n-1);

>
> You need a constraint on n here. 5 % 3 = 2 but 5 & 2 = 0.
>
> -- Richard
> --
> :wq

it should be rather w % (n-1) = w & n;

now
5 % 2 = 1
5 & 3 = 1

, Dec 8, 2007
4. ### Peter NilssonGuest

wrote:
> (Richard Tobin) wrote:
> > <> wrote:
> > > 2) w % n = w & (n-1);

> >
> > You need a constraint on n here.

And w.

> > 5 % 3 = 2 but 5 & 2 = 0.

>
> sorry i made a mistake

Your correction was a much bigger mistake.

> it should be rather w % (n-1) = w & n;

Even on its own terms this makes no sense. [Try w == n.]

--
Peter

Peter Nilsson, Dec 8, 2007
5. ### Guest

On Dec 8, 2:30 am, Peter Nilsson <> wrote:
> wrote:
> > (Richard Tobin) wrote:
> > > <> wrote:
> > > > 2) w % n = w & (n-1);

>
> > > You need a constraint on n here.

>
> And w.
>
> > > 5 % 3 = 2 but 5 & 2 = 0.

>
> > sorry i made a mistake

>
> Your correction was a much bigger mistake.
>
> > it should be rather w % (n-1) = w & n;

>
> Even on its own terms this makes no sense. [Try w == n.]

let us add a constraint n < w and n >= 2

, Dec 8, 2007
6. ### Richard HeathfieldGuest

said:

> On Dec 8, 2:30 am, Peter Nilsson <> wrote:
>> wrote:
>> > (Richard Tobin) wrote:
>> > > <> wrote:
>> > > > 2) w % n = w & (n-1);

>>
>> > > You need a constraint on n here.

>>
>> And w.
>>
>> > > 5 % 3 = 2 but 5 & 2 = 0.

>>
>> > sorry i made a mistake

>>
>> Your correction was a much bigger mistake.
>>
>> > it should be rather w % (n-1) = w & n;

>>
>> Even on its own terms this makes no sense. [Try w == n.]

>
> let us add a constraint n < w and n >= 2

Okay. Let n = 79, and let w = 83. w % n is 4, but w & (n - 1) is 66 if my
bit-twiddling is right. Last I checked, 4 != 66.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Richard Heathfield, Dec 8, 2007
7. ### James KuyperGuest

wrote:
> Hi all,
>
> the following are the relations involving modulus operator that i have
> found working

....
> 2) w % n = w & (n-1);
>
> can any one give more examples for relations like this ...????

The second relationship is true only when n is a power of 2.

James Kuyper, Dec 8, 2007
8. ### Guest

On Dec 8, 7:57 am, Richard Heathfield <> wrote:
> said:
>
>
>
> > On Dec 8, 2:30 am, Peter Nilsson <> wrote:
> >> wrote:
> >> > (Richard Tobin) wrote:
> >> > > <> wrote:
> >> > > > 2) w % n = w & (n-1);

>
> >> > > You need a constraint on n here.

>
> >> And w.

>
> >> > > 5 % 3 = 2 but 5 & 2 = 0.

>
> >> > sorry i made a mistake

>
> >> Your correction was a much bigger mistake.

>
> >> > it should be rather w % (n-1) = w & n;

>
> >> Even on its own terms this makes no sense. [Try w == n.]

>
> > let us add a constraint n < w and n >= 2

>
> Okay. Let n = 79, and let w = 83. w % n is 4, but w & (n - 1) is 66 if my
> bit-twiddling is right. Last I checked, 4 != 66.

I have seen this result in one book, but i don't remember it
correctly.I think it is in your book "C unleashed"

, Dec 8, 2007
9. ### Guest

On Dec 8, 10:00 am, wrote:
> w % n = w & (n-1);
> I have seen this result in one book, but i don't remember it correctly.

You remembered it wrong.

The formula above is guaranteed only if n is a power of 2.
That is, n must be 1, or 2, or 4, or 8, or 16, or 32...

, Dec 8, 2007