Binary to Decimal conversion -Bitwise Left shift Operator

Binary to decimal conversion using Bitwise Left-shift Operator - algorithm

Declaration
Input Read char bits[9]
Output integer decval
Temp var integer n=0,len=0
   
  len = strlen(bits)
Loop n=0 to len-1
decval left shift by 1
bits[n] == '1' then decval OR by 1
decval left shift by 1
End Loop
Output print,bits

Binary to Decimal conversion by Left-shift -C programming code

The C programming code converts binary bits into a decimal value by bitwise left-shift operator. The input binary bits are given to the program while it is running and returns a decimal value as conversion result.

 

#include <stdio.h>
#include <string.h>

int main() {


   int decval;
   char bits[9]; int n=0;   
   printf ("\n Bitwise Left-shift Operator Decimal Conversion");
   
   printf("\n Enter a binary bits :" );
   scanf("%s", &bits);
   
   int len = strlen(bits);
   decval=0;
   for (n=0;n<len;n++) 
    { 
      decval = decval <<1;       
      if( bits[n]=='1')
       decval =decval | 1;
    
          
    }
         
   printf("\n Binary bits :%s",bits);
   printf("\n Decimal value :%d",decval);
   return 0;
}

 

Output



  Bitwise Left-shift Operator Decimal Conversion

     Enter a binary bits : : 1111

     Decimal value : 15  





Download source code

Comments

Popular posts from this blog

Hexadecimal to Binary Conversion

Convert Octal to Binary