Skip to content

Commit 46c6839

Browse files
authored
Merge pull request #228 from RAUNAK-PANDEY/Raunak
Added 6 programs in Bit Manipulation directory #221
2 parents c7783a3 + 05ef239 commit 46c6839

File tree

6 files changed

+342
-0
lines changed

6 files changed

+342
-0
lines changed

Bit Manipulation/Bit_Difference.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
/*
3+
You are given two numbers A and B. The task is to count the number of bits needed to be flipped to convert A to B.
4+
5+
Example 1:
6+
7+
Input: A = 10, B = 20
8+
Output: 4
9+
Explanation:
10+
A = 01010
11+
B = 10100
12+
As we can see, the bits of A that need
13+
to be flipped are 01010. If we flip
14+
these bits, we get 10100, which is B.*/
15+
//Initial Template for C++
16+
17+
#include<bits/stdc++.h>
18+
using namespace std;
19+
20+
// Function to find number of bits to be flip
21+
// to convert A to B
22+
int countBitsFlip(int a, int b){
23+
24+
// Your logic here
25+
int ans=a^b;
26+
int c=0;
27+
while(ans>0)
28+
{
29+
ans &=(ans-1);
30+
c++;
31+
}
32+
return c;
33+
34+
}
35+
36+
// { Driver Code Starts.
37+
38+
// Driver Code
39+
int main()
40+
{
41+
int t;
42+
cin>>t;// input the testcases
43+
while(t--) //while testcases exist
44+
{
45+
int a,b;
46+
cin>>a>>b; //input a and b
47+
48+
cout<<countBitsFlip(a, b)<<endl;
49+
}
50+
return 0;
51+
} // } Driver Code Ends
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/*
3+
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive).
4+
5+
Example 1:
6+
7+
Input: N = 4
8+
Output: 5
9+
Explanation:
10+
For numbers from 1 to 4.
11+
For 1: 0 0 1 = 1 set bits
12+
For 2: 0 1 0 = 1 set bits
13+
For 3: 0 1 1 = 2 set bits
14+
For 4: 1 0 0 = 1 set bits
15+
Therefore, the total set bits is 5.*/
16+
17+
#include<bits/stdc++.h>
18+
using namespace std;
19+
20+
// Function to count set bits in the given number x
21+
// n: input to count the number of set bits
22+
int largestpowerof2inrange(int n)
23+
{
24+
int x=0;
25+
while((1<<x) <=n)
26+
{
27+
x++;
28+
}
29+
return x-1;
30+
}
31+
int countSetBits(int n)
32+
{
33+
// Your logic
34+
if(n==0) return 0;
35+
int x=largestpowerof2inrange(n);
36+
int first= x*(1<<(x-1));
37+
int second= n-(1<<x)+1;
38+
int third=n-(1<<x);
39+
int ans=first+second+countSetBits(third);
40+
return ans;
41+
}
42+
43+
// Driver code
44+
int main()
45+
{
46+
int t;
47+
cin>>t;// input testcases
48+
while(t--) //while testcases exist
49+
{
50+
int n;
51+
cin>>n; //input n
52+
53+
cout << countSetBits(n) << endl;// print the answer
54+
}
55+
return 0;
56+
}
57+
// } Driver Code Ends
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
/*
3+
Given a number N having only one ‘1’ and all other ’0’s in its binary representation, find position of the only set bit. If there are 0 or more than 1 set bit the answer should be -1. Position of set bit '1' should be counted starting with 1 from LSB side in binary representation of the number.
4+
5+
Example 1:
6+
7+
Input:
8+
N = 2
9+
Output:
10+
2
11+
Explanation:
12+
2 is represented as "10" in Binary.
13+
As we see there's only one set bit
14+
and it's in Position 2 and thus the
15+
Output 2.*/
16+
#include <bits/stdc++.h>
17+
using namespace std;
18+
19+
20+
class Solution {
21+
public:
22+
23+
int isPowerOfTwo(unsigned N)
24+
{
25+
return N && (!(N & (N - 1)));
26+
}
27+
int findPosition(int N) {
28+
// code here
29+
if (!isPowerOfTwo(N))
30+
return -1;
31+
32+
unsigned i = 1, pos = 1;
33+
34+
35+
while (!(i & N)) {
36+
37+
i = i << 1;
38+
39+
// increment position
40+
++pos;
41+
}
42+
43+
return pos;
44+
45+
}
46+
};
47+
48+
// { Driver Code Starts.
49+
int main() {
50+
int t;
51+
cin >> t;
52+
while (t--) {
53+
int N;
54+
55+
cin>>N;
56+
57+
Solution ob;
58+
cout << ob.findPosition(N) << endl;
59+
}
60+
return 0;
61+
} // } Driver Code Ends
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
/*
3+
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers.
4+
5+
6+
Example 1:
7+
8+
Input:
9+
N = 2
10+
arr[] = {1, 2, 3, 2, 1, 4}
11+
Output:
12+
3 4
13+
Explanation:
14+
3 and 4 occur exactly once.*/
15+
#include<bits/stdc++.h>
16+
using namespace std;
17+
18+
19+
class Solution
20+
{
21+
public:
22+
vector<int> singleNumber(vector<int> nums)
23+
{
24+
// Code here.
25+
int res=0;
26+
vector <int> vec,v;
27+
for(int i=0;i<nums.size();i++)
28+
{
29+
res^=nums[i];
30+
31+
}
32+
int set_bit_no = res & ~(res-1);
33+
int temp=res;
34+
for(int i=0;i<nums.size();i++)
35+
{
36+
if((nums[i]& set_bit_no))
37+
{
38+
vec.push_back(nums[i]);
39+
}
40+
}
41+
for(int i=0;i<vec.size();i++)
42+
{
43+
temp^=vec[i];
44+
}
45+
v.push_back(temp);
46+
res^=temp;
47+
v.push_back(res);
48+
sort(v.begin(),v.end());
49+
return v;
50+
51+
}
52+
};
53+
54+
// { Driver Code Starts.
55+
int main(){
56+
int T;
57+
cin >> T;
58+
while(T--)
59+
{
60+
int n;
61+
cin >> n;
62+
vector<int> v(2 * n + 2);
63+
for(int i = 0; i < 2 * n + 2; i++)
64+
cin >> v[i];
65+
Solution ob;
66+
vector<int > ans = ob.singleNumber(v);
67+
for(auto i: ans)
68+
cout << i << " ";
69+
cout << "\n";
70+
}
71+
return 0;
72+
} // } Driver Code Ends

