Here are C++ program to demonstrate working of bitwise right shift (>>), left shift (<<), 1's complement (~), AND (&), OR (|) and XOR (^) operators.
In all the following programs I have used binary() function which convert decimal to binary to show how bits are manipulated by different bitwise operators. I have also given programs to shift bits right or left shift bits without using bitwise right shift or left shift operators and a program to find 1's complement of a number without using bitwise 1's complement operator.
In all the following programs I have used binary() function which convert decimal to binary to show how bits are manipulated by different bitwise operators. I have also given programs to shift bits right or left shift bits without using bitwise right shift or left shift operators and a program to find 1's complement of a number without using bitwise 1's complement operator.
C++ program to demonstrate working of bitwise right shift operator
#include<iostream>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int a,n;
cout<<"Enter the number ";
cin>>a;
cout<<"Enter the value of n : ";
cin>>n;
cout<<"Before Right shift ";
binary(a);
a=a>>n;
cout<<endl<<"After Right shift bits to "<<n<<" places : ";
binary(a);
return 0;
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnnh4FbZzTtl22_Y4Y6Xc7zZBYZfPVaq6C00iAiy0TV-9m8id7z_tYabCIx3WcAPkg5jLvs9v1-P1wuVhvka3L6lNSjQAodEy-Uu8tqMhyphenhyphen-VaMr7plvWh18FRpP9wYHUMbhjO59XKrC-zL/s1600/Capture294.PNG)
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int a,n;
cout<<"Enter the number ";
cin>>a;
cout<<"Enter the value of n : ";
cin>>n;
cout<<"Before Right shift ";
binary(a);
a=a>>n;
cout<<endl<<"After Right shift bits to "<<n<<" places : ";
binary(a);
return 0;
}
OUTPUT
C++ program to shift bit to right without using bitwise right shift operator
#include<iostream>
#include<math.h>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int a,n;
cout<<"Enter the number ";
cin>>a;
cout<<"Enter the value of n : ";
cin>>n;
cout<<"Before Right shift ";
binary(a);
a=a/pow(2,n);
cout<<endl<<"After Right shift bits to "<<n<<" places : ";
binary(a);
return 0;
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQnZTqG7CjMfrGVNKDCJsXp7Qkdga8O6PUoCthF6ohvdI0oHYipyape1DNScnjWIcfdvsi-N6gWCx7HJ8YDtTIQNlCkWtn5LkVtbif_TWcdONC_denUroBbqxfVu_PIxbC-cOxAW53kpO9/s1600/Capture295.PNG)
#include<math.h>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int a,n;
cout<<"Enter the number ";
cin>>a;
cout<<"Enter the value of n : ";
cin>>n;
cout<<"Before Right shift ";
binary(a);
a=a/pow(2,n);
cout<<endl<<"After Right shift bits to "<<n<<" places : ";
binary(a);
return 0;
}
OUTPUT
C++ program to demonstrate working of bitwise left shift operator
#include<iostream>
#include<math.h>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int a,n;
cout<<"Enter the number : ";
cin>>a;
cout<<"Enter the value of n : ";
cin>>n;
cout<<"Before Left shift : ";
binary(a);
a=a<<n;
cout<<endl<<"After Left shift bits to "<<n<<" places : ";
binary(a);
return 0;
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlx4Rm523fVnYZqUf2DQbYn7reYeICR4BUNtQpoI2AbQGMOI-DixPVUIMlsnFMFBQBQsoD0_2BoEFbmQlKb66XoTRBgOrnWPrqGWIF0ooPfOYYupTvn_mVILvlzrN3TtiLvCssAoaKsay_/s1600/Capture296.PNG)
#include<math.h>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int a,n;
cout<<"Enter the number : ";
cin>>a;
cout<<"Enter the value of n : ";
cin>>n;
cout<<"Before Left shift : ";
binary(a);
a=a<<n;
cout<<endl<<"After Left shift bits to "<<n<<" places : ";
binary(a);
return 0;
}
OUTPUT
C++ program to shift bits to left without using bitwise left shift operator
#include<iostream>
#include<math.h>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int a,n;
cout<<"Enter the number : ";
cin>>a;
cout<<"Enter the value of n : ";
cin>>n;
cout<<"Before Left shift : ";
binary(a);
a=a*pow(2,n);
cout<<endl<<"After Left shift bits to "<<n<<" places : ";
binary(a);
return 0;
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhooPNGApCZJrtCOmgBkYO9lpglpACMl8FWEXV88t_Heliez9W5e1PRBaC9ArBHcHDAXpZCmGuytG1rxuxN5VZ7tMICDCJyKqaaQjcI7w4fuEq0Tdw3S1HVCojd2seL5YjSbn4_HEUv2y__/s1600/Capture297.PNG)
#include<math.h>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int a,n;
cout<<"Enter the number : ";
cin>>a;
cout<<"Enter the value of n : ";
cin>>n;
cout<<"Before Left shift : ";
binary(a);
a=a*pow(2,n);
cout<<endl<<"After Left shift bits to "<<n<<" places : ";
binary(a);
return 0;
}
OUTPUT
C++ program to demonstrate working of bitwise 1's complement operator
#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;
void binary(unsigned int num)
{
unsigned int a=num,k,y,amask;
for(int i=31;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
unsigned int a,n;
cout<<"Enter the number : ";
cin>>a;
cout<<"Before Complementing : ";
binary(a);
a=~a;
cout<<endl<<"After Complementing : ";
binary(a);
return 0;
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiH7yLwrQKNQ3v0M-rg5TU3_xLpYbxmbldo6xuwPfIavezJIfudky-YiEK2DwiIBut1HcEjlqwvu5cmb3YWJkCewih7a8XmRDIY_avFNbWSyRhjvbmB58ORkQgNafP90DqEuyelyR5-GGZs/s1600/Capture298.PNG)
#include<math.h>
#include<stdlib.h>
using namespace std;
void binary(unsigned int num)
{
unsigned int a=num,k,y,amask;
for(int i=31;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
unsigned int a,n;
cout<<"Enter the number : ";
cin>>a;
cout<<"Before Complementing : ";
binary(a);
a=~a;
cout<<endl<<"After Complementing : ";
binary(a);
return 0;
}
OUTPUT
C++ program to find 1's complement without using 1's complement operator
#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
void complement(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"1":cout<<"0";
}
}
int main()
{
int a,n;
cout<<"Enter the number : ";
cin>>a;
cout<<"Before Complementing : ";
binary(a);
cout<<endl<<"After Complementing : ";
complement(a);
return 0;
}
#include<math.h>
#include<stdlib.h>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
void complement(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"1":cout<<"0";
}
}
int main()
{
int a,n;
cout<<"Enter the number : ";
cin>>a;
cout<<"Before Complementing : ";
binary(a);
cout<<endl<<"After Complementing : ";
complement(a);
return 0;
}
OUTPUT
C++ program to demonstrate working of bitwise AND operator
#include<iostream>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int num1,num2,result;
cout<<"Enter two numbers : ";
cin>>num1>>num2;
cout<<"Before AND operation : ";
binary(num1);
cout<<" ";
binary(num2);
result=num1&num2;
cout<<"\nAfter AND operation : ";
binary(result);
return 0;
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjIL-WhZeEba8mCJ0dFsKsZ0W-JTLPGs_Mri9Yv98fvohyphenhypheneMPAPb7-gibv0Tk7hYZui0uCQbJLO6q9Rl2E2VH-0bBml4jhdOu4PWe8f0F2j0h5jH_-7f-Z9pS3U0O3UycGRLShlkBOIQ59/s1600/Capture308.PNG)
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int num1,num2,result;
cout<<"Enter two numbers : ";
cin>>num1>>num2;
cout<<"Before AND operation : ";
binary(num1);
cout<<" ";
binary(num2);
result=num1&num2;
cout<<"\nAfter AND operation : ";
binary(result);
return 0;
}
OUTPUT
C++ program to demonstrate working of bitwise OR operator
#include<iostream>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int num1,num2,result;
cout<<"Enter two numbers : ";
cin>>num1>>num2;
cout<<"Before OR operation : ";
binary(num1);
cout<<" ";
binary(num2);
result=num1|num2;
cout<<"\nAfter OR operation : ";
binary(result);
return 0;
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFtSCCmCmOkqqwKs8wdkCd7-a1mIiDJD6u0NNQg8YrKLlH-Qg3GtSyqnhL5MogdhyozBHU5Xy75GHOeD9DSocxECpIFjlSOxKx8pHmsj2pz06FfSYFxj4kXG4SZ8vlhxaef-g_2aSj2jhF/s1600/Capture306.PNG)
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int num1,num2,result;
cout<<"Enter two numbers : ";
cin>>num1>>num2;
cout<<"Before OR operation : ";
binary(num1);
cout<<" ";
binary(num2);
result=num1|num2;
cout<<"\nAfter OR operation : ";
binary(result);
return 0;
}
OUTPUT
C++ program to demonstrate working of bitwise XOR operator
#include<iostream>
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int num1,num2,result;
cout<<"Enter two numbers : ";
cin>>num1>>num2;
cout<<"Before XOR operation : ";
binary(num1);
cout<<" ";
binary(num2);
result=num1^num2;
cout<<"\nAfter XOR operation : ";
binary(result);
return 0;
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBTQEZAvYMHxxDOgxwCtCQRFtTv5XUpI-IKkpDQ-wjQJCUOUR-nzdBzGuKoCKl5idtup4-0fC_8GEpru5R5c7tcIl1HY4eAmITO_YKYoh_hGQ-54tIAavMR_od4AJuuUEqD8IoekLENrf4/s1600/Capture307.PNG)
using namespace std;
void binary(int num)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
for(int i=y-1;i>=0;i--)
{
amask=1<<i;
k=a&amask;
k==0?cout<<"0":cout<<"1";
}
}
int main()
{
int num1,num2,result;
cout<<"Enter two numbers : ";
cin>>num1>>num2;
cout<<"Before XOR operation : ";
binary(num1);
cout<<" ";
binary(num2);
result=num1^num2;
cout<<"\nAfter XOR operation : ";
binary(result);
return 0;
}
No comments:
Post a Comment