


Adjust the binary representation lengths of two numbers to be equal and then perform an XOR operation
XOR, or exclusive OR, is a Boolean logic operation used to generate parity bits for error checking, fault tolerance, etc. Various symbols are used to represent this operation: ^, ⊕, ⊻, etc.
XOR logic
The XOR operation is true only if the two parameters are different. In other words, the XOR of the same bits is 0, and the XOR of different bits is 1.
Same bits -
0^0=0
1^1=0
Different bits −
0^1=1
1 ^ 0 = 1
Problem Statement
Given two numbers a and b, find their exclusive OR after making the lengths of their binary representations equal.
Tip − By adding a trailing zero after the smaller number, the binary representation will become equal.
Example
Input -
a = 10, b = 5
Output-
0
illustrate
The binary representation of 10 is 1010, and the binary representation of 5 is 101.
Add the trailing zero to 5 to get 1010.
Therefore, the XOR result of 1010^1010 is 0.
Therefore, output.
Enter -
a = 15, b = 8
Output −
7
illustrate -
The binary representation of15 is 1111, and the binary representation of 8 is 1000.
Since the two binary representations are equal in length, there is no need to add trailing zeros.
1111 ^ The XOR result of 1000 is 0111, which is 7 in decimal notation. Therefore, the output is 7.
Enter -
a = 15, b = 3
Output −
7
illustrate -
The binary representation of15 is 1111. The binary representation of 3 is 11. The binary representation of 3, with a trailing zero, becomes 1100.
The XOR result of 1111^1100 is 0011.
0011 is 3 in decimal representation. Therefore, the result is output.
method
Calculate the number of digits in two numbers.
The number of digits can be calculated by shifting the number to the right until it becomes zero, and counting the number of times the loop is executed. Shifting a number to the right by 1 place is equivalent to dividing it by 2.
If the smaller number has fewer digits, the left shift is performed as follows: smaller_number
XOR two numbers to get the answer and print it.
pseudocode
main() Initialize a -> 15 and b -> 3. Function call find_xor(a,b); find_xor(int a, int b): c -> minimum of a and b. d -> maximum of a and b. count_c -> bit_count(c) count_d ->bit_count(d) If count_c < cound_d, then: c -> c << (count_d - count_c) Return c XOR d. bit_count(int x): count -> 0 while(x != 0): Increase the count by one. Right shift x by 1, i.e., divide it by 2. Return x.
Example
The following is a C program for calculating the XOR value of two numbers after making their binary representations equal in length.
#include <bits/stdc++.h> using namespace std; // Function to count the number of bits in binary representation // of an integer int bit_count(int x){ //Initialize count as zero int count = 0; //Count the bits till x becomes zero. while (x) { //Incrementing the count count++; // right shift x by 1 // i.e, divide by 2 x = x>>1; } return count; } //Function to find the XOR of two numbers. Trailing zeros are added to the number having a lesser number of bits to make the bits in both numbers equal. int find_xor(int a, int b){ //Store the minimum and maximum of both the numbers int c = min(a,b); int d = max(a,b); //Store the number of bits in both numbers. int count_c = bit_count(c); int count_d = bit_count(d); //If the number of bits in c is less, left shift if by the number of exceeding bits. if (count_c < count_d){ c = c << ( count_d - count_c); } return (c^d); } //Driver code int main(){ //Initialize a and b. int a = 15, b = 3; cout << "a = 15, b = 3" << endl; //Store the XOR of both the numbers after required computations //Function call int ans = find_xor(a,b); //Print the final result cout << "XOR of a and b: "<<ans<<endl; return 0; }
Output
a = 15, b = 3 XOR of a and b: 3
analyze
Time complexity - O(log n) [logarithm]
Due to the while loop in the count function, the time complexity is logarithmic.
Since this number is divided by two until it becomes zero, the complexity becomes log n base 2.
Space Complexity - O(1) [Constant]
Space complexity is constant because no extra space is used in the program.
in conclusion
In this article, we discussed the problem of computing the XOR of two numbers after making their binary representations equal in length.
We discussed the concept of XOR, and then explained examples and methods. This method uses trailing zeros to equalize the number of bits in the binary representation. We also saw pseudocode and a C program for the problem.
The above is the detailed content of Adjust the binary representation lengths of two numbers to be equal and then perform an XOR operation. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Binary arithmetic is an operation method based on binary numbers. Its basic operations include addition, subtraction, multiplication and division. In addition to basic operations, binary arithmetic also includes logical operations, displacement operations and other operations. Logical operations include AND, OR, NOT and other operations, and displacement operations include left shift and right shift operations. These operations have corresponding rules and operand requirements.

EDVAC has two major improvements: one is the use of binary, and the other is the completion of stored programs, which can automatically advance from one program instruction to the next, and its operations can be automatically completed through instructions. "Instructions" include data and programs, which are input into the memory device of the machine in the form of codes, that is, the same memory device that stores data is used to store instructions for performing operations. This is the new concept of so-called stored programs.

Binary numbers are represented by 1s and 0s. The 16-bit hexadecimal number system is {0,1,2,3…..9,A(10),B(11),…F(15)} in order to convert from binary representation to hexadecimal Represents that the bit string ID is grouped into 4-bit chunks, called nibbles starting from the least significant side. Each block is replaced with the corresponding hexadecimal number. Let us see an example to get a clear understanding of hexadecimal and binary number representation. 001111100101101100011101 3 E 5 B&nb

How to read binary files in Golang? Binary files are files stored in binary form that contain data that a computer can recognize and process. In Golang, we can use some methods to read binary files and parse them into the data format we want. The following will introduce how to read binary files in Golang and give specific code examples. First, we need to open a binary file using the Open function from the os package, which will return a file object. Then we can make

The main reasons why computers use binary systems: 1. Computers are composed of logic circuits. Logic circuits usually only have two states, the switch is on and off, and these two states can be represented by "1" and "0"; 2. Only two numbers, 0 and 1, are used in the binary system, which is less error-prone during transmission and processing, thus ensuring high reliability of the computer.

Title: Easily learn to convert hexadecimal to binary in Go language. Specific code examples are required. In computer programming, conversion operations between different base numbers are often involved. Among them, conversion between hexadecimal and binary is relatively common. In the Go language, we can achieve hexadecimal to binary conversion through some simple code examples. Let us learn together. First, let's take a look at the representation methods of hexadecimal and binary. Hexadecimal is a method of representing numbers, using 0-9 and A-F to represent 1

There is no fixed limit to the length of an array in PHP, it can be dynamically adjusted according to the system's memory size. In PHP, an array is a very flexible data structure that can store any number of elements, and each element can be a value of any type, or even another array. The length limit of PHP arrays mainly depends on the memory size of the system and the memory limit of PHP configuration. Generally speaking, if the system's memory is large enough and PHP's memory limit is high enough, the length of the array can be very large. However, if your system is low on memory or

Negative numbers are represented in computers using two's complement, that is, negative numbers are represented by the two's complement of positive numbers.
