XOR of Two Binary Strings using Built-in Functions
PHP provides built-in functions like bindec() and decbin() to convert between binary and decimal representations, which can be used to perform the XOR operation.
<?php
function xorOperation($bin1, $bin2) {
$dec1 = bindec($bin1);
$dec2 = bindec($bin2);
$xorResult = $dec1 ^ $dec2;
return decbin($xorResult);
}
// Driver code
$bin1 = "1101";
$bin2 = "1011";
echo "XOR Operation on Binary: "
. xorOperation($bin1, $bin2);
?>
Output
XOR Operation on Binary: 110
Explanation:
- xorOperation Function: This function takes two binary strings as arguments and returns their XOR result as a binary string.
- bindec() Function: Converts a binary string to its decimal equivalent.
- ^ Operator: Performs the bitwise XOR operation on the decimal equivalents of the binary strings.
- decbin() Function: Converts the decimal result of the XOR operation back to a binary string.
PHP Program for XOR of Two Binary Strings
Given two binary strings, the task is to calculate the XOR of two binary strings in PHP. The XOR operation is a bitwise operation used in computer science and digital electronics. It takes two binary strings as input and produces a binary string as output, where each bit in the output is the result of the XOR operation on the corresponding bits in the input strings.
Table of Content
- Using Built-in Functions
- Bitwise XOR on Strings
- Using Bitwise Operators on Characters
Contact Us