May 16, 2020

C program using bitwise right shift(>>)

EX: write a C program to perform bitwise right shift operation. How to write a C program to perform bitwise right shift operation of intiger. C program to perform bitwise right shift operation of intiger.

Input from user:

Enter the number:15

Enter how many numbers you wan't to right shift:2

Expected Output:

Output after performing right shift operation is: 3

Logic to perform right shift operation of two intiger's:

Firstly you need to know how work right shift operator.

•  It  takes two numbers and right shift the bits of the first operand, the second  operand decides the number of places to shift.

1. We take two intiger numbers i.e, no=15 and s=2.

Where no shows the number which you wan't to right shift and s shows the how many bits you want to right shift.

2. Binary representation of no>>s is-

no    = 00001111
------------------------------------
no>>s   = 00000011 =3(in decimal)

3. After performing right shift operation 8 bit binary representation is 0000 0011 which is 3 in decimal.

4. So output of the program is  3.

Program:

#include<stdio.h>
void main()
{
int no,s;

printf("Enter the number\n");
scanf("%d",&no);

printf("Enter how many bits you wan't to right shift\n");
scanf("%d",&s);

printf("Output after performing right-shift operation is:%d",no>>s);
}

Above program shows the following output: