gmp_setbit


gmp apg

SET a bit by a given value.





$index = 0 represents the least significant bit.

$value = 1 = TRUE the bit will be set to a given value.

$value = 0 = FALSE the bit will be clared.

Unlike most of the other GMP functions, this function must be called with a GMP object that already exists (using gmp_init for example).

One will not be automatically created.



<?php

void gmp_setbit 
( GMP $num, 
                              
int $index, 
                           
bool $value = true )


where,

$num = The value to modify

$index 
= The index of the bit to set

$value 
= To control the bit set

?>
 

$num


The value to modify.



$index


The index of the bit to set.



$value


The control the bit set.



  1 EXERCISE   

<?php

$a01 
= gmp_init('1000000');

$arri01 = [0, 1, 2, 3, 4, 5];

foreach(
$arri01 as $i01)
{

    
$c01 = gmp_setbit($a01, $i01, TRUE);
    
    
$t01 = gmp_testbit($a01, $i01);
    
    if(
$t01 == true)
    {
        echo 
'The bit ( ' . $a01 . ', ' . $i01 . ' ) now it\'s set<br><br>';
    }
    else
    {
        echo 
'The bit ( ' . $a01 . ', ' . $i01 . ' ) is NOT yet set<br><br>';
    }
}

?> 

  2 EXERCISE   

<?php

$a02 
= gmp_init('1000000');

$arri02 = [0, 1, 2, 3, 4, 5];

foreach(
$arri02 as $i02)
{

    
$c02 = gmp_setbit($a02, $i02, FALSE);
    
    
$t02 = gmp_testbit($a02, $i02);
    
    if(
$t02 == true)
    {
        echo 
'The bit ( ' . $a02 . ', ' . $i02 . ' ) now it\'s set<br><br>';
    }
    else
    {
        echo 
'The bit ( ' . $a02 . ', ' . $i02 . ' ) is NOT yet set<br><br>';
    }
}

?> 

  3 EXERCISE   

<?php

$a03 
= gmp_init('0xF4240');

$arri03 = [0, 1, 2, 3, 4, 5];

$b03 = mt_rand(0, 1);
// 0 = FALSE and 1 = TRUE

foreach($arri03 as $i03)
{

    
$c03 = gmp_setbit($a03, $i03, $b03);
    
    
$t03 = gmp_testbit($a03, $i03);
    
    if(
$t03 == true)
    {
        echo 
'The bit ( ' . $a03 . ', ' . $i03 . ' ) now it\'s set<br><br>';
    }
    else
    {
        echo 
'The bit ( ' . $a03 . ', ' . $i03 . ' ) is NOT yet set<br><br>';
    }
}

?>