Before going to the program to convert Binary to Decimal using Shift Left operator first let us see what is Left Shift Operator?

Left Shift Operator:

                   Left Shift operator is Binary operator used to shift the bits left and it is denoted by “<<“.

The left operand specifies the value to be shifted. The right operand specifies the number of positions that the bits in the value are to be shifted.

For Example,

               Let us assume the number as 5 and number of positions to be shifted as 1. So it is specified as 5<<1.

First the number is converted into binary form internally.

Thus the binary number equivalent to 5 is:

Binary Number:  0  1  0  1

Now if we shift the bits left by 1 bit, the result is:

Binary Number:  1  0  1  0

Thus when we perform the left shift operation it shifts the bits to the left by adding equivalent amount of 0’s to the right and finally returns the value equivalent to that binary number.

Program code to convert Binary to Decimal using Left Shift operator:

/* Program to convert Binary to Decimal using Left Shift operator */
#include<stdio.h> void main()
{ int n,bnum,digit,dec=0,i=0; clrscr(); printf("n Enter the binary number:"); scanf("%d",&bnum); n=bnum; /*Loop to calculate decimal number for given the binary number*/ while(n!=0) { digit=n%10; dec+=digit<<i; n=n/10; i++; } printf("n The binary equivalent of %d in decimal = %d",bnum,dec); getch();
}

Related: C program to convert Binary to Decimal using the pow() function

Step by Step working of the above Program Code:

  1. Let us assume that the user enters the binary number as 1111.
  2. It assigns the value of dec=0, i=0, bnum=1111.
  3. It assigns the n=bnum (ie. n=1111) and the loop continues till the condition of the while loop is true.

3.1.   n!=0    (1111!=0)    while loop condition is true

digit=n%10                      (digit=1111%10)         So  digit=1
dec+=digit<<i                 (dec=0+(1<<0))           So  dec=1
n=n/10                             (n=1111/10)               So  n=111
i++                                     (i=i+1)                            So  i=2

3.2.   n!=0    (111!=0)    while loop condition is true

digit=n%10                     (digit=111%10)            So  digit=1
dec+=digit<<i                (dec=1+(1<<1))           So  dec=3
n=n/10                            (n=111/10)                  So  n=11
i++                                    (i=i+1)                            So  i=3

3.3.   n!=0    (11!=0)    while loop condition is true

digit=n%10                     (digit=11%10)               So  digit=0
dec+=digit<<i                (dec=3+(1<<2))           So  dec=7
n=n/10                            (n=11/10)                     So  n=1
i++                                    (i=i+1)                             So  i=4

3.4.   n!=0    (1!=0)    while loop condition is true

digit=n%10                     (digit=1%10)                 So  digit=1
dec+=digit<<i                (dec=7+(1<<3))           So  dec=15
n=n/10                            (n=1/10)                       So  n=0
i++                                    (i=i+1)                            So  i=5

3.5.   n!=0    (0!=0)    while loop condition is false

It comes out of the while loop.

  1. Finally it prints The binary equivalent of 1111 in decimal is: 15
  2. Thus the program execution is completed.

Output:

binary to decimal

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

Similar Posts