binary based output

This is a discussion on binary based output within the C Programming forums, part of the General Programming Boards category; hello, Is there any simple way to show output in bin base. something like.... Code: int digit = 10; printf("%s",DecToBin(digit)); ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    2

    binary based output

    hello,

    Is there any simple way to show output in bin base.

    something like....

    Code:
    int digit = 10;
    printf("%s",DecToBin(digit));
    expected output:
    1010


    I use gcc.

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Depends what you mean by simple. For converting an unsigned datatype to binary you could just use bit-shifting to find each individual bit in the variable and stick them in a string of characters.
    Code:
    void convertToString(unsigned long input, char* output)
    {
       int i;
       for(i = 0; i < 32; i++) //assuming unsigned long is 32 bits. Don't shoot me, Prelude! :D
       {
    	 output[i] = input & (1 << (31 - i)) ? '1' : '0';
       }
    }
    Something to that effect. I didn't test it, so it might not work entirely properly, but that's the gist of it.

    **EDIT**
    Note: Assuming that an unsigned long is 32 bits is bad programming practice
    Last edited by Hunter2; 09-20-2004 at 07:25 PM. Reason: Wasn't sure if it was valid C. Modified a bit..
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there any simple way to show output in bin base.
    Sure. So simple that it can be done in one line.
    Code:
    #include <stdio.h>
    
    int dtob ( int d )
    {
      return d ? dtob ( d >> 1U ), putchar ( d & 1U | '0' ) : 0;
    }
    
    int main ( void )
    {
      dtob ( 10 );
    
      return 0;
    }
    >//assuming unsigned long is 32 bits. Don't shoot me, Prelude!
    Just as long as you mention that it's a bad practice somewhere in your post.
    My best code is written with the delete key.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Sure. So simple that it can be done in one line.
    Woh. So... you wanna comment that line?

    >>Just as long as you mention that it's a bad practice somewhere in your post.
    Done.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    I see what that function does.. but what is the U for?

    btw, wonderful use of the ternary operator and recursion.. very impressive.

    [edit]
    Ok, I also got lost when you or it with '0'.. isnt that 0x30 hex?
    hmmmmmmmmmmmmmm.
    Last edited by Vicious; 09-20-2004 at 07:33 PM.
    What is C++?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Code:
    putchar ( d & 1U | '0' )
    ...So you get 0x30 or 0x31 ('0' or '1'). You could rewrite it as:
    Code:
    putchar ( ( d & 1U ) + '0' )
    ...if it makes more sense to you.

    The U is for Unsigned (I think).
    If you understand what you're doing, you're not learning anything.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, I now get what the line does. But why does dtob have to return a value?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    So it knows when to stop recursing?

    Is recursing a real word?
    What is C++?

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Is recursing a real word?
    probably not

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>So it knows when to stop recursing?
    No, it knows that when d == 0...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Yeah, it returns 0 and quits the function, else it calls it self again (recurses).

    I think she did that to show it could be done in one line.
    What is C++?

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    void dtob ( int d )
    {
      d ? dtob ( d >> 1U ), putchar ( d & 1U | '0' ) : 0;  //I think that works...
    }
    void dtob ( int d )
    {
      if(d) dtob ( d >> 1U ), putchar ( d & 1U | '0');  //I think that works...
    }
    void dtob ( int d )
    {
      int dummy = d ? dtob ( d >> 1U ), putchar ( d & 1U | '0' ) : 0;  //That works
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ah I see... then um.. I dunno.

    I'm sure there is a reason behind it since it's Prelude. Probably some kind of "good habit" lesson in there somewhere.
    What is C++?

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Probably some kind of "good habit" lesson in there somewhere.
    I suppose so, even in writing code that breaks just about every readability rule there is Prelude will stick to her good habits
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    2
    thank you all,

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  5. Binary Output
    By Ajsan in forum C++ Programming
    Replies: 9
    Last Post: 04-23-2004, 04:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21