Bitwise Operators
What the &|^~!
In addition to shift operators, we have bitwise AND, OR, NOT, and XOR operators.
To continue this analogy, you can think of 0 as false and 1 as true.
Bitwise AND (&)
For bitwise AND, we take the two bit patterns we are interested and AND bits at corresponding indices. In other words, we will look at bits at corresponding indices, and if the bits are both 1, the corresponding bit in the output bit pattern will be 1. Otherwise, the bit in the corresponding bit pattern will be 0.
0b10100110 & 0b01101011 = 0b00100010
Bitwise OR (|)
For bitwise OR, which is represented by the pipe character, we take the two bit patterns given as arguments to the bitwise OR. If at least one of the bits at a certain index is 1, then the bit at the output index will also be 1. Otherwise, the bit in the output bit pattern will be 0.
0b10101000 | 0b11001100 = 0b11101100
Bitwise NOT (~)
Bitwise NOT (unlike AND, OR, and XOR) only operates on a single bit pattern. It takes the input bit pattern and flips all the bits.
~ 0b10100110 = 0b01011001
Bitwise XOR (^)
Bitwise XOR, aka "exclusive OR", introduces some new logic. XOR will only return 1 if exactly one of the bits is 1. Otherwise, the return bit is 0.
0b1100 ^ 0b1010 = 0b0110
Activity: bitwiseXOR in Nom
Complete the bitwiseXOR method in the Nom class. It takes in another Nom and XORs the two Nom-represented numbers to return a new Nom.
Alternative Symbols
You may see the following symbols used to denote bitwise operations:
AND
OR
NOT ¬
XOR ⊕ or +