|
1 | 1 | Bitwise XOR
|
2 |
| -=========== |
| 2 | +============ |
3 | 3 |
|
4 | 4 | - Introduction:
|
| 5 | +=============== |
5 | 6 |
|
6 |
| -Bitwise XOR (exclusive OR) is a binary operation that operates on the individual bits of its operands. It is a fundamental |
7 |
| -operation in digital logic and computer science, commonly used in tasks like encryption, error detection, and data manipulation. |
| 7 | + Bitwise XOR (exclusive OR) is a binary operation that operates on the individual bits of its operands. It is a fundamental |
| 8 | + operation in digital logic and computer science, commonly used in tasks like encryption, error detection, and data manipulation. |
8 | 9 |
|
9 |
| -- How Bitwise XOR Works |
| 10 | + - How Bitwise XOR Works: |
| 11 | + ------------------------ |
10 | 12 |
|
11 |
| -Bitwise XOR compares corresponding bits of two numbers and returns a new number where each bit is set to 1 if the corresponding |
12 |
| -bits of the operands are different, and 0 if they are the same. |
| 13 | + Bitwise XOR compares corresponding bits of two numbers and returns a new number where each bit is set to 1 if the corresponding |
| 14 | + bits of the operands are different, and 0 if they are the same. |
13 | 15 |
|
14 |
| -Here's a bit-by-bit comparison for clarification: |
| 16 | + Here's a bit-by-bit comparison for clarification: |
15 | 17 |
|
16 |
| -- 0 XOR 0 = 0 |
17 |
| -- 0 XOR 1 = 1 |
18 |
| -- 1 XOR 0 = 1 |
19 |
| -- 1 XOR 1 = 0 |
| 18 | + - 0 XOR 0 = 0 |
| 19 | + - 0 XOR 1 = 1 |
| 20 | + - 1 XOR 0 = 1 |
| 21 | + - 1 XOR 1 = 0 |
20 | 22 |
|
21 |
| -- Example Calculation |
| 23 | + - Example Calculation: |
| 24 | + ---------------------- |
22 | 25 |
|
23 |
| -Consider the bitwise XOR of the binary numbers `1010` and `1100`: |
| 26 | + Consider the bitwise XOR of the binary numbers `1010` and `1100`: |
24 | 27 |
|
25 |
| - 1010 |
26 |
| -XOR 1100 |
27 |
| ---------- |
28 |
| - 0110 |
| 28 | + 1010 |
| 29 | + XOR 1100 |
| 30 | + --------- |
| 31 | + 0110 |
29 | 32 |
|
30 |
| -- The first bit: 1 XOR 1 = 0 |
31 |
| -- The second bit: 0 XOR 1 = 1 |
32 |
| -- The third bit: 1 XOR 0 = 1 |
33 |
| -- The fourth bit: 0 XOR 0 = 0 |
| 33 | + - The first bit: 1 XOR 1 = 0 |
| 34 | + - The second bit: 0 XOR 1 = 1 |
| 35 | + - The third bit: 1 XOR 0 = 1 |
| 36 | + - The fourth bit: 0 XOR 0 = 0 |
34 | 37 |
|
35 |
| -The result is `0110`. |
| 38 | + The result is `0110`. |
36 | 39 |
|
37 |
| -- Properties of Bitwise XOR |
| 40 | + - Properties of Bitwise XOR: |
| 41 | + ---------------------------- |
38 | 42 |
|
39 |
| -1. Commutative: `A XOR B = B XOR A` |
40 |
| -2. Associative: `(A XOR B) XOR C = A XOR (B XOR C)` |
41 |
| -3. Identity: `A XOR 0 = A` |
42 |
| -4. Self-inverse: `A XOR A = 0` |
| 43 | + 1. Commutative: `A XOR B = B XOR A` |
| 44 | + 2. Associative: `(A XOR B) XOR C = A XOR (B XOR C)` |
| 45 | + 3. Identity: `A XOR 0 = A` |
| 46 | + 4. Self-inverse: `A XOR A = 0` |
43 | 47 |
|
44 |
| -- Applications |
| 48 | + - Applications: |
| 49 | + --------------- |
45 | 50 |
|
46 |
| -1. Swapping Values: XOR can be used to swap two values without using a temporary variable: |
| 51 | + 1. Swapping Values: XOR can be used to swap two values without using a temporary variable: |
47 | 52 |
|
48 |
| - a = a XOR b |
49 |
| - b = a XOR b |
50 |
| - a = a XOR b |
| 53 | + a = a XOR b |
| 54 | + b = a XOR b |
| 55 | + a = a XOR b |
51 | 56 |
|
52 |
| -2. Encryption and Decryption: In some simple encryption algorithms, a key is XORed with the plaintext to produce ciphertext. |
53 |
| -3. Checksum and Error Detection: XOR is used in generating parity bits for error detection in data transmission. |
| 57 | + 2. Encryption and Decryption: In some simple encryption algorithms, a key is XORed with the plaintext to produce |
| 58 | + ciphertext. |
| 59 | + 3. Checksum and Error Detection: XOR is used in generating parity bits for error detection in data transmission. |
54 | 60 |
|
55 |
| -Bitwise XOR is a versatile and efficient operation widely used in low-level programming and computer science for its |
56 |
| -unique properties and simplicity. |
| 61 | + Bitwise XOR is a versatile and efficient operation widely used in low-level programming and computer science for its |
| 62 | + unique properties and simplicity. |
57 | 63 |
|
58 |
| -Certainly! Here’s a high-level pseudocode for performing a Bitwise XOR (exclusive OR) operation between two integers: |
| 64 | + - Implementation: |
| 65 | + ----------------- |
59 | 66 |
|
60 |
| -- Pseudocode |
| 67 | + In Java, the bitwise XOR operator is represented by the caret symbol (`^`). It operates on the individual bits of |
| 68 | + its operands and returns a new integer whose bits are set according to the XOR logic. |
61 | 69 |
|
62 |
| -Function BitwiseXOR(a, b): |
63 |
| - Initialize result to 0 |
64 |
| - Initialize bitPosition to 0 |
| 70 | + * Syntax |
65 | 71 |
|
66 |
| - While a is not 0 or b is not 0: |
67 |
| - // Extract the least significant bit of a and b |
68 |
| - bitA = a AND 1 |
69 |
| - bitB = b AND 1 |
| 72 | + result = operand1 ^ operand2; |
70 | 73 |
|
71 |
| - // Perform XOR on the extracted bits |
72 |
| - xorBit = bitA XOR bitB |
| 74 | + * Example Usage |
73 | 75 |
|
74 |
| - // Place the xorBit in the correct bit position in the result |
75 |
| - result = result OR (xorBit << bitPosition) |
| 76 | + Here's an example demonstrating the use of the bitwise XOR operator in Java: |
76 | 77 |
|
77 |
| - // Shift a and b to the right to process the next bit |
78 |
| - a = a >> 1 |
79 |
| - b = b >> 1 |
| 78 | + public class BitwiseXORExample { |
| 79 | + public static void main(String[] args) { |
| 80 | + int a = 10; // in binary: 1010 |
| 81 | + int b = 12; // in binary: 1100 |
80 | 82 |
|
81 |
| - // Move to the next bit position |
82 |
| - bitPosition = bitPosition + 1 |
| 83 | + // Perform bitwise XOR |
| 84 | + int result = a ^ b; |
83 | 85 |
|
84 |
| - Return result |
| 86 | + // Print the result |
| 87 | + System.out.println("a ^ b = " + result); // Outputs 6, which is 0110 in binary |
| 88 | + } |
| 89 | + } |
85 | 90 |
|
86 |
| -- Explanation |
| 91 | + * Detailed Breakdown |
87 | 92 |
|
88 |
| -1. Function Signature: Define a function `BitwiseXOR` that takes two integers, `a` and `b`. |
89 |
| -2. Initialize Variables: |
90 |
| - - `result` to store the final result of the XOR operation. |
91 |
| - - `bitPosition` to keep track of the current bit position being processed. |
92 |
| -3. Loop Until Both Numbers Are Zero: Use a `while` loop to process each bit of `a` and `b` until both numbers become zero. |
93 |
| -4. Extract the Least Significant Bits: Use the bitwise AND operation to extract the least significant bit of `a` and `b`. |
94 |
| -5. Perform XOR on the Extracted Bits: Use the XOR operation to compute the XOR of the extracted bits. |
95 |
| -6. Update the Result: Place the `xorBit` in the correct position in the `result` using bitwise OR and left shift. |
96 |
| -7. Shift `a` and `b` Right: Move to the next bit by shifting `a` and `b` to the right. |
97 |
| -8. Update the Bit Position: Increment the `bitPosition` to reflect the shift. |
98 |
| -9. Return the Result: After processing all bits, return the final `result`. |
| 93 | + Let's break down the example: |
99 | 94 |
|
100 |
| -This pseudocode outlines the process of performing a bitwise XOR operation bit by bit. It ensures that each bit of the |
101 |
| -two input numbers is XORed and the result is constructed correctly. |
| 95 | + - `a = 10` which is `1010` in binary. |
| 96 | + - `b = 12` which is `1100` in binary. |
| 97 | + - Performing `a ^ b` (bitwise XOR): |
102 | 98 |
|
103 |
| -- Implementation |
| 99 | + 1010 |
| 100 | + ^ 1100 |
| 101 | + ------ |
| 102 | + 0110 |
104 | 103 |
|
105 |
| -In Java, the bitwise XOR operator is represented by the caret symbol (`^`). It operates on the individual bits of its operands |
106 |
| -and returns a new integer whose bits are set according to the XOR logic. |
| 104 | + - The result is `6`, which is `0110` in binary. |
107 | 105 |
|
108 |
| -- Syntax |
| 106 | + * Swapping Two Numbers Using XOR |
109 | 107 |
|
110 |
| -result = operand1 ^ operand2; |
| 108 | + One of the classic uses of the XOR operator is swapping two numbers without using a temporary variable: |
111 | 109 |
|
112 |
| -- Example Usage |
| 110 | + public class XORSwapExample { |
| 111 | + public static void main(String[] args) { |
| 112 | + int x = 5; // in binary: 0101 |
| 113 | + int y = 9; // in binary: 1001 |
113 | 114 |
|
114 |
| -Here's an example demonstrating the use of the bitwise XOR operator in Java: |
| 115 | + // Display original values |
| 116 | + System.out.println("Before swapping: x = " + x + ", y = " + y); |
115 | 117 |
|
116 |
| -public class BitwiseXORExample { |
117 |
| - public static void main(String[] args) { |
118 |
| - int a = 10; // in binary: 1010 |
119 |
| - int b = 12; // in binary: 1100 |
| 118 | + // Swap using XOR |
| 119 | + x = x ^ y; |
| 120 | + y = x ^ y; |
| 121 | + x = x ^ y; |
120 | 122 |
|
121 |
| - // Perform bitwise XOR |
122 |
| - int result = a ^ b; |
| 123 | + // Display swapped values |
| 124 | + System.out.println("After swapping: x = " + x + ", y = " + y); |
| 125 | + } |
| 126 | + } |
123 | 127 |
|
124 |
| - // Print the result |
125 |
| - System.out.println("a ^ b = " + result); // Outputs 6, which is 0110 in binary |
126 |
| - } |
127 |
| -} |
| 128 | + - Practical Applications: |
| 129 | + ------------------------- |
128 | 130 |
|
129 |
| -- Detailed Breakdown |
| 131 | + 1. Toggle Bits: XOR can be used to toggle specific bits in a number. |
| 132 | + 2. Checksum Calculation: XOR is used in algorithms to compute checksums for error detection. |
| 133 | + 3. Encryption/Decryption: XOR is used in simple encryption algorithms where the same key is used to encrypt and |
| 134 | + decrypt data. |
130 | 135 |
|
131 |
| -Let's break down the example: |
| 136 | + - Conclusion: |
| 137 | + ------------- |
132 | 138 |
|
133 |
| -- `a = 10` which is `1010` in binary. |
134 |
| -- `b = 12` which is `1100` in binary. |
135 |
| -- Performing `a ^ b` (bitwise XOR): |
| 139 | + The bitwise XOR operator in Java is a powerful tool for bit-level manipulation, enabling efficient and concise |
| 140 | + operations that are fundamental in many areas of programming and computer science. |
136 | 141 |
|
137 |
| - 1010 |
138 |
| -^ 1100 |
139 |
| ------- |
140 |
| - 0110 |
| 142 | +- Bitwise XOR Example LeetCode Question: 1835. Find XOR Sum of All Pairs Bitwise AND |
| 143 | +==================================================================================== |
141 | 144 |
|
142 |
| -- The result is `6`, which is `0110` in binary. |
| 145 | + To solve the problem efficiently, let's break down the operations and utilize properties of the XOR and AND bitwise |
| 146 | + operations to reduce the computational complexity. |
143 | 147 |
|
144 |
| -- Swapping Two Numbers Using XOR |
| 148 | + * Problem Explanation: |
| 149 | + ---------------------- |
145 | 150 |
|
146 |
| -One of the classic uses of the XOR operator is swapping two numbers without using a temporary variable: |
| 151 | + Given two arrays `arr1` and `arr2`, we need to consider every possible pair `(i, j)` where `0 <= i < arr1.length` |
| 152 | + and `0 <= j < arr2.length`. For each pair, we calculate `arr1[i] AND arr2[j]`, and then we need to find the XOR sum |
| 153 | + of all these results. |
147 | 154 |
|
148 |
| -public class XORSwapExample { |
149 |
| - public static void main(String[] args) { |
150 |
| - int x = 5; // in binary: 0101 |
151 |
| - int y = 9; // in binary: 1001 |
| 155 | + * Observations: |
| 156 | + --------------- |
152 | 157 |
|
153 |
| - // Display original values |
154 |
| - System.out.println("Before swapping: x = " + x + ", y = " + y); |
| 158 | + 1. AND and XOR properties: |
| 159 | + - XOR is both commutative and associative, which means the order of operations does not matter. |
| 160 | + - AND operation is distributive over XOR. |
155 | 161 |
|
156 |
| - // Swap using XOR |
157 |
| - x = x ^ y; |
158 |
| - y = x ^ y; |
159 |
| - x = x ^ y; |
| 162 | + 2. Decomposing the problem: |
| 163 | + - Instead of calculating every `arr1[i] AND arr2[j]` and then taking the XOR, we can leverage the distributive |
| 164 | + property of the AND and XOR operations. |
160 | 165 |
|
161 |
| - // Display swapped values |
162 |
| - System.out.println("After swapping: x = " + x + ", y = " + y); |
163 |
| - } |
164 |
| -} |
| 166 | + * Efficient Calculation: |
| 167 | + ------------------------ |
165 | 168 |
|
166 |
| -- Practical Applications |
| 169 | + Let's consider how the bitwise operations interact: |
167 | 170 |
|
168 |
| -1. Toggle Bits: XOR can be used to toggle specific bits in a number. |
169 |
| -2. Checksum Calculation: XOR is used in algorithms to compute checksums for error detection. |
170 |
| -3. Encryption/Decryption: XOR is used in simple encryption algorithms where the same key is used to encrypt and decrypt data. |
| 171 | + For any bit position ( k ): |
171 | 172 |
|
172 |
| -- Conclusion |
| 173 | + - For each number in `arr1`, determine how many numbers in `arr2` have the ( k )-th bit set and how many do not. |
| 174 | + - The XOR sum for a bit position ( k ) is determined by whether the number of set bits in `arr1` and `arr2` at |
| 175 | + position ( k ) are odd or even. |
173 | 176 |
|
174 |
| -The bitwise XOR operator in Java is a powerful tool for bit-level manipulation, enabling efficient and concise operations that |
175 |
| -are fundamental in many areas of programming and computer science. |
| 177 | + If we denote: |
| 178 | + |
| 179 | + - `count1_k` as the number of elements in `arr1` where the ( k )-th bit is set. |
| 180 | + - `count2_k` as the number of elements in `arr2` where the ( k )-th bit is set. |
| 181 | + |
| 182 | + The contribution to the XOR sum from the \( k \)-th bit is significant only if `count1_k * count2_k` is odd. |
| 183 | + This is because an even number of `1` bits in a position will cancel out in XOR, whereas an odd number will result in a `1`. |
| 184 | + |
| 185 | + * Implementation: |
| 186 | + ----------------- |
| 187 | + |
| 188 | + Let's implement this idea in Java: |
| 189 | + |
| 190 | + public class Solution { |
| 191 | + public int getXORSum(int[] arr1, int[] arr2) { |
| 192 | + int xor1 = 0, xor2 = 0; |
| 193 | + |
| 194 | + // Compute the XOR of all elements in arr1 |
| 195 | + for (int num : arr1) { |
| 196 | + xor1 ^= num; |
| 197 | + } |
| 198 | + |
| 199 | + // Compute the XOR of all elements in arr2 |
| 200 | + for (int num : arr2) { |
| 201 | + xor2 ^= num; |
| 202 | + } |
| 203 | + |
| 204 | + // The result is the AND of the two XORs |
| 205 | + return xor1 & xor2; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + * Explanation: |
| 210 | + -------------- |
| 211 | + |
| 212 | + 1. Compute `xor1`: This is the XOR of all elements in `arr1`. |
| 213 | + 2. Compute `xor2`: This is the XOR of all elements in `arr2`. |
| 214 | + 3. Result: The XOR sum of all `arr1[i] AND arr2[j]` pairs is equivalent to `(xor1 & xor2)`. |
| 215 | + |
| 216 | + This approach ensures that we compute the required XOR sum in O(n + m) time complexity, where ( n ) and ( m ) are the |
| 217 | + lengths of `arr1` and `arr2`, respectively. This is efficient and avoids the need to explicitly compute and store all |
| 218 | + possible pairs. |
0 commit comments