Forum Moderators: coopster

Message Too Old, No Replies

Receiving Sum of Values as One Parameter

         

PhoneGuy

3:58 am on Aug 3, 2004 (gmt 0)

10+ Year Member



I really don't know the term for this question.

I want to pass one parameter (a sum of numbers ie: 2+4+8+16+32+64) and parse the number to determine if any given number (Are these are base2 numbers?) is found in the sum. Say you pass a number (4+8+64) that triggers 3 different options in the function.

This function will parse the number but is there a faster or more compact method to do the same?

Function isOption($pn_value, $pn_sum)
{
// verify that $pn_value can be in $pn_sum
// returns true/false
// first # is 2 then double to increase (ie: 2, 4, 8, 16, 32...)
// maximum sum = 16384
$ln_control_sum = 16384;
$ln_sum = $pn_sum;

while( $ln_sum >= 1 )
{
if( $ln_sum >= $ln_control_sum )
{
$ln_sum = $ln_sum - $ln_control_sum;
if( $pn_value = $ln_control_sum ) return True;
}

$ln_control_sum = $ln_control_sum / 2;
}

return False;
}

Any ideas or comments would be welcomed.

ergophobe

6:19 pm on Aug 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If I get what you're doing, I think a little bitwise math would do the trick.

You have a series that is basically 2^x that goes

1 2 4 8 16 32 64 ...

if you have 2 + 64 = 66, then options 2 and 7 are set, like file permissions. So given 37, you want to know whether option 2 is set.

$x = term to test - is it an option set in the total/
$y = sum

if ($y == ($y & $z))
{
echo "Y is set";
}

For example
$x = 2;
$y = 66;

$x & $y = 2 = bitwise and = $x

You woudl have to figure out how to make this work for the series of options that you're interested in, but you could, for example, just have an array of options

$options = array(1, 2, 4, 8, 16);

And then walk through the array.

PhoneGuy

7:29 pm on Aug 3, 2004 (gmt 0)

10+ Year Member



Thanks for the reply ergophobe.

This type of parameter is something I used many years ago when accessing various Windows features. As in your example one would pass a 66 as the parameter which would trigger effects based on both 2 and 64. Like 2 might be a "OK" button and 64 might be a "Cancel" button so both buttons would be displayed in the Windows window.

I want to do the same here in PHP.

You said "if ($y == ($y & $z))" but did you mean "if ($y == ($y & $x))" (x rather than z)?

Where did you get "So given 37"?

if( isOption(2, 66) ) would return true since 2 is in 66.

ergophobe

8:05 pm on Aug 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, I meant to use $x and $y. I tested before posting and had x, y, and z and was careless. Sorry.

As for 37, that came from the Grey Matter Random Number Generator. I just meant, give a number, you want to know whether a given feature was selected.

Tom

ergophobe

8:08 pm on Aug 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




if( isOption(2, 66) ) would return true since 2 is in 66.

Exactly what I'm driving at.

function isOption($option, $total)
{

if ($option == ($option & $total))
{
return true;
}

return false;

}

PhoneGuy

10:02 pm on Aug 3, 2004 (gmt 0)

10+ Year Member



That was excellent. Thanks for the help. It works better than my original plan and faster too.

Did you just think that up? :-)

And I always thought Bitwise was just a pain in the butt.

ergophobe

10:36 pm on Aug 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It should be super fast. There's nothing that computers excel at like bitwise operations.

I have to confess that I never use bitwise math, but when I saw your question, it just begged for it. So yeah, I just thought that up, especially for you.

First round's on you

Tom