Here is C++ program to perform circular left shift and circular right shift.
C++ program to perform circular right shift
C++ program to perform circular left shift
C++ program to perform circular right shift
#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";
}
}
void cilft(int num,int lft)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
lft%=y;
a=(a>>lft)|(a<<(y-lft));
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 Circular Right shift : ";
binary(a);
cout<<endl<<"After Circular Right shift bits to "<<n<<" places : ";
cilft(a,n);
return 0;
}
#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";
}
}
void cilft(int num,int lft)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
lft%=y;
a=(a>>lft)|(a<<(y-lft));
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 Circular Right shift : ";
binary(a);
cout<<endl<<"After Circular Right shift bits to "<<n<<" places : ";
cilft(a,n);
return 0;
}
OUTPUT
C++ program to perform circular left shift
#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";
}
}
void cilft(int num,int lft)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
lft%=y;
a=(a<<lft)|(a>>(y-lft));
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 Circular Left shift : ";
binary(a);
cout<<endl<<"After Circular Left shift bits to "<<n<<" places : ";
cilft(a,n);
return 0;
}
#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";
}
}
void cilft(int num,int lft)
{
int a=num,k,y,amask;
for(y=0;num>0;y++,num/=2);
lft%=y;
a=(a<<lft)|(a>>(y-lft));
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 Circular Left shift : ";
binary(a);
cout<<endl<<"After Circular Left shift bits to "<<n<<" places : ";
cilft(a,n);
return 0;
}
No comments:
Post a Comment