#include<conio.h>
#include<stdio.h>
int main()
{
int x,y;
printf("Read the integer from keyboard (x):-");
scanf("%d",&x);
x<<=3; // <<left shift operator //
y=x;
printf("The right shifted data is = %d",y);
getch();
}
/*The number enterd through the keyboard is 2 and its corresponding binary no is 10.
Shifting three bits left means the number is multiplied by 8; in short y=n*2^s where n=number and
s=the no of position to be shifted.
As per the program Y=2*2^3=16.*/
Here’s an explanation of the program:
- The program starts by including the necessary header files,
stdio.h
andconio.h
, to allow input and output. - Inside the
main()
function, two integer variables,x
andy
, are declared. - The program then prompts the user to enter an integer value, which is read using the
scanf()
function and stored in the variablex
. - The value of
x
is then shifted left by three bits using the left shift operator<<
. This is achieved by multiplying the value ofx
with2
raised to the power of the number of bits to be shifted left, which in this case is3
. - The shifted value of
x
is then assigned to the variabley
. - Finally, the program prints the value of
y
using theprintf()
function.
As an example, let’s say the user enters the integer value 2
. The binary representation of 2
is 10
. When this value is shifted left by three bits, the resulting binary value is 10000
, which is equal to decimal 16
. Therefore, the program will output 16
.
Thnaks
Write a program to shift inputted data by three bits left.