## 3 Replies - 2053 Views - Last Post: 23 December 2010 - 05:21 AMRate Topic:     //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=364187&amp;s=dddeb30e1e9380fb94f96925ff98e047&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gumstic Reputation: 1
• Posts: 44
• Joined: 28-October 06

# Decimal-to-Binary Converter

Posted 03 December 2006 - 01:45 AM

Description: This is a simple Decimal to Binary converter using Shifters & Masking to show the output as a 32bit binary number.
```/*
Decimal to Bianry converter, console application.
A program that uses a loop to convert a decimal number
into a binary number. Also looks for a condition to
exit the program using the input number '9999'.

Author: Gumstic
Version: 1.0
*/

// Include directives.
#include "stdafx.h"
#include "conio.h"

// Start Main function.
int main(int argc, char **argv)
{
//Initialise variables.
int mask = 2147483648; // Mask to use for size of bits.
int numInput; // The input number that will be taken from the user.

// Print a title for program.
printf ("=================================n");
printf ("=                               =n");
printf ("=  Decimal to Binary Converter  =n");
printf ("=                               =n");
printf ("=================================nn");

// Start a Do/While statement to keep the program running,
// prompting for user input until the loop returns '0' from
// within the loop.
do
{
printf ("nnEnter a Decimal number (or 9999 to quit): "); // Prompt user for input.
scanf ("%d", &numInput); // Get user input as decimal digit.

// Print out the calculated result.
printf("nThe Binary equivalent of (%d) is: t", numInput);

// Start a For loop to calculate the binary conversion from
// the decimal input. Initialise variable 'i' with '0'.
// As long as 'i' is smaller than '32' (the size of binary digits)
// increment 'i' by one point.
for (int i = 0; i < 32; i++)
{
// AND the input number with MASK, check if it equals '0'.
// If it equals '0' output a '0'.
// If it is not equal to '0' output a '1'.
if ((numInput & mask) == 0)
{
printf("0");
}

else
{
printf("1");
}
// Check if the input is '9999' and
// if true, quit program.
if (numInput == 9999 )
{
return(0);
}
// Move the input number one position to the Left to
// calculate the next digit of the 32 bits.
numInput = numInput << 1;
}// End For loop
}// End Do/While loop
while(1);// Test Do/While loop if it is true or not (1 = true, 0 = false)
}// End function Main

```

Is This A Good Question/Topic? 0

## Replies To: Decimal-to-Binary Converter

### #2 mariamalik Reputation: 0
• Posts: 1
• Joined: 29-April 09

## Re: Decimal-to-Binary Converter

Posted 15 May 2009 - 01:40 AM

Me Ayesha. is this possible to understand for any student who is learning it for first time. create a short one.

### #3 mustafa1111 Reputation: 0
• Posts: 0
• Joined: 06-December 10

## Re: Decimal-to-Binary Converter

Posted 06 December 2010 - 07:56 AM

Thanks for this clear function Hani Mustafa

### #4 neo23331 Reputation: 0
• Posts: 0
• Joined: 23-December 10

## Re: Decimal-to-Binary Converter

Posted 23 December 2010 - 05:21 AM

thank you very clear code.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }