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
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
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
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 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

 

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.