RE: [PHP] Test for one bit set?

2002-04-08 Thread Brinkman, Theodore

Try a bitwise and (I think the operator is '&')...
$value1 has bit 5 set? $value2 has bit 5 set? 
- Theo
-Original Message-
From: Miguel Cruz [mailto:[EMAIL PROTECTED]]
Sent: Sunday, April 07, 2002 5:04 PM
To: Charlie Killian
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] Test for one bit set?


On Sun, 7 Apr 2002, Charlie Killian wrote:
> How can I test if a number only has on bit set?
> 
> So testing different numbers will return TRUE or FALSE:
> 
> testing 0001 would return TRUE.
> testing 0011 would return FALSE.

Think back to math class when you were 14!

  function isOneBitSet($n)
  {
$x = log($n)/log(2);
return ($x == intval($x));
  }

Cheesy alternative:

  function isOneBitSet($n)
  {
return (1 == substr_count(base_convert($n, 10, 2), '1'));
  }

miguel


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Test for one bit set?

2002-04-07 Thread Charlie Killian

Thanks to all those that replied. Especially Torben. (What have you been up
to?)

Miguel's solution works great.

Check out the example below:



Prints out:
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768,

Just what I wanted.

Thanks again,

Charlie


> > How can I test if a number only has on bit set?
> >
> > So testing different numbers will return TRUE or FALSE:
> >
> > testing 0001 would return TRUE.
> > testing 0011 would return FALSE.
>
> Think back to math class when you were 14!
>
>   function isOneBitSet($n)
>   {
> $x = log($n)/log(2);
> return ($x == intval($x));
>   }
>
> Cheesy alternative:
>
>   function isOneBitSet($n)
>   {
> return (1 == substr_count(base_convert($n, 10, 2), '1'));
>   }
>
> miguel
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Test for one bit set?

2002-04-07 Thread Lars Torben Wilson

On Sun, 2002-04-07 at 13:38, Charlie Killian wrote:
> How can I test if a number only has on bit set?
> 
> So testing different numbers will return TRUE or FALSE:
> 
> testing 0001 would return TRUE.
> testing 0011 would return FALSE.
> 
> TIA
> 
> Charlie

AND them bitwise:




Torben

-- 
 Torben Wilson <[EMAIL PROTECTED]>
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Test for one bit set?

2002-04-07 Thread bvr


This shows all integers from 1 to 10 that have the 2 least significant 
bits set:



bvr.

Charlie Killian wrote:

>How can I test if a number only has on bit set?
>
>So testing different numbers will return TRUE or FALSE:
>
>testing 0001 would return TRUE.
>testing 0011 would return FALSE.
>
>TIA
>
>Charlie
>
>
>
>
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Test for one bit set?

2002-04-07 Thread Miguel Cruz

On Sun, 7 Apr 2002, Charlie Killian wrote:
> How can I test if a number only has on bit set?
> 
> So testing different numbers will return TRUE or FALSE:
> 
> testing 0001 would return TRUE.
> testing 0011 would return FALSE.

Think back to math class when you were 14!

  function isOneBitSet($n)
  {
$x = log($n)/log(2);
return ($x == intval($x));
  }

Cheesy alternative:

  function isOneBitSet($n)
  {
return (1 == substr_count(base_convert($n, 10, 2), '1'));
  }

miguel


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Test for one bit set?

2002-04-07 Thread Charlie Killian

How can I test if a number only has on bit set?

So testing different numbers will return TRUE or FALSE:

testing 0001 would return TRUE.
testing 0011 would return FALSE.

TIA

Charlie





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php