PHP Program for XOR of Two Binary Strings Last Updated : 26 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 FunctionsBitwise XOR on StringsUsing Bitwise Operators on CharactersXOR of Two Binary Strings using Built-in FunctionsPHP 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 <?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); ?> OutputXOR Operation on Binary: 110Explanation: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.XOR of Two Binary Strings using Bitwise XOR on StringsAlternatively, we can perform the XOR operation directly on the binary strings without converting them to decimal. PHP <?php function xorOperation($bin1, $bin2) { $result = ''; $length = max(strlen($bin1), strlen($bin2)); $bin1 = str_pad($bin1, $length, '0', STR_PAD_LEFT); $bin2 = str_pad($bin2, $length, '0', STR_PAD_LEFT); for ($i = 0; $i < $length; $i++) { $result .= (string)((int)$bin1[$i] ^ (int)$bin2[$i]); } return $result; } // Driver code $bin1 = "1101"; $bin2 = "1011"; echo "XOR Operation on Binary: " . xorOperation($bin1, $bin2); ?> OutputXOR Operation on Binary: 0110Explanation:xorOperation Function: This function performs the XOR operation directly on the binary strings.str_pad() Function: Ensures that both binary strings are of equal length by padding them with zeros on the left if necessary.For Loop: Iterates over each bit of the binary strings, performing the XOR operation on each pair of corresponding bits.XOR of Two Binary Strings using Bitwise Operators on CharactersWe can also perform the XOR operation on each character of the binary strings individually. PHP <?php function xorOperation($bin1, $bin2) { $result = ''; $length = max(strlen($bin1), strlen($bin2)); $bin1 = str_pad($bin1, $length, '0', STR_PAD_LEFT); $bin2 = str_pad($bin2, $length, '0', STR_PAD_LEFT); for ($i = 0; $i < $length; $i++) { $bit1 = (int)$bin1[$i]; $bit2 = (int)$bin2[$i]; $xorBit = $bit1 ^ $bit2; $result .= (string)$xorBit; } return $result; } // Driver code $bin1 = "1101"; $bin2 = "1011"; echo "XOR Operation on Binary: " . xorOperation($bin1, $bin2); ?> OutputXOR Operation on Binary: 0110Explanation:xorOperation Function: This function performs the XOR operation on each character of the binary strings.$bit1 and $bit2: Variables that store the individual bits from the binary strings.$xorBit: Stores the result of the XOR operation on the individual bits. Comment More infoAdvertise with us Next Article PHP program to check for Anagram B blalverma92 Follow Improve Article Tags : PHP Similar Reads PHP Program to Count number of binary strings without consecutive 1's Write a PHP program for a given positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1s. Examples: Input: N = 2Output: 3Explanation: The 3 strings are 00, 01, 10 Input: N = 3Output: 5Explanation: The 5 strings are 000, 001, 010, 2 min read PHP program to check for Anagram An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all the original letters exactly once. To check for anagrams in PHP, remove spaces, convert to lowercase, sort characters, and compare the sorted strings or arrays.ExamplesInput : "anagram", "nagaram"Ou 5 min read PHP Program to check a string is a rotation of another string Given the two strings we have to check if one string is a rotation of another string. Examples: Input : $string1 = "WayToCrack", $string2 = "CrackWayTo"; Output : Yes Input : $string1 = "WillPower" $string2 = "lliW"; Output : No. The above problem can be easily solved in other languages by concatena 3 min read Concatenation of two strings in PHP In this article, we will concatenate two strings in PHP. There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right 2 min read Bitwise XOR Operator in Programming Bitwise XOR Operator is represented by the caret symbol (^). It is used to perform a bitwise XOR operation on the individual bits of two operands. The XOR operator returns 1 if the corresponding bits in the two operands are different, and 0 if they are the same.Table of Content What is Bitwise XOR?B 5 min read PHP program to swap two numbers Integer values can be stored in a variable in PHP. It is easy to store and modify and swap these integer values using various mentioned approaches: Using a temporary variable: The value of the first number is stored in the temporary variable. This value is then replaced by the value of the second nu 4 min read Like