# Bitwise operation

```#include<bits/stdc++.h>
using namespace std;
int main()
{
int a=0b101;
int b=0b10110;
b<<=4;        //means b=101100000 shifting of b by 4
int c=a|b;
int d=a&b;
int e=a^b;
cout<<bitset<9>(c)<<"\n";
cout<<bitset<9>(d)<<"\n";
cout<<bitset<10>(e)<<"\n";
return 0;
}```
```#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

ll t, p, q, r, ans, i, j;

cin>>t;

for(;t--;)
{
cin>>p>>q>>r;

ll c[21];

for(i=0; i<=20; i++){
c[i]=0;
}

for(i=0; i<20; i++)
{
c[i]=c[i]+(p%2)+(q%2)+(r%2);
p/=2;
q/=2;
r/=2;
}

ans=1;

for(i=0; i<20; i++)
{
if(c[i]==1){
ans=0;
break;
}else if(c[i]==3){
ans=ans*4;
}
}
cout<<ans<<"\n";
}

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

int main() {
int t;
int n,y;
cin>>t;
while(t--)
{
cin>>n>>y;
long a[n];
bool u=false;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
long p=0;
for(int i=0;i<n;i++)
{
p=p | a[i];
}
for(int i=0;i<=y;i++)
{
if((p|i)==y)
{
cout<<i<<"\n";
u=true;
break;
}
}
if(u==false) cout<<"-1\n";
}
return 0;
}

METHOD 2:-

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

int main() {
int t,n,y;
cin>>t;
while(t--)
{
cin>>n>>y;
int a[n];
string required, current;
int orans=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
orans|=a[i];
}
current=bitset<32>(orans).to_string();
required=bitset<32>(y).to_string();
for(int i=31; i>=0; i--)
{
if(current[i]=='1' && required[i]=='0')
{
flag=1;
break;
}
if(current[i]=='0' && required[i]=='1')
{
}
}
if(flag){cout<<-1<<endl;}
}
return 0;
}
```
```#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int p;
cin>>p;
int itemprice=2048;
int ans=0;
while(p>0)
{
ans+=p/itemprice;
p%=itemprice;
itemprice/=2;
}
cout<<ans<<"\n";
}
return 0;
}
```
```#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int ans=0;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if((a[i]==1&&a[j]==1)||(a[i]==0)) ans++;
}
}
cout<<ans<<"\n";
}
return 0;
}
```
```#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
long long x;
cin>>x;
cout<<x<<" "<<0<<"\n";
}
return 0;
}
```
```#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
char a[n][10];
for(int i=0;i<n;i++)
{
for(int j=0;j<10;j++)
{
cin>>a[i][j];
}
}
int ans=0;
for(int i=0;i<10;i++)
{
int cnt=0;
for(int j=0;j<n;j++)
{
if(a[j][i]=='1') cnt++;
}
if(cnt%2==1) ans++;
}
cout<<ans<<"\n";
}
return 0;
}
```
```class Solution {
public:
vector<int> countBits(int n) {
vector<int> v(n+1, 0);
for(int i=1;i<=n;i++)
{
int p=i;
int t=32;
int ans=0;

while(p!=0)
{
int rsbm = p&-p;
p-=rsbm;
ans++;
}
v[i]=ans;
}
return v;
}
};```