Bitwise Operations on "byte" Values

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.

Java defines 4 bitwise operations called: "And", "Or", "Exclusive Or", and "Complement"

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:

```  Decimal        Hex          Binary
858993459     33333333     00110011 00110011 00110011 00110011
& 1431655765   & 55555555   & 01010101 01010101 01010101 01010101
------------   ----------   -------------------------------------
=  286331153   = 11111111   = 00010001 00010001 00010001 00010001
```

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:

```  Decimal        Hex          Binary
858993459     33333333     00110011 00110011 00110011 00110011
| 1431655765   | 55555555   | 01010101 01010101 01010101 01010101
------------   ----------   -------------------------------------
= 2004318071   = 77777777   = 01110111 01110111 01110111 01110111
```

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:

```  Decimal        Hex          Binary
858993459     33333333     00110011 00110011 00110011 00110011
^ 1431655765   ^ 55555555   ^ 01010101 01010101 01010101 01010101
------------   ----------   -------------------------------------
= 1717986918   = 66666666   = 01100110 01100110 01100110 01100110
```

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:

```  Decimal        Hex          Binary
~  858993459   ~ 33333333   ~ 00110011 00110011 00110011 00110011
------------   ----------   -------------------------------------
= 3233857728   = C0C0C0C0   = 11001100 11001100 11001100 11001100
```

Last update: 2014.

Table of Contents