RE: [PHP] Test for one bit set?

2002-04-08 Thread Brinkman, Theodore

Try a bitwise and (I think the operator is '')...
?php
$bitMask = 16;
$value1 = 24;
$value2 = 36;

?$value1 has bit 5 set? ?php
if($bitMask  $value1)
{
//should get here
echo 'true\r\n';
}
else
{
echo 'false\r\n';
}

?$value2 has bit 5 set? ?php
if($bitMask  $value1)
{
echo 'true\r\n';
}
else
{
//should get here
echo 'false\r\n';
}
?
- 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 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




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:

?php

   for ($i = 1; $i = 10; $i++) 
   {
  if ($i  3)
  {
 echo($i . \n);
  }
   }
?

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 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:

?php
error_reporting(E_ALL);

$bit = 16;

$foo = bindec('0001');
$bar = bindec('0011');

echo $foo; $bar\n;

if ($foo  $bit)
{
echo Bit 5 is set.\n;
}
else 
{
echo Bit 5 is not set.\n;
}

if ($bar  $bit)
{
echo Bit 5 is set.\n;
}
else 
{
echo Bit 5 is not set.\n;
}

?


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 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:

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

for ($i = 0; $i  32769; ++$i) {
  if(isOneBitSet($i)) {
  print($i, );
  }
}

?

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