Bitwise & operator tricky question
Author Message
Bitwise & operator tricky question

Does anyone know what the following does:

for (a = 1; a < 100; a++)
{
a =& (a-1);
}

I know what the & operator does, but what is special about doing it to an
integer and that same integer minus 1?
Some kind of pattern.

Thanks!

Sat, 22 Jul 2000 03:00:00 GMT
Bitwise & operator tricky question

Quote:

>Does anyone know what the following does:

>for (a = 1; a < 100; a++)
>  {
>    a =& (a-1);
>  }

Yes, it generates error messages about different levels of indirection or a
syntax error, I suppose.  If you meant a &= (a-1) then it loops forever, since
the first pass will set a to zero, then it goes to 1, then to zero etc.

Quote:

>I know what the & operator does, but what is special about doing it to an
>integer and that same integer minus 1?
>Some kind of pattern.

You will have to state your question properly if you expect to receive a
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com

Sat, 22 Jul 2000 03:00:00 GMT
Bitwise & operator tricky question

Quote:
> Does anyone know what the following does:

> for (a = 1; a < 100; a++)
>   {
>     a =& (a-1);

^^

I hope this is a type.  Correct C is "&=", not "=&".  At least
not for a long, long time.

Quote:
>   }

> I know what the & operator does, but what is special about
doing it to an
> integer and that same integer minus 1?
> Some kind of pattern.

> Thanks!

What this does is create an infinite loop.  In the first
iteration, a == 1 (initialization part of the for loop).  (a -
1) = 0.  1 &= 0 is 0.  Then the a++ sets a back to 1 and the
same thing happens all over again, and again, and again.

Jack

Sun, 23 Jul 2000 03:00:00 GMT
Bitwise & operator tricky question

Quote:

>Does anyone know what the following does:

>for (a = 1; a < 100; a++)
>  {
>    a =& (a-1);
>  }

>I know what the & operator does, but what is special about doing it to
>an integer and that same integer minus 1?  Some kind of pattern.

Are you sure this is right?  If so, you are looking at some very out
of date C code.  The "=&" assignment operator doesn't exist anymore in
Standard C.  It use to mean the same thing as "&=".

The loop rewritten as

for (a = 1; a < 100; a++)
{
a &= (a-1);
}

Is infinite as far as I can observe.

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>

Sun, 23 Jul 2000 03:00:00 GMT
Bitwise & operator tricky question

Doug...

Not very tricky. If you really meant "a = & (a-1);" the result is a compiler
error. If you meant
"a &= (a-1)" then as soon as a gets to 2, the result of anding 1 and two will
set a to zero and, as I'm sure you know, the result of anding zero with
anything is zero; so a will have a value of zero from then on -- resulting in a
never-ending loop.

Morris Dovey

Des Moines, Iowa, USA

Sun, 23 Jul 2000 03:00:00 GMT
Bitwise & operator tricky question

Quote:
>Does anyone know what the following does:

>for (a = 1; a < 100; a++)
>  {
>    a =& (a-1);
>  }

>I know what the & operator does, but what is special about doing it to an
>integer and that same integer minus 1?

x &= x-1;

is a trick to zero the lowest order 1 bit in an integer (to be safe you
should use unsigned int or unsigned long). Aso thers have noted it doesn't
make much sense in a loop like this because it won't allow a to get larger
than 1.

--
-----------------------------------------

-----------------------------------------

Sun, 23 Jul 2000 03:00:00 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages