Decimal to 8 bit Binary conversion

The binary number has two digits {0,1} is called base-2 number system, while the decimal number system has 10 digits {0,1,2,3,4,5,6,7,8,9} is called base-10 number system. The conversion between decimal (base-10) to 8-bit binary (base-2) is carried out by arithmetic modulus and division operators.

The decimal to binary conversion returns 8-bit binary number result for any decimal value given between 0 to 255.

       for example, if given a decimal value is 19,
          return 8-bit binary result will be 00010011. 

       for example, if given a decimal value is 83,
          return 8-bit binary result will be 01010011. 


decimal to 8-bit binary conversion system

Decimal to 8 bits binary conversion - algorithm

Variable Declaration
Input Read integer decval
Output char bits[9]
Temp var integer n,reminder
   
Loop decval not equal zero
reminder = decval module by 2
bits[n] = reminder
decval = decval divide by 2
n=n+1
End Loop
Output print,reverse(bits)

C program code - decimal to binary

The c programming code converts a decimal number to 8 bit binary numbers using arithmetic operators. The decimal value is read from user and must be between 0 to 255.



#include "stdio.h"
#include "string.h"

int main(void) {
 
 int decval=0; int reminder=0;
 int n=0,k=0,nbits=8; 
 int bits[nbits];
 
 printf ("\n Decimal to 8-bit  Binary conversion \n");
 printf ("\n Enter Decimal value \n");
 scanf("%d",&decval);
 
 for( n=0;n<nbits;n++) 
 {
      reminder= decval %2;
      decval  = decval /2; 
      bits[n] = reminder;
 }
 
 printf ("\n Binary 8-bit : ");
 for( k=0; k<nbits; k++) {
    printf("%d",bits[nbits-k-1]);
    
 }    
 return 0;
}





Decimal to 8 bit Binary conversion c program output


 Decimal to 8-bit Binary conversion
     
 Enter Decimal value 
    15

 Binary 8-bit : 00001111





Download source code

Comments

Popular posts from this blog

Hexadecimal to Binary Conversion

Binary to Decimal conversion -Bitwise Left shift Operator

Convert Octal to Binary