pranay_teja
9/28/2018 - 7:48 AM

Basic Bitwise Operations

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

// #BitManipulation #BasicProblem

int AND(int x, int y);
int OR(int x, int y);
int XOR(int x, int y);
int NOT(int x);	// or 1's compliment
int RightShift(int x, int n);
int LeftShift(int x, int n);

int main(){
    int t;
    cin>>t;
    while(t--){
        int x,y;
        cin>>x>>y;
        cout<<x<<" AND "<<y<<": "<<AND(x,y)<<endl;
        cout<<x<<" OR "<<y<<": "<<OR(x,y)<<endl;
        cout<<x<<" XOR "<<y<<": "<<XOR(x,y)<<endl;
        cout<<"LeftShift"<<x<<" : "<<LeftShift(x,1)<<endl;
        cout<<"RightShift"<<y<<" 2 times: "<<RightShift(y,1)<<endl;
        cout<<"LeftShift"<<x<<" 2 times: "<<LeftShift(x,2)<<endl;
    }
    return 0;
}

int AND(int x, int y){
    return x&y;
}
int OR(int x, int y){
    return x|y;
}
int XOR(int x, int y){
    return x^y;
}
int NOT(int x){	// 1's compliment
	return ~x;	// ~(00001100) = 11110011
				// ~12 = -13 most siginificant bits is sign bit
}
int RightShift(int x,int n){
	return x>>n;	// right shifting a number divides it by 2
}
int LeftShift(int x,int n){
	return x<<n;	// left shifting a number multiplies it by 2
}