Cshot's code does not appear to be correct.
Code:
#include <stdio.h>
int main()
{
int i;
int value = 125;
for(i = 0; i < sizeof(int)*8; i++)
printf("%d", (value >> (sizeof(int)*8 /* - 1 */ - i)) & 1);
printf("\n");
return 0;
}
/* my output
10000000000000000000000000111110
*/
I would like to point out that a char does not necessarily contain 8 bits, so the 8 is not very portable.
Also, an integer /may/ include padding bits, so using sizeof(int) * CHAR_BIT is not even fully portable.
Using unsigned types would be preferable when using bit shifts.
And as an individual preference, I like to do sizeof(value) instead of sizeof(int) -- I try to avoid the middleman of assuming the underlying type.