Saturday 21 June 2014

C++ programs to demonstrate working of bitwise operators

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.


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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

OUTPUT

No comments:

Post a Comment