Saturday, September 6, 2025
HomeLanguagesPHP | gmp_xor() Function

PHP | gmp_xor() Function

The gmp_xor() is an in-built function in PHP which is used to calculate the XOR of 2 GMP numbers (GNU Multiple Precision : For large numbers).

Syntax:

gmp_xor( $num1, $num2 )

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

Return Value: This function returns a positive GMP number which is the XOR of $num1 and $num2.

Examples:

Input : $num1 = "3" $num2 = "5"
Output : 6

Input : $num1 = 1 $num2 = 1
Output : 0 

Below programs illustrate the gmp_xor() function:

Program 1: The program calculates the XOR of two numbers when numeric strings as GMP numbers are passed as arguments.




<?php
// PHP program to calculate the XOR 
// of two numbers using gmp_xor() 
  
// numeric string passed as arguments 
$xor = gmp_xor("3", "5"); 
  
// prints the GMP number which xor 
// of two numeric strings 
echo $xor;
   
?>


Output:

6

Program 2: The program calculates the XOR of two numbers when GMP numbers are passed as arguments.




<?php
// PHP program to calculate XOR 
// of two numbers 
  
// GMP numbers passed as arguments 
$xor1 = gmp_init("1101101110", 2);
$xor2 = gmp_init("0110011001", 2);
  
// function calculates the XOR of two numbers 
$xor3 = gmp_xor($xor1, $xor2);
  
// prints the GMP number which is the 
// XOR of two GMP numbers 
// gmp_strval Convert GMP number to string
//  representation in given base(default 10).
echo gmp_strval($xor3, 2);
?>


Output:

1011110111

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS