3 Replies - 1642 Views - Last Post: 23 December 2010 - 05:21 AM Rate Topic: -----

#1 gumstic  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 mustafa1111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 neo23331  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 23-December 10

Re: Decimal-to-Binary Converter

Posted 23 December 2010 - 05:21 AM

thank you very clear code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1