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.
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.
I believe that showbits() is not a standard C function.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I am guessing that it returns or prints the binary representation of an integer. That should be rather trivial to write, methinks.If showbits() is not a standard function then is there any other alternative for same.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Fix: Note -1 on the i initializer... a 32-bit (4 * 8 bit) integer has bits from 31..0, not 32..0...Code:void PrintBin(unsigned int x) { int i; for(i=(sizeof(int)*8)-1; i>=0; i--) (x&(1<<i))?putchar('1'):putchar('0'); }
--
Mats
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.
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.Originally Posted by matsp
Yeah that should be better. Just out of curiosity is there any way to get the size in bits of a Byte?Originally Posted by robatino
Yes. CHAR_BIT should have it.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
> 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
and then initialize i = n-1.Code:unsigned int j = UINT_MAX, n = 0; while (j >>= 1) ++n;
Edit: Actually, that should be i = n.
Last edited by robatino; 07-27-2007 at 12:39 PM.
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:
If anyone is interested in more of me babbling on this, here you go.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'); }
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.*
That's much more elegant and efficient than mine. I didn't like having a separate loop.
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?