gmp_xor


gmp apg

CALCULATES the bitwise exclusive OR, (XOR), of two GMP numbers.





This function calcalates bitwise XOR of two GMP numbers.



<?php

GMP gmp_xor 
( 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 XOR is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits.

The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1.

In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same.


  1 EXERCISE   

<?php

$a 
= mt_rand(10, 50);

$b = mt_rand(10, 30);

$or01a = gmp_xor($a, $b);

$or01b = $a^$b;

echo 
'gmp_xor( ' . $a .', ' . $b . ' ) = ' . 
                             
$or01a . '<br>or<br>' . $a . '^' . $b . ' = ' . 
                             
$or01b . '<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_xor(\"111111\", \"2222222\")) = " . 
               
gmp_strval(gmp_xor("111111", "2222222")) . '<br><br>';

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

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

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

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

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

$n = gmp_init("987657876543456");
var_dump(gmp_strval(gmp_xor($n, "34332")));
$n1 = gmp_init("987657878765436543456");
var_dump(gmp_strval(gmp_xor($n, $n1)));


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

?>