Bit Manipulation/Number_of_1_Bits.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/* Given a positive integer N, print count of set bits in it.
3+
4+
Example 1:
5+
6+
Input:
7+
N = 6
8+
Output:
9+
2
10+
Explanation:
11+
Binary representation is '110'
12+
So the count of the set bit is 2.*/
13+
#include<bits/stdc++.h>
14+
using namespace std;
15+
16+
17+
18+
class Solution
19+
{
20+
public:
21+
int setBits(int N)
22+
{
23+
int count =0;
24+
while (N) {
25+
N &=(N-1);
26+
count ++;
27+
28+
}
29+
return count;
30+
}
31+
};
32+
33+
// { Driver Code Starts.
34+
int main()
35+
{
36+
int t;
37+
cin >> t;
38+
while (t--)
39+
{
40+
int N;
41+
cin >> N;
42+
43+
Solution ob;
44+
int cnt = ob.setBits(N);
45+
cout << cnt << endl;
46+
}
47+
return 0;
48+
}
49+
// } Driver Code Ends

Bit Manipulation/Power_of_2.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Given a non-negative integer N. The task is to check if N is a power of 2. More formally, check if N can be expressed as 2x for some x.
3+
4+
5+
Example 1:
6+
7+
Input: N = 1
8+
Output: true
9+
Explanation:
10+
1 is equal to 2 raised to 0 (20 = 1).*/
11+
//Initial Template for C++
12+
13+
#include<bits/stdc++.h>
14+
using namespace std;
15+
16+
// Function to check power of two
17+
bool isPowerofTwo(long long n){
18+
19+
// Your code here
20+
if(n==0)
21+
return 0;
22+
23+
else if(n>0)
24+
{
25+
if((n & n-1)==0)
26+
return 1;
27+
}
28+
return 0;
29+
}
30+
31+
// Driver code
32+
int main()
33+
{
34+
35+
int t;
36+
cin>>t;//testcases
37+
38+
for(int i=0;i<t;i++)
39+
{
40+
long long n; //input a number n
41+
cin>>n;
42+
43+
if(isPowerofTwo(n))//Now, if log2 produces an integer not decimal then we are sure raising 2 to this value
44+
cout<<"YES"<<endl;
45+
else
46+
cout<<"NO"<<endl;
47+
48+
}
49+
50+
return 0;
51+
}
52+
// } Driver Code Ends

0 commit comments

Comments
 (0)