Added sets bit manupulation and number system
parent
75e0a5dc3c
commit
aa74bdbd10
|
@ -0,0 +1,2 @@
|
|||
.vscode/
|
||||
*.exe
|
|
@ -0,0 +1,20 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int unique(int a[],int n){
|
||||
int num=0;
|
||||
for(int i=0;i<n;i++){
|
||||
num=a[i]^num;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n;
|
||||
cin>>n;
|
||||
int a[n];
|
||||
for(int i=0;i<n;i++){
|
||||
cin>>a[i];
|
||||
}
|
||||
cout<<unique(a,n);
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int unique2(int a[],int n){
|
||||
int num=0;
|
||||
for(int i=0;i<n;i++)
|
||||
num=num^a[i];
|
||||
return num;
|
||||
}
|
||||
int getbit(int n,int num){
|
||||
return (n&(1<<num));
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n;
|
||||
cin>>n;
|
||||
int a[n];
|
||||
for(int i=0;i<n;i++){
|
||||
cin>>a[i];
|
||||
}
|
||||
int num=unique2(a,n);
|
||||
int c=0;
|
||||
while(c<num){
|
||||
if(num&(1<<c))
|
||||
break;
|
||||
c++;
|
||||
}
|
||||
int n1=0;
|
||||
for(int i=0;i<n;i++){
|
||||
if(getbit(a[i],c))
|
||||
n1=n1^a[i];
|
||||
}
|
||||
cout<<n1<<" "<<(n1^num);
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
int n;
|
||||
cin>>n;
|
||||
int a[n];
|
||||
for(int i=0;i<n;i++)
|
||||
cin>>a[i];
|
||||
|
||||
int sum=0,num=0;
|
||||
for(int i=0;i<64;i++){
|
||||
sum=0;
|
||||
for(int j=0;j<n;j++){
|
||||
if(a[j]&(1<<i))
|
||||
sum++;
|
||||
}
|
||||
if(sum%3 !=0)
|
||||
num=(num|(1<<i));
|
||||
}
|
||||
cout<<num;
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int setbit(int n,int pos){
|
||||
return (n|(1<<pos));
|
||||
}
|
||||
int getbit(int n,int pos){
|
||||
return (( n &(1<<pos) )!=0);
|
||||
}
|
||||
int removebit(int n,int pos){
|
||||
return (n &(~(1<<pos)));
|
||||
}
|
||||
int updatebit(int n,int pos,int val){
|
||||
int a=n & (~(1<<pos));
|
||||
return (a|(val<<pos));
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n,p,v;
|
||||
cin>>n>>p;
|
||||
cout<<getbit(n,p)<<endl;
|
||||
cout<<setbit(n,p)<<endl;
|
||||
cout<<removebit(n,p)<<endl;
|
||||
cin>>v;
|
||||
cout<<updatebit(n,p,v)<<endl;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int countones(int n){
|
||||
int count=0;
|
||||
while(n>0){
|
||||
n=(n&(n-1));
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n;
|
||||
cin>>n;
|
||||
cout<<countones(n);
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int check2power(int n){
|
||||
|
||||
return ((n & (n-1))==0);
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n;
|
||||
cin>>n;
|
||||
cout<<check2power(n);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
int n;
|
||||
cin>>n;
|
||||
int a[n];
|
||||
for(int i=0;i<n;i++)
|
||||
cin>>a[i];
|
||||
|
||||
for(int i=0;i<(1<<n);i++){
|
||||
for(int j=0;j<n;j++){
|
||||
if(i&(1<<j))
|
||||
cout<<a[j]<<" ";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int reverse(int n){
|
||||
int ans=0,d;
|
||||
while(n>0){
|
||||
d=n%10;
|
||||
ans=ans*10 + d;
|
||||
n=n/10;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
int binary_addition(int a,int b){
|
||||
int add=0,carry=0;
|
||||
while(a>0 && b>0){
|
||||
|
||||
if(a%2==1 && b%2==1){
|
||||
add=add*10 + carry;
|
||||
carry=1;
|
||||
}
|
||||
else if((a%2==1 && b%2==0) || (a%2==0 && b%2==1)){
|
||||
if(carry==1){
|
||||
add=add*10 + 0;
|
||||
carry=1;
|
||||
}
|
||||
else{
|
||||
add=add*10 + 1;
|
||||
carry=0;
|
||||
}
|
||||
}
|
||||
else{
|
||||
add = add*10 + carry;
|
||||
carry=0;
|
||||
}
|
||||
a=a/10;
|
||||
b=b/10;
|
||||
}
|
||||
while(a>0){
|
||||
if(a%2==1){
|
||||
if(carry==1){
|
||||
carry=1;
|
||||
add=add*10+0;
|
||||
}
|
||||
else{
|
||||
carry=0;
|
||||
add=add*10 + 1;
|
||||
}
|
||||
}
|
||||
else{
|
||||
add=add*10 +carry;
|
||||
carry=0;
|
||||
}
|
||||
a=a/10;
|
||||
}
|
||||
while(b>0){
|
||||
if(b%2==1){
|
||||
if(carry==1){
|
||||
carry=1;
|
||||
add=add*10+0;
|
||||
}
|
||||
else{
|
||||
carry=0;
|
||||
add=add*10 + 1;
|
||||
}
|
||||
}
|
||||
else{
|
||||
add=add*10 +carry;
|
||||
carry=0;
|
||||
}
|
||||
b=b/10;
|
||||
}
|
||||
if(carry==1){
|
||||
add=add*10 +1;
|
||||
}
|
||||
add=reverse(add);
|
||||
return add;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int a,b;
|
||||
cin>>a>>b;
|
||||
int add=binary_addition(a,b);
|
||||
cout<<add;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
int binarytodecimal(int n){
|
||||
int two=1,r,num=0;
|
||||
while(n>0){
|
||||
r=n%10;
|
||||
num=num + two*r;
|
||||
two*=2;
|
||||
n/=10;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
int main(){
|
||||
int n;
|
||||
cin>>n;
|
||||
int decimal=binarytodecimal(n);
|
||||
cout<<decimal;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int decimaltobinary(int n){
|
||||
int max=1;
|
||||
while(max<=n){
|
||||
max=max*2;
|
||||
}
|
||||
max/=2;
|
||||
int num=0;
|
||||
while(max>=1){
|
||||
num=num*10 + n/max;
|
||||
n-=max*(n/max);
|
||||
max=max/2;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n;
|
||||
cin>>n;
|
||||
int binary=decimaltobinary(n);
|
||||
cout<<binary;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
string decimal_to_hexa(int n){
|
||||
int multipler=1;
|
||||
while(multipler<=n)
|
||||
multipler*=16;
|
||||
multipler/=16;
|
||||
string num="";
|
||||
int d;
|
||||
while(multipler>=1){
|
||||
d=n/multipler;
|
||||
if(d>9){
|
||||
char c='A'+(d-10);
|
||||
num= num + c;
|
||||
}
|
||||
else
|
||||
num=num + to_string(d);
|
||||
n=n-(d*multipler);
|
||||
multipler/=16;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
int main(){
|
||||
int n;
|
||||
cin>>n;
|
||||
string hexa=decimal_to_hexa(n);
|
||||
cout<<hexa;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int decimal_to_octal(int n){
|
||||
int multiplier=1;
|
||||
while(multiplier<=n){
|
||||
multiplier*=8;
|
||||
}
|
||||
|
||||
multiplier/=8;
|
||||
int num=0;
|
||||
while(multiplier>=1){
|
||||
num=num*10 + n/multiplier;
|
||||
n=n - multiplier*(n/multiplier);
|
||||
multiplier/=8;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
int main(){
|
||||
int n;
|
||||
cin>>n;
|
||||
int octal=decimal_to_octal(n);
|
||||
cout<<octal;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int hexa_to_decimal(string n){
|
||||
int l=n.size();
|
||||
l=l-1;
|
||||
int ans=0,x=1;
|
||||
while(l>=0){
|
||||
if(n[l]>='0' && n[l]<='9')
|
||||
ans+=(n[l]-'0')*x;
|
||||
else
|
||||
ans+=x*(n[l]-'A'+10);
|
||||
x*=16;
|
||||
l--;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
int main(){
|
||||
string s;
|
||||
cin>>s;
|
||||
int decimal=hexa_to_decimal(s);
|
||||
cout<<decimal;
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int octal_to_decimal(int n){
|
||||
int num=0,r,multiplier=1;
|
||||
while(n>0){
|
||||
r=n%10;
|
||||
num=num + (r*multiplier);
|
||||
n=n/10;
|
||||
multiplier=multiplier*8;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n;
|
||||
cin>>n;
|
||||
int decimal=octal_to_decimal(n);
|
||||
cout<<decimal;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
multiset<int> ms;
|
||||
ms.insert(1);
|
||||
ms.insert(1);
|
||||
ms.insert(1);
|
||||
ms.insert(2);
|
||||
ms.insert(3);
|
||||
ms.insert(5);
|
||||
for(auto el:ms)
|
||||
cout<<el;
|
||||
cout<<endl;
|
||||
ms.erase(ms.find(1));
|
||||
for(auto el:ms)
|
||||
cout<<el;
|
||||
cout<<endl;
|
||||
ms.erase(5);
|
||||
ms.erase(1);
|
||||
for(auto el:ms)
|
||||
cout<<el;
|
||||
cout<<endl;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
set<int> s;
|
||||
s.insert(1);
|
||||
s.insert(2);
|
||||
s.insert(3);
|
||||
s.insert(4);
|
||||
s.insert(2);
|
||||
for(auto el:s)
|
||||
cout<<el;
|
||||
cout<<endl;
|
||||
for(auto el=s.begin();el!=s.end();el++)
|
||||
cout<<*el;
|
||||
cout<<endl;
|
||||
for(auto el=s.rbegin();el!=s.rend();el++)
|
||||
cout<<*el;
|
||||
cout<<endl;
|
||||
|
||||
|
||||
set<int,greater<int>> s1;
|
||||
s1.insert(1);
|
||||
s1.insert(2);
|
||||
s1.insert(3);
|
||||
s1.insert(4);
|
||||
s1.insert(2);
|
||||
for(auto el:s1)
|
||||
cout<<el;
|
||||
cout<<endl;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
unordered_set<int> us;
|
||||
us.insert(2);
|
||||
us.insert(1);
|
||||
us.insert(4);
|
||||
us.insert(5);
|
||||
for(auto el:us)
|
||||
cout<<el;
|
||||
cout<<endl;
|
||||
}
|
Loading…
Reference in New Issue