Thread: Typing decimal number and cover it binary number!

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Yeah, either shift the number OR shift the mask. Not both.

  2. #17
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    I know NOTHING of masks, however if that's not an exact part of your assignment, this can easily be done with nothing but a printf statement, a for loop and a couple lines for the math.

  3. #18
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    ok whiteflags you are right.
    shifting both makes nonsene.

    But even if i remove that line number = number >> 1;
    its still not working.

    meanwhile I tried to do what Subsonics said:

    the beginning is same:

    b = mask
    number = the positive number you type in

    Code:
    else if ( number != 0 )
    
    
    	 while ( b != 0 )
    	 {
    
    	  if ( b & number == 0)
    	  {
    	  printf ("0");
    	  b = b >> 1;
    	  }
    
    	  else ( b & number != 0 );
    	  printf ("1");
    	  b = b >> 1;
     
    
    
    
    	 }
    
    
    
    
     
    	getchar ();
    	getchar ();
    
    
    
    
    
    }

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Hmmm... everyone's off in the wrong direction here...

    Code:
    char binary[17] = {0};
    
    ... get number from 0 to 65535 from keyboard.
    
    for (int x = 15; x > -1 ; x--)
      { if (number & 1)
           binary[x] = '1';
        else  
           binary[x] = '0';
        number >>= 1; }
    
      printf("%s\n", binary);
    Last edited by CommonTater; 04-16-2011 at 07:39 PM.

  5. #20
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You are missing some braces and you should not use a semicolon after the else case. If you want to keep the condition then it should be "else if", but it's not really necessary after the first test.

    Code:
    while ( b != 0 )
    {
    	if ( b & number == 0)
    	{
    		printf ("0");
    		b = b >> 1;
    	}
    	else if ( b & number != 0 )
    	{
    		printf ("1");
    		b = b >> 1;
    	}
    }
    You can also move the 'b = b >> 1' part to the end of the loop, functionally it's the same however.

    Edit:

    So this, is functionally the same.

    Code:
    while ( b != 0 )
    {
    	if ( b & number == 0)
    	{
    		printf ("0");
    	}
    	else
    	{
    		printf ("1");
    	}
    	b = b >> 1;
    }
    Last edited by Subsonics; 04-16-2011 at 03:40 PM.

  6. #21
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    yea, subsonics but its still not working....

    @CommonTater I dont know what you mean....

    almost 20 responds but problem still have not been solved.... >_<

    Can anyone just code it and then copy it here?

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Kazumi View Post
    @CommonTater I dont know what you mean....
    Did you even try it?

  8. #23
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    I am not that expert. I do not understand your code at all.

    I tried pasting but just syntax error...

  9. #24
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Kazumi View Post
    yea, subsonics but its still not working....
    You actually need to wrap up your condition like this:

    if( (b & number) == 0 )

    if you are going to test it against 0, but post the code.

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Kazumi, you might want to read this about the most significant bit. I'm sorry it's quite technical, but the point is you can't just assume 0x8000 is going to be the most significant anything. I mean, what you're saying is true, concerning the most significant bit and how it relates to the first byte (0x8000) but it's going to be wrong for any piece of information longer than that byte.

    Commontater showed you another way to do it, but you're pretty close using a mask to do this anyway, so it's up to you what to do. We're not "off in the wrong direction" as he puts it.

  11. #26
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    @whiteflags

    I dont have to read it.
    Thats why I use "unsigned"
    if I didnt use unsigned it depends on the compiler.

    you should rather read it =)

  12. #27
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    MUAHAHAHAHHA DONE

    thanks special to Subsonics

    you have to use brackets for & TTTTTTTTTTTT
    if (( number & b ) != 0 )


    So all others completely failed =)
    same beginners as me though 4k posts^^

    here the working code for future noobs

    Working CODE 1
    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 );
    	 }
    
       else if ( ( number & b ) == 0)
       {
        printf ("0");
        b = ( b >> 1 );
       
    	 }
    
       }
    
     }
    
      printf ("\n\nDone by Kazumi =)");
    
     
    	getchar ();
    	getchar ();
    
    
    
    
    
    }
    Last edited by Kazumi; 04-16-2011 at 04:50 PM.

  13. #28
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, you're not really helping by just saying it doesn't work all the time. I mean, if you could show us what you get for output, or ...anything it would help my detective work.

    Assuming you're really working with short integers, and I can trust that this is still your code:
    Code:
     while ( b != 0 )
    	 {
    
    	  if ( b & number == 0)
    	  {
    	  printf ("0");
    	  b = b >> 1;
    	  }
    
    	  else ( b & number != 0 );
    	  printf ("1");
    	  b = b >> 1;
     
    
    
    
    	 }
    The only other thing I think that might be wrong is the scanf call.

    Is it still

    Code:
    scanf("%d", &number);
    and is number still a short? If number is a short, then that call is wrong. Rather it should be

    Code:
    scanf("%hu", &number);
    I can only speculate what happens when the format string doesn't match the type of the argument, but I know that it's undefined behavior.

    So if you actually enter 15 and 00001111 doesn't come out, that could be a reason.
    Last edited by whiteflags; 04-16-2011 at 05:01 PM.

  14. #29
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    It's working now. didnt read my previous code?
    and I also make the 2nd way working.

    No Problem if you know the mistake.

    And how was I supposed to tell a mistake if there wasnt a syntax error?

    Anyway...

    All done yet =)

    *Happy Day*


    WORKING CODE 2
    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  ( b != 0 ) 
    	 {
    
    	  if ( ( b & number ) == 0 )
    	  {
    
    	  printf ("0");
    	  b = ( b >> 1 );
    
    	  }
    
    	  else if ( ( b & number ) != 0 )
    	  {
    
    	  printf ("1");
    	  b = ( b >> 1 );
    
    	  }
    
    
    	 }
    
     }
    	 
    
    
    
    
     
    	getchar ();
    	getchar ();
    
    
    
    
    
    }
    that was Subsonics mention before. =)
    Last edited by Kazumi; 04-16-2011 at 04:50 PM.

  15. #30
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Kazumi View Post
    It's working now. didnt read my previous code?
    and I also make the 2nd way working.

    No Problem if you know the mistake.

    And how was I supposed to tell a mistake if there wasnt a syntax error?
    I'm just saying you weren't telling us everything we could know.

    Compiling: sandbox.c
    C:\Documents and Settings\Owner\My Documents\sandbox\sandbox.c:3:6: warning: return type of 'main' is not 'int'
    C:\Documents and Settings\Owner\My Documents\sandbox\sandbox.c: In function 'main':
    C:\Documents and Settings\Owner\My Documents\sandbox\sandbox.c:12:3: warning: format '%d' expects type 'int *', but argument 2 has type 'short unsigned int *'
    C:\Documents and Settings\Owner\My Documents\sandbox\sandbox.c:27:4: warning: suggest parentheses around comparison in operand of '&'
    C:\Documents and Settings\Owner\My Documents\sandbox\sandbox.c:35:4: warning: suggest parentheses around comparison in operand of '&'
    Process terminated with status 0 (0 minutes, 0 seconds)
    0 errors, 4 warnings
    That was the digest produced by my compiler from your first post, which I did just now. The compiler lays out in black and white, all of your problems. But you never posted such information. So who's responsibility is it to compile what you write? If you didn't get anything like this (although I find that weird, because I don't need warnings on to get this), I suspect you could at least show us what does happen when you execute the program.

    In other words, you could show us WHY it didn't work. So you know, the whole 20 post thread, and the getting privilege of calling me an idiot is rather your own fault.

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