showbits(int)

This is a discussion on showbits(int) within the C Programming forums, part of the General Programming Boards category; There is a bitwize function showbits(int) in C language which displays what is saved in individual bit of integer. I ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    showbits(int)

    There is a bitwize function showbits(int) in C language which displays what is saved in individual bit of integer. I am trying it from hours but it is not working if any one know which header file have to be used then please tell.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    I believe that showbits() is not a standard C function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    What to use to display individual bit content

    Quote Originally Posted by laserlight View Post
    I believe that showbits() is not a standard C function.
    If showbits() is not a standard function then is there any other alternative for same.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    If showbits() is not a standard function then is there any other alternative for same.
    I am guessing that it returns or prints the binary representation of an integer. That should be rather trivial to write, methinks.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by Bhushan View Post
    There is a bitwize function showbits(int) in C language which displays what is saved in individual bit of integer. I am trying it from hours but it is not working if any one know which header file have to be used then please tell.
    Hours? You've been trying to call a nonexistent function for HOURS?

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I've never heard of showbits(), but this will print the binary value of an integer to the screen:
    Code:
    void PrintBin(unsigned int x)
    {
        int i; 
        for(i=(sizeof(int)*8); i>=0; i--)
            (x&(1<<i))?putchar('1'):putchar('0');
    }
    Last edited by mike_g; 07-27-2007 at 09:57 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void PrintBin(unsigned int x)
    {
        int i; 
        for(i=(sizeof(int)*8)-1; i>=0; i--)
            (x&(1<<i))?putchar('1'):putchar('0');
    }
    Fix: Note -1 on the i initializer... a 32-bit (4 * 8 bit) integer has bits from 31..0, not 32..0...

    --
    Mats

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    For portability you might want to compute the number of bits in UINT_MAX by right-shifting until you get 0, since it's not always true that the number of bits in a byte is 8, nor that all the bits in an unsigned int are value bits.

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by matsp
    Fix: Note -1 on the i initializer... a 32-bit (4 * 8 bit) integer has bits from 31..0, not 32..0...
    Oh yeah, that was quite dumb of me. I did it right, then edited it to take the -1 out because for some reason I though it was wrong o_0.

    Quote Originally Posted by robatino
    For portability you might want to compute the number of bits in UINT_MAX by right-shifting until you get 0, since it's not always true that the number of bits in a byte is 8, nor that all the bits in an unsigned int are value bits.
    Yeah that should be better. Just out of curiosity is there any way to get the size in bits of a Byte?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Yes. CHAR_BIT should have it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Ok, thanks

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > Yeah that should be better. Just out of curiosity is there any way to get the size in bits of a Byte?
    <limits.h> gives you both UINT_MAX (maximum value of unsigned int) and CHAR_BIT (number of bits in a byte). But although sizeof(unsigned int)*CHAR_BIT is always the number of bits in an unsigned int (and you could shorten that to sizeof(int)*CHAR_BIT since the signed and unsigned versions are always the same size) it's not guaranteed that all of those bits are value bits - for example, even if the number of bits is 32, it could happen that only 23 of those are value bits, so UINT_MAX would be equal to 2^23 - 1, not 2^32 - 1 as you would expect. The whole business of "value bits" and "padding bits" is something it's not easy to get information on - all I've found are newsgroup postings of people quoting the Standard at each other. But basically, UINT_MAX is equal to 2^N - 1, where N is equal to the number of value bits, which is <= the number of bits. To find the number of bits in UINT_MAX, you can do something like
    Code:
    unsigned int j = UINT_MAX, n = 0;
    while (j >>= 1) ++n;
    and then initialize i = n-1.

    Edit: Actually, that should be i = n.
    Last edited by robatino; 07-27-2007 at 12:39 PM.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    As merely a side note on what has already been mentioned, you don't really need to know the exact number of value bits in the int. By using a bitmask as the loop "counter", you can achieve the same effect. My preferred method is along this line:
    Code:
    void showbits(unsigned int value)
    {
       unsigned int bit;
       for ( bit = /* msb */(-1U >> 1) + 1; bit > 0; bit >>= 1 )
       {
          putchar(value & bit ? '1' : '0');
       }
       putchar('\n');
    }
    If anyone is interested in more of me babbling on this, here you go.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    That's much more elegant and efficient than mine. I didn't like having a separate loop.

  15. #15
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Hey thats cool I read your article about how this part works: bit=(-1U >> 1)+1

    One thing I am unfamiliar with is the U by the number. Here it seems as if it treats an unsigned int as if it was signed. Is that what the U does?

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

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