Jump to content

[SOLVED] How to check if multiple of 2?


t_machine

Recommended Posts

I am creating a tournament script and need to have total matches as a multiple of 2 (2, 4, 8, 16, 32... ). How can i check the number to see if it's ok.

 

example

$matchesNum = 38;//obviously this is not a multiple

 

How would I check that number to make sure it was 32 or 64...

 

Thanks for any help :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/63593-solved-how-to-check-if-multiple-of-2/
Share on other sites

Try somethign like:

 

<?php
function multipleof2($num){
    while($num > 2){
        $num /= 2;
    }
    if($num == 2){
        return true;
    }else{
        return false;
    }
}
if(multipleof2(32)){
    //multiple of two
}else{
    //not a multiple of two
}
?>

This works... I tested it:

<?php
function multiple($number){
$max = 20000;
for($i=2;$i<$max;$i=$i*2){
	if($number == $i){
		return TRUE;
		break;
	}else{
		continue;
	}
}
}
if(multiple(10)){
echo'This is a multiple of 2.';
}else{
echo'This is NOT a multiple of 2.';
}
?>

that is right chiprivers, so if you were to put it into function form, it would look like this:

 

 

<?php
function mul($x){
if ($x % 2 == 0) {
	return TRUE;
}else{
	return FALSE;
}
}
if(mul(38)){
echo'This is a multiple of 2.';
}else{
echo'This is NOT a multiple of 2.';
}
?>

Folks, go back and read the real question. Even though the OP is asking about a multiple of 2 (for which a simple modulus test will suffice), s/he's really wanting to know if a number is a positive integer power of 2.

 

2,4,8,16,32 ..... i.e. 2^1, 2^2, 2^3, etc.

 

http://ca.php.net/manual/en/function.log.php with base=2 might help

Thanks everyone for the replies :)

AndyB does have the right idea but I am not sure how to implement it. Using that code, how would I know if 32 is a power of 2?

 

I guess for simplicity I could just add those numbers in an array(2, 4, 6.....) then check if total matches in the array.

I would prefer if a function could be created instead to check if the total matches, is a power of 2.

Thanks :)

<?php
function multiple($number){
$max = 20000;
$m = 1;
for($i=2;$i<$max;$i=$i*2){
	$m++;
	if($number == $i){
		GLOBAL $ar;
		$m--;
		$ar = array(TRUE,$m);
		return $ar;
		break;
	}else{
		continue;
	}
}
}
if(multiple(32)){
echo'This is a multiple of 2.<br>2^'.$ar['1'];
}else{
echo'This is NOT a multiple of 2.';
}
?>

Try somethign like:

 

<?php
function multipleof2($num){
    while($num > 2){
        $num /= 2;
    }
    if($num == 2){
        return true;
    }else{
        return false;
    }
}
if(multipleof2(32)){
    //multiple of two
}else{
    //not a multiple of two
}
?>

 

This code will do what you want, because it just keeps dividing the number by 2 until it is less than or equal to 2. then, if it is 2, it is obviously a power of 2, or if it is something else, then it is not a power of 2.

Example:

 

32/2= 16/2=8/2=4/2=2, therefore 32 is power of 2.

38/2= 19, already not a power of 2

 

its not so complicated

 

 

function isit($num)

{

$max=2000/*this will set ur upper limit, iterative will be better than recurrsive because it can set limit to the resources/time which server will utilize*/

for ($d=0;$d<$max;$d++)

{

if(2^$d==$num)return true;

}

return false;

}

 

isit($number)

{

//if it is

}else

{

//if itisnt

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.