VOOZH about

URL: https://www.geeksforgeeks.org/dsa/bits-manipulation-important-tactics/

⇱ Bits manipulation (Important tactics) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Bits manipulation (Important tactics)

Last Updated : 7 May, 2024

Prerequisites: Bitwise operators in C, Bitwise Hacks for Competitive Programming, Bit Tricks for Competitive Programming

Table of Contents

1. Compute XOR from 1 to n (direct method):

The  problem can be solved based on the following observations:

Say x = n%4. The XOR value depends on the value if x. If

  • x = 0, then the answer is n.
  • x = 1, then answer is 1.
  • x = 2, then answer is n+1.
  • x = 3, then answer is 0.

Below is the implementation of the above approach.

Time Complexity: O(1)
Auxiliary Space: O(1)

Refer Compute XOR from 1 to n for details.

2. Count of numbers (x) smaller than or equal to n such that n+x = n^x:

The count of such numbers x can be counted using the following mathematical trick. 

The count = pow(2, count of zero bits).


Output
1

Refer Equal Sum and XOR for details.

Time complexity: O(log n)

Auxiliary Space: O(1)

3. How to know if a number is a power of 2?

This can be solved based on the following fact:

If a number N is a power of 2, then the bitwise AND of N and N-1 will be 0. But this will not work if N is 0. So just check these two conditions, if any of these two conditions is true.

Refer check if a number is power of two for details.

Below is the implementation of the above approach.

Time Complexity: O(1)
Auxiliary Space: O(1)

4. Find XOR of all subsets of a set

We can do it in O(1) time. The answer is always 0 if the given set has more than one element. For sets with a single element, the answer is the value of the single element. 

Refer XOR of the XOR’s of all subsets for details.

5. Find the number of leading, trailing zeroes and number of 1’s

We can quickly find the number of leading, trailing zeroes and number of 1’s in a binary code of an integer in C++ using GCC. 

It can be done by using inbuilt functions i.e.

Number of leading zeroes: __builtin_clz(x)
Number of trailing zeroes : __builtin_ctz(x)
Number of 1-bits: __builtin_popcount(x) 

Refer GCC inbuilt functions for details.

6. Convert binary code directly into an integer in C++


Output
3

Time Complexity: O(1)
Auxiliary Space: O(1)

7. The Quickest way to swap two numbers:

Two numbers can be swapped easily using the following bitwise operations:

a ^= b;
b ^= a; 
a ^= b;


Output
Before Swapping, a = 5 b = 7
After Swapping, a = 7 b = 5

Time Complexity: O(1)
Auxiliary Space: O(1)

Refer swap two numbers for more details.  

8. Finding the most significant set bit (MSB):

We can find the most significant set bit in O(1) time for a fixed size integer. For example below code is for 32-bit integer.  

Time Complexity: O(1)
Auxiliary Space: O(1)

Refer Find most significant set bit of a number for details. 

9. Check if a number has bits in an alternate pattern

We can quickly check if bits in a number are in an alternate pattern (like 101010). 

Compute bitwise XOR (XOR denoted using ^) of n and (n >> 1). If n has an alternate pattern, then n ^ (n >> 1) operation will produce a number having all bits set.

Below is the implementation of the above approach.

Time Complexity: O(1)
Auxiliary Space: O(1)

Refer check if a number has bits in alternate pattern for details.

πŸ‘ DSA Self Paced Course
DSA Self Paced Course


Comment
Article Tags:
Article Tags: