Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   relations/identities (http://www.velocityreviews.com/forums/t558133-relations-identities.html)

 aarklon@gmail.com 12-07-2007 05:43 PM

relations/identities

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 12-07-2007 06:42 PM

Re: relations/identities

<aarklon@gmail.com> 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 12-08-2007 05:52 AM

Re: relations/identities

On Dec 7, 1:42 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>
> <aark...@gmail.com> 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

 Peter Nilsson 12-08-2007 07:30 AM

Re: relations/identities

aark...@gmail.com wrote:
> rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
> > <aark...@gmail.com> 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 12-08-2007 12:07 PM

Re: relations/identities

On Dec 8, 2:30 am, Peter Nilsson <ai...@acay.com.au> wrote:
> aark...@gmail.com wrote:
> > rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
> > > <aark...@gmail.com> 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 12-08-2007 12:57 PM

Re: relations/identities

aarklon@gmail.com said:

> On Dec 8, 2:30 am, Peter Nilsson <ai...@acay.com.au> wrote:
>> aark...@gmail.com wrote:
>> > rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>> > > <aark...@gmail.com> 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 12-08-2007 01:02 PM

Re: relations/identities

aarklon@gmail.com 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 12-08-2007 03:00 PM

Re: relations/identities

On Dec 8, 7:57 am, Richard Heathfield <r...@see.sig.invalid> wrote:
> aark...@gmail.com said:
>
>
>
> > On Dec 8, 2:30 am, Peter Nilsson <ai...@acay.com.au> wrote:
> >> aark...@gmail.com wrote:
> >> > rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
> >> > > <aark...@gmail.com> 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 12-08-2007 03:09 PM

Re: relations/identities

On Dec 8, 10:00 am, aark...@gmail.com 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...

 All times are GMT. The time now is 07:10 AM.