Velocity Reviews > relations/identities

# relations/identities

aarklon@gmail.com
Guest
Posts: n/a

 12-07-2007
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 ...????

Richard Tobin
Guest
Posts: n/a

 12-07-2007
In article <(E-Mail Removed)>,
<(E-Mail Removed)> wrote:

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

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

-- Richard
--
:wq

aarklon@gmail.com
Guest
Posts: n/a

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

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

sorry i made a mistake

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

now
5 % 2 = 1
5 & 3 = 1

Peter Nilsson
Guest
Posts: n/a

 12-08-2007
(E-Mail Removed) wrote:
> (E-Mail Removed) (Richard Tobin) wrote:
> > <(E-Mail Removed)> 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

aarklon@gmail.com
Guest
Posts: n/a

 12-08-2007
On Dec 8, 2:30 am, Peter Nilsson <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > (E-Mail Removed) (Richard Tobin) wrote:
> > > <(E-Mail Removed)> 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

Richard Heathfield
Guest
Posts: n/a

 12-08-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) said:

> On Dec 8, 2:30 am, Peter Nilsson <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>> > (E-Mail Removed) (Richard Tobin) wrote:
>> > > <(E-Mail Removed)> 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

James Kuyper
Guest
Posts: n/a

 12-08-2007
(E-Mail Removed) 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.

aarklon@gmail.com
Guest
Posts: n/a

 12-08-2007
On Dec 8, 7:57 am, Richard Heathfield <(E-Mail Removed)> wrote:
> (E-Mail Removed) said:
>
>
>
> > On Dec 8, 2:30 am, Peter Nilsson <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) wrote:
> >> > (E-Mail Removed) (Richard Tobin) wrote:
> >> > > <(E-Mail Removed)> 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"

ptkmartin@gmail.com
Guest
Posts: n/a

 12-08-2007
On Dec 8, 10:00 am, (E-Mail Removed) 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...