Bits manipulation (Important tactics) - GeeksforGeeks

07 Feb, 2022
Prerequisites : Bitwise operators in C, Bitwise Hacks for Competitive Programming, Bit Tricks for Competitive Programming 
Compute XOR from 1 to n (direct method) :  
Compute XOR from 1 to n (direct method) :  
CPP
Javascript
// Direct XOR of all numbers from 1 to nint 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;}
<script> // Direct XOR of all numbers from 1 to nfunction computeXOR(n){    if (n % 4 == 0)        return n;    if (n % 4 == 1)        return 1;    if (n % 4 == 2)        return n + 1;    else        return 0;} // This code is contributed by Shubham Singh </script>
 
 
Input: 6
Output: 7
Refer Compute XOR from 1 to n for details.We can quickly calculate the total number of combinations with numbers smaller than or equal to a number whose sum and XOR are equal. Instead of using looping (Brute force method), we can directly find it by a mathematical trick i.e.  
Refer Compute XOR from 1 to n for details.
We can quickly calculate the total number of combinations with numbers smaller than or equal to a number whose sum and XOR are equal. Instead of using looping (Brute force method), we can directly find it by a mathematical trick i.e.  
// Refer Equal Sum and XOR for details.
Answer = pow(2, count of zero bits)
How to know if a number is a power of 2?  
How to know if a number is a power of 2?  
CPP
//  Function to check if x is power of 2bool isPowerOfTwo(int x){     // First x in the below expression is     // for  the case when x is 0     return x && (!(x & (x - 1)));}
Refer check if a number is power of two for details.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 single element. Refer XOR of the XOR’s of all subsets for details.We can quickly find 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 function i.e.
Refer check if a number is power of two for details.
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 single element. Refer XOR of the XOR’s of all subsets for details.
We can quickly find 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 function 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.Convert binary code directly into an integer in C++.  
Refer GCC inbuilt functions for details.
Convert binary code directly into an integer in C++.  
CPP
// Conversion into Binary code//#include <iostream>using namespace std; int main(){    auto number = 0b011;    cout << number;    return 0;}
 
 
Output: 3
The Quickest way to swap two numbers:  
The Quickest way to swap two numbers:  
a ^= b;
b ^= a; 
a ^= b;
Refer swap two numbers for details.  Simple approach to flip the bits of a number: It can be done in a simple way, just simply subtract the number from the value obtained when all the bits are equal to 1. For example:  
Refer swap two numbers for details.  
Simple approach to flip the bits of a number: It can be done in a simple way, just simply subtract the number from the value obtained when all the bits are equal to 1. For example:  
Number : Given Number
Value  : A number with all bits set in given number.
Flipped number = Value – Number.

Example : 
Number = 23,
Binary form: 10111;
After flipping digits number will be: 01000;
Value: 11111 = 31;
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.  
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.  We can quickly check if bits in a number are in alternate pattern (like 101010). We compute n ^ (n >> 1). If n has an alternate pattern, then n ^ (n >> 1) operation will produce a number having set bits only. ‘^’ is a bitwise XOR operation. Refer check if a number has bits in alternate pattern for details.
Refer Find most significant set bit of a number for details.  
We can quickly check if bits in a number are in alternate pattern (like 101010). We compute n ^ (n >> 1). If n has an alternate pattern, then n ^ (n >> 1) operation will produce a number having set bits only. ‘^’ is a bitwise XOR operation. Refer check if a number has bits in alternate pattern for details.
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. 
kkvanonymous
SHUBHAMSINGH10
Bit Magic
Competitive Programming
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bit Fields in C
Set, Clear and Toggle a given bit of a number in C
1's and 2's complement of a Binary Number
Divide two integers without using multiplication, division and mod operator
Check whether K-th bit is set or not
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Top 10 Algorithms and Data Structures for Competitive Programming
Modulo 10^9+7 (1000000007)