gmp_and


gmp apg

CALCULATES the bitwise AND of two GMP numbers.





This function calcalates bitwise AND of two GMP numbers.



<?php

GMP gmp_and 
GMP|int|string $num1
                         
GMP|int|string $num2 )


where,

$num1 The first GMP number

$num2 
The second GMP number

?>
 

$num1


The first GMP number.



$num2


The second GMP number.



 Bitwise Operators 

Bitwise operators allow evaluation and manipulation of specific bits within an integer.


OPERATOR USE NAME WHAT DOES MAKES
& $a & $b AND Bits that are set in
both $a and $b are set
| $a | $b Or (inclusive) Bits that are set in
either $a or $b are set
^ $a ^ $b XOR
Or (exclusive)
Bits that are set in
$a or $b
but not both are set
~ ~$a Not Bits that are set in
$a are not set,
and vice versa
<< $a << $b Shift Left Shift the bits of $r1,
$b steps to the left
(each step means "multiply by two")
>> $a >> $b Shift Right Shift the bits of $r1,
$b steps to the right
(each step means "divide by two")
ed48


A bitwise AND is a binary operation that takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits, which is equivalent to multiplying them.

Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is:

1 * 1 = 1 or otherwise 1 * 0 = 0 and 0 * 0 = 0.


  1 EXERCISE   

<?php

// Run this code several times

$a mt_rand(1050);

$b mt_rand(1030);

$and01a gmp_and($a$b);

$and01b $a&$b;

echo 
'gmp_and( ' $a .', ' $b ' ) = ' 
                             
$and01a '<br>or<br>' $a '&' $b ' = ' 
                             
$and01b '<br><br>where:<br><br>';

$bin01a decbin($a);
$bin01b decbin($b);

echo 
'decbin( ' $a ' ) = ' $bin01a '<br>'
echo 
'decbin( ' $b ' ) = ' $bin01b '<br>';

?> 

  2 EXERCISE   

<?php

echo "gmp_strval(gmp_and(\"111111\", \"2222222\")) = " 
               
gmp_strval(gmp_and("111111""2222222")) . '<br><br>';

echo 
"gmp_strval(gmp_and(123123, 435234)) = " 
               
gmp_strval(gmp_and(123123435234)) . '<br><br>';

echo 
"gmp_strval(gmp_and(555, \"2342341123\")) = " 
               
gmp_strval(gmp_and(555"2342341123")) . '<br><br>';

echo 
"gmp_strval(gmp_and(-1, 3333)) = " 
               
gmp_strval(gmp_and(-13333)) . '<br><br>';

echo 
"gmp_strval(gmp_and(4545, -20)) = " 
               
gmp_strval(gmp_and(4545, -20))  . '<br><br>';

try {
    
var_dump(gmp_strval(gmp_and("test""no test")));
} catch (
\TypeError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

$n gmp_init("987657876543456");
var_dump(gmp_strval(gmp_and($n"34332")));
$n1 gmp_init("987657878765436543456");
var_dump(gmp_strval(gmp_and($n$n1)));


try {
    
var_dump(gmp_and(array(), 1));
} catch (
\TypeError $e) {
    echo 
$e->getMessage() . "<br><br>";
}
try {
    
var_dump(gmp_and(1, array()));
} catch (
\TypeError $e) {
    echo 
$e->getMessage() . "<br><br>";
}
try {
    
var_dump(gmp_and(array(), array()));
} catch (
\TypeError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

?>