May 16, 2020

C program using bitwise left shift(<<)

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


Input from user:


Enter the number:15

Enter how many numbers you wan't to left shift:1

Expected Output:

Output after performing left shift operation is: 30





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


 Firstly you need to know how work left shift operator.




  •  It  takes two numbers and left 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=1.

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

 2. Binary representation of no<<s is-


      no    = 00001111
------------------------------------ 
 no<<s   = 000011110 =30(in decimal)

3. After performing left shift operation 8 bit binary representation is 00001 1110 which is 30 in decimal.

4. So output of the program is  30.



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 left shift\n");
scanf("%d",&s);
printf("Output after performing left-shift operation is:%d",no<<s);

}


Above program shows the following output:


No comments:

Post a Comment

If you have any doubts, please discuss here...