# Bits manipulation (Important tactics)

• Difficulty Level : Medium
• Last Updated : 10 Aug, 2022

## 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.

## CPP

 `// Direct XOR of all numbers from 1 to n ` `int` `computeXOR(``int` `n) ` `{ ` `    ``if` `(n % 4 == 0) ` `        ``return` `n; ` `    ``if` `(n % 4 == 1) ` `        ``return` `1; ` `    ``if` `(n % 4 == 2) ` `        ``return` `n + 1; ` `    ``else` `        ``return` `0; ` `} `

## Java

 `/*package whatever //do not write package name here */` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` `   `  `  ``// Direct XOR of all numbers from 1 to n ` `  ``public` `static` `int` `computeXOR(``int` `n) ` `  ``{ ` `    ``if` `(n % ``4` `== ``0``) ` `      ``return` `n; ` `    ``if` `(n % ``4` `== ``1``) ` `      ``return` `1``; ` `    ``if` `(n % ``4` `== ``2``) ` `      ``return` `n + ``1``; ` `    ``else` `      ``return` `0``; ` `  ``} ` ` `  `  ``public` `static` `void` `main (String[] args) { ` ` `  `  ``} ` `} ` ` `  `// This code is contributed by akashish__ `

## Python3

 `# Direct XOR of all numbers from 1 to n ` `def` `computeXOR(n): ` `    ``if` `(n ``%` `4` `is` `0``): ` `        ``return` `n ` `    ``if` `(n ``%` `4` `is` `1``): ` `        ``return` `1` `    ``if` `(n ``%` `4` `is` `2``): ` `        ``return` `n ``+` `1` `    ``else``: ` `        ``return` `0` ` `  `       `  `# This code is contributed by akashish__ `

## C#

 `using` `System; ` `public` `class` `GFG ` `{ ` ` `  `  ``// Direct XOR of all numbers from 1 to n ` `  ``public` `static` `int` `computeXOR(``int` `n) ` `  ``{ ` ` `  `    ``if` `(n % 4 == 0) ` ` `  `      ``return` `n; ` ` `  `    ``if` `(n % 4 == 1) ` ` `  `      ``return` `1; ` ` `  `    ``if` `(n % 4 == 2) ` ` `  `      ``return` `n + 1; ` ` `  `    ``else` ` `  `      ``return` `0; ` ` `  `  ``} ` `  ``public` `static` `void` `Main(){} ` ` `  ` `  `} ` ` `  `// This code is contributed by akashish__ `

## Javascript

 ` `

```Input: 6
Output: 7```

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).

Refer Equal Sum and XOR for details.

## 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.

## CPP

 `//  Function to check if x is power of 2 ` `bool` `isPowerOfTwo(``int` `x) ` `{ ` `     ``// First x in the below expression is ` `     ``// for  the case when x is 0  ` `     ``return` `x && (!(x & (x - 1))); ` `} `

## Python3

 `#  Function to check if x is power of 2 ` `def` `isPowerOfTwo(x): ` `   `  `  ``# First x in the below expression is ` `  ``# for  the case when x is 0  ` `  ``return` `x ``and` `(``not``(x & (x ``-` `1``))) ` ` `  `# This code is contributed by akashish__ `

## C#

 `using` `System; ` ` `  `public` `class` `GFG{ ` `   `  `  ``//  Function to check if x is power of 2 ` `static` `public` `bool` `isPowerOfTwo(``int` `x) ` `{ ` `     ``// First x in the below expression is ` `     ``// for  the case when x is 0  ` `       ``return` `(x != 0) && ((x & (x - 1)) == 0); ` `} ` ` `  `    ``static` `public` `void` `Main (){ ` ` `  `    ``} ` `} ` ` `  `// This code is contributed by akashish__`

## 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.

## CPP

 `// Conversion into Binary code ` ` `  `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``auto` `number = 0b011; ` `    ``cout << number; ` `    ``return` `0; ` `}`

Output

`3`

## 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;

Refer swap two numbers for more details.

## 9. 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.

## C++

 `int` `setBitNumber(``int` `n) ` `{ ` `    ``// Below steps set bits after ` `    ``// MSB (including MSB) ` ` `  `    ``// Suppose n is 273 (binary ` `    ``// is 100010001). It does following ` `    ``// 100010001 | 010001000 = 110011001 ` `    ``n |= n >> 1; ` ` `  `    ``// This makes sure 4 bits ` `    ``// (From MSB and including MSB) ` `    ``// are set. It does following ` `    ``// 110011001 | 001100110 = 111111111 ` `    ``n |= n >> 2; ` ` `  `    ``n |= n >> 4; ` `    ``n |= n >> 8; ` `    ``n |= n >> 16; ` ` `  `    ``// Increment n by 1 so that ` `    ``// there is only one set bit ` `    ``// which is just before original ` `    ``// MSB. n now becomes 1000000000 ` `    ``n = n + 1; ` ` `  `    ``// Return original MSB after shifting. ` `    ``// n now becomes 100000000 ` `    ``return` `(n >> 1); ` `}`

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

## 10. 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.

## C++

 `// Function to check if a number  ` `// has bits in alternate pattern ` `bool` `bitsAreInAltOrder(unsigned ``int` `n) ` `{ ` `    ``unsigned ``int` `num = n ^ (n >> 1); ` `     `  `    ``// To check if all bits are set in 'num' ` `    ``return` `allBitsAreSet(num); ` `}`

Refer check if a number has bits in alternate pattern for details. DSA Self Paced Course

This article is contributed by Sanchit Garg 1. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :