Categories: Data Structure

DSA: Bits Manipulation

What is Bits Manipulation?

The process of modifying individual bits or groups of bits within a binary representation of data is known as bit manipulation. It is often used in computer programming to execute bit-level operations on data, which can be more efficient than typical arithmetic or logic operations.

What is the need for bit manipulation?

Bit manipulation is often used for a variety of reasons, including:

  1. Memory optimization: utilizing bit manipulation operations can reduce the amount of memory required to hold data, particularly when dealing with huge datasets.
  2. Faster execution: Because bit manipulation operations can be done directly on the binary representation of data, they can be faster than standard arithmetic or logic operations.
  3. Code optimization: This is especially beneficial in high-performance applications like video games and scientific simulations.
  4. Cryptography: Bit manipulation operations are commonly used in cryptographic algorithms for data encryption and decryption.
  5. Device programming: often used in low-level programming for devices such as microcontrollers, where memory and processing power are limited.

Following are the types of bitwise operators:

&bitwise AND
|bitwise OR
^bitwise XOR
~bitwise complement or NOT
<<left shift
>>signed right shift
>>>unsigned right shift
Truth Table:
ABA & BA | BA ^ B~A
000001
010111
100110
111100
Shift Operations:
  • Left shift (<<) operator: This operator shifts the binary representation of a number to the left by a specified number of bits. The leftmost bits are filled with zeros.
  • Right shift (>>) operator: This operator shifts the binary representation of a number to the right by a specified number of bits. The rightmost bits are filled with zeros (if the number is positive) or ones (if the number is negative).
  • Unsigned right shift (>>>) operator: This operator is similar to the right shift operator, but the leftmost bits are always filled with zeros, regardless of the sign of the number. This operator is useful when working with unsigned numbers.
ABA<<BA>>BA>>>B
25210066
-252-100-71073741817

Some common uses of bit manipulation include:

  • Checking if a number is even or odd: If the least significant bit is 0, the number is even. If it is 1, the number is odd. We can use the bitwise AND operator to check the least significant bit:
int num = 11; 
if ((num & 1) == 0) 
{ 
  System.out.println("Even"); 
} 
else 
{ 
  System.out.println("Odd"); 
}
  • Setting a bit: We can set a specific bit to 1 using the bitwise OR operator and a mask with a 1 in the desired bit position:
int num = 5; 
int mask = 1 << 2; // set the third bit to 1 
num |= mask; 
System.out.println(num); // prints 5 + 4 = 9
  • Clearing a bit: We can clear a specific bit to 0 using the bitwise AND operator and a mask with a 0 in the desired bit position:
int num = 13; 
int mask = ~(1 << 3); // clear the fourth bit 
num &= mask; 
System.out.println(num); // prints 13 - 8 = 5
  • Flipping a bit: We can flip a specific bit (change 0 to 1 or 1 to 0) using the bitwise XOR operator and a mask with a 1 in the desired bit position:
int num = 10; 
int mask = 1 << 1; // flip the second bit 
num ^= mask; 
System.out.println(num); // prints 10 - 2 = 8

Bit manipulation can also be used in various algorithms and data structures such as:

  • Bitsets: A bitset is a collection of bits that can be manipulated using bitwise operations. It is often used to represent a set of binary values efficiently. In Java, the java.util.BitSet class provides a convenient way to work with bitsets.
  • Counting the number of set bits (popcount): The popcount of a number is the number of 1 bits in its binary representation. There are various ways to compute the popcount efficiently using bitwise operations.
  • Bitwise operations on arrays: Bitwise operations can be used to perform operations on arrays of integers efficiently. For example, we can use bitwise OR to combine two arrays of integers element-wise.

Let us discuss the problems related to bit manipulation in the next topic.

Note: also read about DSA: Sliding Window Technique

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago