Bit Manipulation in Java: Get, Set, Clear, and Update Bit Operations Explained

What is Bit Manipulation?

Bit manipulation involves operating on individual bits of a binary number. It's a powerful technique used to optimize algorithms and reduce time complexity, especially important for advanced placement questions and computer programming.

Core Concepts

Four Major Bit Operations

1. Get Bit

Purpose: Check if a specific bit (at a given position) is 0 or 1.

Process:

  1. Create a bitmask by left-shifting 1 by the target position.
  2. Perform AND operation between the original number and the bitmask.
  3. If the result is non-zero, the bit is 1; if zero, the bit is 0.

Example:

  • Number: 0101 (binary for 5)
  • Position: 2
  • Bitmask: 1 << 2 = 0100
  • AND operation: 0101 & 0100 = 0100 (non-zero, so bit is 1)

2. Set Bit

Purpose: Change a specific bit to 1.

Process:

  1. Create a bitmask by left-shifting 1 by the target position.
  2. Perform OR operation between the original number and the bitmask.

Example:

  • Number: 0101 (binary for 5)
  • Position: 1
  • Bitmask: 1 << 1 = 0010
  • OR operation: 0101 | 0010 = 0111 (decimal 7)

3. Clear Bit

Purpose: Change a specific bit to 0.

Process:

  1. Create a bitmask by left-shifting 1 by the target position.
  2. Take the bitwise NOT (~) of the bitmask.
  3. Perform AND operation between the original number and the NOTted bitmask.

Example:

  • Number: 1001 (binary for 9)
  • Position: 2
  • Bitmask: 1 << 2 = 0100
  • NOT of bitmask: ~0100 = 1011 (in 4-bit representation)
  • AND operation: 1001 & 1011 = 1001 (binary for 9, but bit at position 2 was already 0? Let's recalculate: 1001 & 1011 = 1001, bit position 2 is 0. Wait - let's use a clearer example)

Clear Example (Corrected):

  • Number: 1111 (binary for 15)
  • Position: 2 (clear bit at position 2 to 0)
  • Bitmask: 1 << 2 = 0100
  • NOT of bitmask: ~0100 = 1011
  • AND operation: 1111 & 1011 = 1011 (bit 2 is now 0; result is decimal 11)

4. Update Bit

Purpose: Change a specific bit to either 0 or 1 based on requirement.

Process:

  1. Determine the desired value (0 or 1).
  2. If setting to 1: Use Set Bit operation (OR with bitmask).
  3. If setting to 0: Use Clear Bit operation (AND with NOT of bitmask).

Code Examples in Java

Get Bit Operation

int n = 5; // binary 0101
int pos = 2;
int bitmask = 1 << pos;
if ((bitmask & n) == 0) {
    System.out.println("Bit is 0");
} else {
    System.out.println("Bit is 1");
}

Set Bit Operation

int n = 5; // binary 0101
int pos = 1;
int bitmask = 1 << pos;
int newNumber = bitmask | n;
System.out.println(newNumber); // Output: 7

Clear Bit Operation

int n = 9; // binary 1001
int pos = 3;
int bitmask = 1 << pos;
int notBitmask = ~bitmask;
int newNumber = notBitmask & n;
System.out.println(newNumber);

Update Bit Operation

int n = 5; // binary 0101
int pos = 1;
int operation = 1; // 1 to set, 0 to clear

if (operation == 1) {
    // Set Bit
    int bitmask = 1 << pos;
    int newNumber = bitmask | n;
    System.out.println(newNumber); // Output: 7
} else {
    // Clear Bit
    int bitmask = 1 << pos;
    int notBitmask = ~bitmask;
    int newNumber = notBitmask & n;
    System.out.println(newNumber); // Would clear bit to 0
}

Key Logic Behind Operations

  • Get Bit: Since the mask has only one 1 at the target position, AND isolates that bit. If the original bit is 1, result is non-zero; if 0, result is zero.
  • Set Bit: OR operation ensures the target bit becomes 1 while keeping all other bits unchanged. For more on logical operations in Java, check out Understanding Java Short Circuit Operations: A Comprehensive Guide.
  • Clear Bit: By taking NOT of the mask, we create a mask with 0 only at the target position. AND then forces that bit to 0 while preserving others.
  • Update Bit: Combines Set or Clear based on the desired value.

Important Notes

  • Always calculate the bitmask first, then perform the operation with the original number.
  • Understanding the logic is more important than memorizing the operations for interviews and real-world applications.
  • If you're new to Java, consider reviewing Java Basics: Outputs, Variables, and User Input Explained first.

Keep this summary

Save it to LunaNotes and it becomes a real note in your library — editable, searchable, and ready to turn into flashcards or a diagram. Free to start.

Save to LunaNotes

Or summarise for another video.

This summary and transcript were automatically generated using AI with the Free YouTube Transcript Summary Tool by LunaNotes.

Related summaries

Bit Manipulation in Java: Get, Set, Clear & Update Bit Operations

Bit Manipulation in Java: Get, Set, Clear & Update Bit Operations

Learn bit manipulation in Java with this comprehensive guide covering 4 major bit operations: Get, Set, Clear, and Update. Understand bitmasking techniques, left/right shift operators, and see practical code examples to master placement and programming interview questions.

Understanding Java Short Circuit Operations: A Comprehensive Guide

Understanding Java Short Circuit Operations: A Comprehensive Guide

Explore Java short circuit operations, including examples and benefits for improved performance in programming.

Understanding Number Systems and Binary Conversion in Python

Understanding Number Systems and Binary Conversion in Python

This video tutorial explains the fundamentals of various number systems including binary, octal, decimal, and hexadecimal, focusing on their significance in programming. Learn how to convert decimal numbers to binary using Python's built-in functions and understand manual conversion techniques for accuracy and better comprehension. Practical examples and homework exercises enhance your grasp on number system conversions essential for programming tasks such as working with IP addresses and bitwise operations.

Understanding the Order of Operations in Mathematics

Understanding the Order of Operations in Mathematics

Learn the importance of the Order of Operations and how to solve math problems accurately.

Understanding Operators in C Programming: Types and Applications

Understanding Operators in C Programming: Types and Applications

This lecture introduces the concept of operators in C programming, explaining their essential role in performing calculations, comparisons, and logical decisions. It covers various categories of operators including arithmetic, relational, logical, bitwise, increment/decrement, assignment, and others, with practical examples illustrating their use.

Found this summary useful?

Take it with you. One click puts it in your own LunaNotes library.

Save to LunaNotes

Start taking better notes today with LunaNotes