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:

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:

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:

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:

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

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

Bits, Bytes, Bitwise and Shift Operations

 What Are Bits and Bytes

 "byte" Data Type and Implicit Casting

 Operations on "byte" Data Type Values

Bitwise Operations on "byte" Values

 Bitwise Operations on "byte" Values - Example Program

 Shift Operations - Left, Right or Unsigned Right

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB