Bit Manipulation
Bit Manipulation
Class striver
Type Lecture
Materials https://www.youtube.com/watch?v=5rtVTYAk9KQ&t=330s
Reviewed
Bit Manipulation
Binary
0 & 1
Decimal to Binary
2 |10 | 0
2 | 5 | 1
2 | 2 | 0
| 1
//so the in binary 10 is 1010
2 | 14 | 0
2 | 07 | 1
2 | 03 | 1
| 1
3 2 1 0 bits
1 0 1 1
--------
Bit Manipulation 1
//so for binary to decimal
//starting from the rightmost bit
/**
1 * 2 ^ 0 = 1
+ 1 * 2 ^ 1 = 2
+ 0 * 2 ^ 2 = 0
+ 1 * 2 ^ 3 = 8
-----------------
11
OPERATORS
‘&’ - and operator -> all 1 -> 1 a & b & c a = 5, b =7, c = 8
any 0 -> 0 0101 & 0111 & 1000 = 0000
'|' - OR operator -> any 1 -> 1 a = 101 b = 111
all 0 -> 0 101 | 111 = 111
'>>' - right shift operator -> last one bit will go off
a>>1 so for a = 5, 101 >> 1 is 10
the right shift means dividing by 2
so right shift by 3 means 2 ^ 3 div
the number by 8.
'<<' - left shift operator -> the first bit go off
5 << 2 means 0000....101 << 2
0000 ...10100
left shift of two means (5 * 2^2)
Q. Given an array of integers arr[] = {2,1,2,5,6,5,7,7,7,6} every integer occurs twice except
one number print the number.
#include<bits/stdc++.h>
using namespace std;
Bit Manipulation 2
return x;
}
int main(){
int arr[] = {2,1,2,5,6,5,7,7,6};
/**a = 5 b = 7
Step 1 : a = a ^ b ie. a = 5 ^ 7 , b = 7
step 2: b = a ^ b ie. b = 5 ^ 7 ^ 7 = 5, a = 5 ^ 7
step 3 : a = a ^ b ie. a = 5^7^5 = 7, b = 5*/
#include<bits/stdc++.h>
using namespace std;
int main(){
int a = 5;
int b = 7;
cout<< " before swap: a : " << a <<" " << "b: "<< b<<endl;
a = (a ^ b);
b = (a ^ b);
a = (a ^ b)
cout<< " after swap: a : " << a <<" " << "b: "<< b<<endl;
return 0;
}
//approach 1: O(n)
#include<bits/stdc++.h>
using namespace std;
return x;
}
Bit Manipulation 3
int main(){
int n;
cin>>n;
cout<<get_xor(n)<<endl;
return 0;
}
//approach 2: O(1)
logic: for all multiples of 4 xor value is n n%4 == 0 ans : n
n% 4 == 1 ans : 1
n % 4 == 2 ans: n + 1
n % 4 == 3 ans: 0
#include<bits/stdc++.h>
using namespace std;
int get_xor(int n){
if(n % 4 == 0){
return n;
}else if(n % 4 == 1){
return 1;
}else if(n % 4 == 2){
return n + 1;
}else if(n % 4 == 3){
return 0;
}
return -1;
}
int main(){
int n;
cin>>n;
cout<<get_xor(n)<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
Bit Manipulation 4
x = x ^ i;
}
return x;
}
int main(){
int l;
cin>>l;
int r;
cin>>r;
cout<<Xorer(l,r)<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
return -1;
}
int main(){
int l;
cin>>l;
int r;
cin>>r;
cout<<ans<<endl;
return 0;
}
& operator
Bit Manipulation 5
Q. How to check whether a number is odd or even using bit manipulation?
/**logic: The last bit of an odd number is set and the last bit of
an even number is always 0 so if we perform the & operation between the
number and 1 if the answer is 0 then it's even else odd.*/
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
oddEve(n);
return 0;
}
int main(){
int n;
cin>>n;
int i;
cin>>i;
Bit Manipulation 6
checkSetBit(n , i);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
void checkSetBit(int n, int i){
if(n & (1 << i) == 1){
cout<<"The bit is set"<<endl;
}else{
cout<<"The bit is not set"<<endl;
}
}
int main(){
int n;
cin>>n;
int i;
cin>>i;
checkSetBit(n , i);
return 0;
}
/**The second approach is better and more correct because we are not
altering the input value which is given to us. We should always try to
never alter the given value*/
Using OR operator:
Q. set the ith bit of a number:
#include<bits/stdc++.h>
using namespace std;
return x;
}
int main(){
int n;
cin>>n;
int i;
cin>>i;
Bit Manipulation 7
int ans = setIthbit(n,i);
cout<<ans<<endl;
return 0;
}
/**logic: Make the mask like the previous question and then negate it
now we perform the and operator and if the bit is set it will become 0
and if it's 0 it will remain 0.*/
#include<bits/stdc++.h>
using namespace std;
return x;
}
int main(){
int n;
cin>>n;
int i;
cin>>i;
Bit Manipulation 8
int main(){
int n;
cin>>n;
cout<<clearLastSetBit(n);
return 0;
}
/**logic: if 'AND' the result of the number and the preceding number
is 0 then the number is a power of two
8: 1000
&
7: 0111
---------
0: 0000*/
#include<bits/stdc++.h>
using namespace std;
}else{
cout<<"not a power of two"<<endl;
}
}
int main(){
int n;
cin>>n;
powerOfTwo(n);
return 0;
}
Bit Manipulation 9
int countSetBits(int n){
while(n != 0){
int count = 0;
if(n & 1 == 1){
count++;
}
n = n>>1;
}
}
int main(){
int n;
cin>>n;
cout<<countSetBits(n)<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
cout<<countSetBits(n)<<endl;
return 0;
}
Bit Manipulation 10