Thread: Typing decimal number and cover it binary number!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    13

    Lightbulb Typing decimal number and convert it to binary number!

    Hey everyone!
    I am looking for a genius who can help to solve my very easy problem!)

    My kinda homework is: Type a decimal number and the C should spit it out as binary.

    e.g. 15 (decimal)-> 1111 (binary)

    1. I'd create a mask c = 0X800
    2. I would use the operator & to compare the typed number and the mask.
    3. if the value = 0, print 0 and Shift mask 1 to right
    4. Otherwise print 1 Shift mask and number 1 to right

    I am playing with this already for hours.... just no positive result.
    thanks for help and tips.

    Code:
    #include <stdio.h>
    
    void main ()
    
    {
            unsigned short int number; //Typed Number
     	unsigned short int b = 0x8000; // Mask
    
    	
    	
    	printf ("Pleae type a postive number: ");
     	scanf ("%d", &number);
    	 
    	
    
    
     if ( number == 0 )
     { 	printf ("\nResult: 0000.0000.0000.0000");
     }       
          
     
    
     else if ( number != 0 )
     {
    
    
       while ( number & b != 0 );
       { 
    
        printf ("0");
    	b = b >> 1;
       }
    
    
       while ( number & b != 0);
       {
    
       printf ("1");
       b = b >> 1;
       number = number >> 2;
    
    
       }
     
      
     }
    
    
    
     
    	getchar ();
    	getchar ();
    
    
    
    
    
    }
    http://img845.imageshack.us/img845/4589/unledif.jpg
    Last edited by Kazumi; 04-16-2011 at 02:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Number to Decimal
    By theCanuck in forum C++ Programming
    Replies: 12
    Last Post: 02-09-2011, 11:25 PM
  2. Number of digits in a decimal number
    By maverix in forum C Programming
    Replies: 7
    Last Post: 11-04-2007, 12:12 PM
  3. how to remove zero after decimal number
    By abhay_m8 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2007, 02:30 AM
  4. Replies: 9
    Last Post: 10-07-2006, 05:37 AM
  5. How to get the decimal part of a number...
    By Caldus in forum C++ Programming
    Replies: 13
    Last Post: 06-12-2006, 06:41 PM