Caret in C
XOR only yields a value of 1 if the two bits are distinct. In C & C++, this binary operator is represented by the symbol caret (\). Additionally, in C and C++, this bitwise operator is shortened to XOR. Every pair of bits undergoes the exclusive-OR operation by it.
Commonly utilized XOR properties in coding are as follows:
Commutative property: According to the XOR operation's commutative property, the operands' order is irrelevant.
P ^ Q = Q ^ P
Associative property: According to the associative property of XOR, an XOR operation is independent of the operands' grouping.
(P ^ Q) ^ R = P ^ (Q ^ R)
Identity property: According to the identity feature of XOR, any value can be XORed with 0 to get the identical value back.
P ^ 0 = P
Negation property: According to the XOR's negation property, a zero value is returned when it is XORed with itself.
P ^ P = 0
Distributive property: XOR has the distributive property, which can be distributed across bitwise OR.
P ^ (Q | R) = (P ^ Q) | (P ^ R)
Bitwise complement property: A value can be XORed using a bitwise complement of 0 (all bit values set to 1) to obtain the value's bitwise complement, according to the bitwise complement property of XOR.
P ^ ~0 = ~P
Let us have a look at the program that makes use of caret for performing butwise XOR operations in C:
Program:
#include <stdio.h>
int main()
{
// p = 6(00000110), q = 4(00000100)
unsigned char p = 6, q = 4;
// The result is 00000010
printf("The value of p is %d and q is %d\n", p, q);
printf("p XOR q is %d\n", p ^ q);
return 0;
}
Output:
How is bitwise ^ (XOR) implemented?
Bitwise operators like XOR stand for "exclusive or." It carries out logical tasks. The output will be false(0) if all input bits are the same and true(1) otherwise.
XOR table:
P Q P^Q
0 0 0
0 1 1
1 0 1
1 1 0
Example: 5 ^ 3 = 6
In binary:
\0101
^ 0011
------
Result: 0110 => (6)
Negative numbers in an XOR
Here's an example to help you understand: -6^-3 = 7. We can see that the output of -6^-3 in the example above will be 7, but how? Because the output will be 0000 0101 if we represent both inputs in binary form and do an XOR of the bits; however, we know that the output should be 7.
1000 0110 (-6)
^ 1000 0011 (-3)
------------------
0000 0101 => (5) // wrong output
Note: Here, the value of the number is not affected by the leftmost bit position, which is reserved for the value's sign (positive or negative).
Let's examine how the XOR function functions in the case of negative values.
How does an XOR operation with a negative number operate?
Initially, you must transform the integer into its complement before performing the XOR operation on every single bit (the same is 0, the difference is 1).
• A positive number's complement is the number itself.
• For every bit, the complement of the negative number is flipped, and the greatest number is maintained at 1.
// Let us consider -6
In binary: 1000 0110
Reverse: 1111 1001
complement (Increment by 1): 1111 1010
Final result:
complement of -5: 1111 1010
complement of -3: 1111 1101
-----------
Result: 0000 0111 => 7
Here, the value of the final result will be indicated by the remaining bits, while the sign will be indicated by the MSB bit of the result. To comprehend the sign of the outcome, use the XOR sign table:
P Q P^Q
+ + +
+ - -
- + -
- - +
The technique mentioned earlier will function with negative inputs, but what happens if we also have positive inputs?
How do positive and negative values get combined in an XOR operation?
Let's execute -4 ^ 3. Utilize the same method as before to obtain a complement code of -4.
complement of -4: 1111 1100
// Note: The positive number's complement is itself.
complement of 3: 0000 0011
complement result: 1111 1111
Now, complement result (-1): 1111 1110
-----------
Final result(inverse code): 1000 0001 => -1