Thread: Typing decimal number and cover it binary number!

  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.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    1. Congrats for using CODE tags. However, the purpose of using CODE tags is to retain whitespace and the purpose of whitespace is to make the code easier to follow. Your whitespace is pretty bad - nothing lines up.

    2. Some will tell you your main should be

    int main (void), not
    void main ()


    3. You test for number == 0 twice. Why?
    Last edited by mike65535; 04-16-2011 at 01:56 PM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    sorry, I edited it but still doesnt work.

    I am still trying it over and over again

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Define still doesnt work

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    I attached screenshot.
    Now you can see it in color.

    I would really appreciate a solution

    Edit: sofar we just learned it void main ()

    I dont think this is the mistake
    Last edited by Kazumi; 04-16-2011 at 02:02 PM.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    you only need one while loop. The condition should be, while there is still "mask" left. Inside the loop you test mask against your number. If true '1' else '0'. Reduce mask to test against the next digit.

    Do you fully understand what the bit shift operator does?

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Not sure I'd use the pair of while loops.

    Pseudo code

    Test (next) bit of number using mask
    If set, print '1'
    else print '0'

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    I absolutely understand what the bit shift operator does.
    short int = 16 bit.
    a = 0XF = 1111;

    0XF >> 1 = 0000.0000.0000.0111
    0XF << 1 = 0000.0000.0001.1110


    //Edit: Yea, two loops are probably wrong but now C is doing just nothing... >_<

    by the way, this is 1st Semester 2nd Month task...
    Last edited by Kazumi; 04-16-2011 at 02:14 PM.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Great, then you will be able to solve this in no time! 0x8000 is: 1000 0000 0000 0000

    By shifting this in each iteration of the loop you will get: 0100 0000 0000 0000,

    and so on, until eventually you are left with 0.
    Last edited by Subsonics; 04-16-2011 at 02:15 PM.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    yea, and what was my programm doing? exactly it should do this but it wasnt working thats why I came here

    Now I wrote it with just one loop but it shows just nothing...


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

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    sorry, I edited it but still doesnt work
    That worries me...
    And why the screenshot? To prove what you showed us is what you compiled? Worried again...

    1. Fix your indentation (PLEASE)
    2. Copy and paste it into here with CODE tags so we actually get the code you compiled and ran, not something you transcribed and into which you may have introduced errors.

    I still see two number == 0 tests. Get it?

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Why the while?

    Before you start, you know how many times you are going to test number - it's 16 - so use a for loop.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Ok, here's some pseudo code of a simple approach.

    Code:
    while mask != 0
        if mask & number
            print 1
        else
            print 0
    
        mask >> 1

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    can anyone tell me what is exactly wrong with the upper 2nd code?

    I changed the double ==
    it's doing something but not what I want.

    I mostly doubt if the brackets syntax is correct

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
       if ( number & b != 0)
    	{   printf ("1");
       b = b >> 1;
       number = number >> 1;
       }
    
       else if ( number & b == 0)
       {
        printf ("0");
        b = b >> 1;
       
    	 }
    Well, that defeats the purpose of a mask. AND is going to return the information you need to decide what to print. You shift the mask to move the algorithm forward. The use of the mask will let you keep the number constant.

    I think the only problem then is that you change number unnecessarily in this attempt.

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