PHP | gmp_and() Function

The gmp_and() is an inbuilt function in PHP which is used to calculate the bitwise AND of two GMP numbers(GNU Multiple Precision : For large numbers).

Syntax:

gmp_and($num1, $num2)

Parameters: This function accepts two GMP numbers, $num1, $num2 as mandatory parameters as shown in the above syntax. These parameters can be GMP objects in PHP version 5.6 and later, or we are also allowed to pass numeric strings such that it is possible to convert those strings to numbers.



Return Value: This function returns a GMP number which is bitwise AND of GMP numbers passed to it as parameters.

Examples:

Input : gmp_and("4", "2")
Output : 0

Input : gmp_and("9", "10")
Output : 8

Below programs illustrate the gmp_and() function in PHP:

Program 1: Program to calculate the bitwise AND of GMP numbers when numeric strings as GMP numbers are passed as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to calculate the bitwise AND
// GMP numbers passed as arguments 
  
// strings as GMP numbers
$num1 = "10";
$num2 = "9";
  
// calculate the bitwise AND of $num1 and $num2
$res = gmp_and($num1, $num2);
  
echo $res;
  
?>

chevron_right


Output:

8

Program 2: Program to calculate the bitwise AND of GMP numbers when GMP numbers are passed as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to calculate the bitwise AND
// GMP numbers passed as arguments 
  
// creating GMP numbers using gmp_init()
$num1 = gmp_init(4);
$num2 = gmp_init(2);
  
//calculate the bitwise AND of $num1 and $num2
$res = gmp_and($num1, $num2);
echo $res;
  
?>

chevron_right


Output:

0

Reference:
http://php.net/manual/en/function.gmp-and.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.