This section describes 4 types of bitwise operations, 'And', 'Or', 'Exclusive Or', and 'Complement' that applies on 'byte' values indirectly through 'int' values.
What is a bitwise operation? A bitwise operation is an operation that requires the operand(s) to be represented
in a binary format, and applies the operation one bit at a time.
Interestingly, Java defines bitwise operations on "int" data type values, not on "byte" data type values.
So a bitwise operation requires that the involved "int" values to be represented into a 32-bit binary format.
And the operation will be applied 1 bit at a time repeating 32 times.
1. Bitwise operation "And" - "&": The bitwise "And" operation between two "int" values can be described as:
Representing two operands, two "int" values, into two 32-bit long input binary strings.
Combining those two input binary strings into one 32-bit long output binary string, such that
the value of any bit in the output binary string is 1, if and only if both corresponding bits in the input binary strings are 1.
Converting the output binary string to an "int" value as the operation result.
The following diagram gives an example of "&" operations in 3 formats:
2. Bitwise operation "Or" - "|": The bitwise "Or" operation between two "int" values can be described as:
Representing two operands, two "int" values, into two 32-bit long input binary strings.
Combining those two input binary strings into one 32-bit long output binary string, such that
the value of any bit in the output binary string is 0, if and only if both corresponding bits in the input binary strings are 0.
Converting the output binary string to an "int" value as the operation result.
The following diagram gives an example of "|" operations in 3 formats:
3. Bitwise operation "Exclusive Or" - "^": The bitwise "Exclusive Or" operation between two "int" values can be described as:
Representing two operands, two "int" values, into two 32-bit long input binary strings.
Combining those two input binary strings into one 32-bit long output binary string, such that
the value of any bit in the output binary string is 1, if and only if one of the corresponding bits in the input binary strings is 1.
Converting the output binary string to an "int" value as the operation result.
The following diagram gives an example of "^" operations in 3 formats:
4. Bitwise operation "Complement" - "~": The bitwise "Complement" operation on one "int" value can be described as:
Representing the only operand, one "int" value, into one 32-bit long input binary string.
Derive from the input binary string into one 32-bit long output binary string, such that
the value of any bit in the output binary string is 1, if and only if the corresponding bit in the input binary string is 0.
Converting the output binary string to an "int" value as the operation result.
The following diagram gives an example of "~" operations in 3 formats: