Decimal to Binary Conversion program in C

Decimal-to-Binary-conversion-program-in-c

Program Introduction :

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

Here are a 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:

Input:

Output:

The decimal number 100 in Binary.

Example 2:

Input:

Output:

Example 3:

Input:

Output:

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 the 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 the 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.

Output:

Now the output is an 8 Bit spaced binary number.

Conclusion:

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

Related Articles:

Related Reading:

Venkatesh

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...

10 Responses

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

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

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

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

  5. […] C Program to Convert Decimal Number to Binary Number […]

  6. […] C Program to Convert Decimal Number to Binary Number […]

  7. […] C Program to Convert Decimal Number to Binary Number […]

  8. […] C Program to Convert Decimal Number to Binary Number […]

  9. […] C Program to Convert Decimal Number to Binary Number […]

  10. […] C Program to Convert Decimal Number to Binary Number […]

Leave a Reply