bitwise hack for compatitive programming
// how to set bit in the number
#include<bits/stdc++.h>
using namespace std;
// this is for set a bit in number at pos
void settheory(int &num, int pos){
num|=(1<<pos);
}
int main(){
int num=4, pos=1;
settheory(num, pos);
cout<<(int)(num)<<endl;
return 0;
}
// this is unset a bit in a number in given pos
void unset(int &num, int pos){
num&=(~(1<<pos));
}
int main(){
int num=7;
int pos=1;
unset(num, pos);
cout<<num<<endl;
return 0;
}
// toggling a bit in nth position
void toggel(int &num, int pos){
num^=(1<<pos);
}
int main()
{
int num=4;
int pos=1;
toggel(num, pos);
cout<<num<<endl;
return 0;
}
// checking a bit at n th position is set or unset
bool at_pos(int num, int pos){
bool bit=num&(1<<pos);
return bit;
}
int main(){
int num=6;
int pos=3;
bool bit=at_pos(num, pos);
cout<<bit<<endl;
return 0;
}
// converting every bit of a number | 1's compliment
int main(){
int num=4;
cout<<(~num);
return 0;
}
Comments
Post a Comment