Skip to content

Added Bit Set Algorithm Solutions #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions BIT set/Bit Difference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// { Driver Code Starts
//Initial Template for C++

#include<bits/stdc++.h>
using namespace std;


// } Driver Code Ends
//User function Template for C++

class Solution{
public:
// Function to find number of bits needed to be flipped to convert A to B
int countBitsFlip(int a, int b){

// Your logic here
int diff=0;
if(a<b)
swap(a,b);
while(a>0&&b>0)
{
if(a%2!=b%2)//if any bit does not matches
diff++;
a/=2;
b/=2;
}
while(a>0)// if(still a bits are left)
{
if(a%2==1)//a bit is set than only set will different
diff++;
a/=2;

}
return diff;
}
};

// { Driver Code Starts.

// Driver Code
int main()
{
int t;
cin>>t;// input the testcases
while(t--) //while testcases exist
{
int a,b;
cin>>a>>b; //input a and b

Solution ob;
cout<<ob.countBitsFlip(a, b)<<endl;
}
return 0;
} // } Driver Code Ends
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Square of a number using bitwise operators
#include <bits/stdc++.h>
using namespace std;

int square(int n)
{
// Base case
if (n == 0)
return 0;

// Handle negative number
if (n < 0)
n = -n;

// Get floor(n/2) using right shift
int x = n >> 1;

// If n is odd
if (n & 1)
return ((square(x) << 2) + (x << 2) + 1);
else // If n is even
return (square(x) << 2);
}

// Driver Code
int main()
{
// Function calls
for (int n = 1; n <= 5; n++)
cout << "n = " << n << ", n^2 = " << square(n)
<< endl;
return 0;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// C++ implementation to Divide two
// integers without using multiplication,
// division and mod operator
#include <bits/stdc++.h>
using namespace std;

// Function to divide a by b and
// return floor value it
int divide(long long dividend, long long divisor) {

// Calculate sign of divisor i.e.,
// sign will be negative only iff
// either one of them is negative
// otherwise it will be positive
int sign = ((dividend < 0) ^
(divisor < 0)) ? -1 : 1;

// remove sign of operands
dividend = abs(dividend);
divisor = abs(divisor);

// Initialize the quotient
long long quotient = 0, temp = 0;

// test down from the highest bit and
// accumulate the tentative value for
// valid bit
for (int i = 31; i >= 0; --i) {

if (temp + (divisor << i) <= dividend) {
temp += divisor << i;
quotient |= 1LL << i;
}
}

return sign * quotient;
}

// Driver code
int main() {
int a = 10, b = 3;
cout << divide(a, b) << "\n";

a = 43, b = -8;
cout << divide(a, b);

return 0;
}

43 changes: 43 additions & 0 deletions BIT set/Find position of set bit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
class Solution {
public:
int findPosition(int N) {
// code here
if(N==1)
return 1;
int res=log2(N);
float ans=log2(N);
if(ans!=res)
{
return -1;
}
int c=1;
while(N>0)
{
if(N%2==1)
return c;
N/=2;
c++;
}
return -1;
}
};

// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int N;

cin>>N;

Solution ob;
cout << ob.findPosition(N) << endl;
}
return 0;
} // } Driver Code Ends
69 changes: 69 additions & 0 deletions BIT set/Non Repeating Numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;

// } Driver Code Ends
class Solution
{
public:
vector<int> singleNumber(vector<int> nums)
{
// Code here.
int x1=0,a,b;
vector<int> v;
for(int i=0;i<nums.size();i++)
x1=nums[i]^x1;


int rmsb=x1 & -x1;//right most set bit
int x=0,y=0;
for(int i=0;i<nums.size();i++)
{
if((rmsb&nums[i])==0)
{
x=x^nums[i];
}
else
{

y=y^nums[i];
}
}
if(y<x)
return {y,x};
else
return {x,y};












}
};

// { Driver Code Starts.
int main(){
int T;
cin >> T;
while(T--)
{
int n;
cin >> n;
vector<int> v(2 * n + 2);
for(int i = 0; i < 2 * n + 2; i++)
cin >> v[i];
Solution ob;
vector<int > ans = ob.singleNumber(v);
for(auto i: ans)
cout << i << " ";
cout << "\n";
}
return 0;
} // } Driver Code Ends