Decimal to Binary conversion program in C

Spread the love


Program Introduction :

Write a C Program to convert Decimal Number (Integer) to a Binary number ( Decimal to Binary Conversion).

Here are couple of examples with expected input and output.

📢 Note: We are going to consider the size of the Input as 32 Bits (4 Bytes).

Example 1:



The decimal number 100 in Binary.

Example 2:



Example 3:



Decimal to Binary Conversion C Program Logic:

We are going to use the Bitwise Operators to convert the given Decimal (Integer) number to Binary Number.

The main idea is to go to Each bit and check if the bit is Set or Clear. If the bit is Set means the value of that bit is 1. Similarly, if the bit is Clear, Means the value of the bit is .

As we know the integer has 32 bits ( From Index 0 to Index 31 ) or 4 Bytes ( Integer size is compiler dependent, Let’s say 4 Bytes for this example ). We need to go to each bit and check if the bit is Set or Clear.

We will start from the Most Significant Bit (MSB) i.e 31st Index bit and loop until we reach the Least Significant Bit (LSB) i.e 0th index bit. At each Iteration check the bit value.

Algorithm to check if the Bit is Set or Clear:

We can check if the bit is set or clear by using below bitwise operator logic.

To check if the bit ( 'i'th bit) is Set or clear,

  • Firstly, Left shift the number one (1) bit times (i times). i.e ( 1 << i ).
  • Then perform bitwise and ('&') with the given number. i.e ( n & (1 << i) ).
  • If the result of above operation is 1, Then the Bit is Set. Otherwise, The Bit is Clear.

We will repeat the above steps for all the bits of the given Decimal (Integer) Number.

Decimal to Binary Conversion Program In C:

Note: Please go through the comments for understanding the code better.

Decimal to Binary Program Output:

As you can see from the above output, The binary representation of the decimal number is 32 bit long. And it is very hard to read. Let’s try to improve the readability of the output representation.

What if we add an extra space after every 8 bits. This can improve the readability of Binary numbers. So let’s change the above program to display the Output with spaces after every 8 bits.

Decimal to Binary ( Pretty Print Binary Number) :

We are going to add one extra condition to the above program to prettify the displaying binary number.

We will print extra space after every 8th bit. So that we will get 8 bit spaced binary numbers. Which definitely improves the readability.


Now the output is an 8 Bit spaced binary number.


In this article, We have discussed the Decimal to Integer conversion using the Bitwise operators.

Related Articles:

Related Reading:


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. June 4, 2021

    […] To convert Integer to Binary you can following program – Decimal (Integer) to Binary conversion program in C ( […]

  2. July 29, 2021

    […] Here is a C Program to convert an Integer number to the Binary sequence – Decimal to Binary conversion program in C ( […]

  3. August 7, 2021

    […] Here is a C Program to convert an Integer number to the Binary sequence – Decimal to Binary conversion program in C ( […]

Leave a Reply