ビットごとに印刷


https://stackoverflow.com/questions/1697425/how-to-print-out-each-bit-of-a-floating-point-number
1メモリのビットを直接出力しました
各出力にはcharが1つずつ出力され、16進数に出力されます.
やはりバイナリがないので、自分で16進数を2進数に変換しましょう.
static void printme(void *c, size_t n)
{
  unsigned char *t = c;
  if (c == NULL)
    return;
  while (n > 0) {
    --n;
    printf("%02x", t[n]);
  }
  printf("
"); } void fpp(float f, double d) { printme(&f, sizeof f); printme(&d, sizeof d); }

 
 2
float myfloat = 254940.4394f;
printf("0x%p", *(void**)(&myfloat));

3自己変換、精度喪失なし
#include 
#include 

void output_binary_fp_number(double arg)
{
    double pow2;

    if ( arg < 0 ) { putchar('-'); arg = -arg; }
    if ( arg - arg != 0 ) {
        printf("Inf");
    }
    else {
        /* compare and subtract descending powers of two, printing a binary digit for each */
        /* first figure out where to start */
        for ( pow2 = 1; pow2 * 2 <= arg; pow2 *= 2 ) ;
        while ( arg != 0 || pow2 >= 1 ) {
            if ( pow2 == .5 ) putchar('.');
            if ( arg < pow2 ) putchar('0');
            else {
                putchar('1');
                arg -= pow2;
            }
            pow2 *= .5;
        }
    }

    putchar('
'); return; } void usage(char *progname) { fprintf(stderr, "Usage: %s real-number
", progname); exit(EXIT_FAILURE); } int main(int argc, char **argv) { double arg; char *endp; if ( argc != 2 ) usage(argv[0]); arg = strtod(argv[1], &endp); if ( endp == argv[1] || *endp ) usage(argv[0]); output_binary_fp_number(arg); return EXIT_SUCCESS; }

5ビット演算方式
#define IsBitSet(val, bit) ((val) & (1 << (bit)))

/* ... your code ... */

printf ("%c", IsBitSet(bit, 0) ? '1' : '0');
void    print_bits(unsigned char octet)
{
    int z = 128, oct = octet;

    while (z > 0)
    {
        if (oct & z)
            write(1, "1", 1);
        else
            write(1, "0", 1);
        z >>= 1;
    }
}

Including  limits.h  for  CHAR_BIT , allows you to generalize the funciton to allow passing values of any length but limiting the the output to the number of bytes desired. Passing the file descriptor will allow writing to any open descriptor (or simply passing  STDOUT_FILENO  to write to  stdout .
void writebits (const unsigned long v, int fd)
{
    if (!v)  { putchar ('0'); return; };

    size_t sz = sizeof v * CHAR_BIT;
    unsigned long rem = 0;

    while (sz--)
        if ((rem = v >> sz))
            write (fd, (rem & 1) ? "1" : "0", 1);
}

For example:
#include 
#include 

/* CHAR_BIT */
#ifndef CHAR_BIT
# define CHAR_BIT  8
#endif

void writebits (const unsigned long v, int fd)
{
    if (!v)  { putchar ('0'); return; };

    size_t sz = sizeof v * CHAR_BIT;
    unsigned long rem = 0;

    while (sz--)
        if ((rem = v >> sz))
            write (fd, (rem & 1) ? "1" : "0", 1);
}

int main (void) {

    unsigned v = 0xcafebabe;

    writebits (v, STDOUT_FILENO);
    putchar ('
'); writebits ((unsigned char)(v >> 24), STDOUT_FILENO); putchar ('
'); return 0; }

Example Output
$ ./bin/writebits
11001010111111101011101010111110
11001010

 
 
If you want to print bits then used bitwise operator and you can do that...
such example:
for(i=31 ; i>=0 ; i--)
{
  if(num & 1<< i) /* num & 1 << position
   printf("1 ");
  else 
   printf("0 ");
}
printf("
");

 
void print_bits(int n,unsigned int num)
{
   if(n == 31) return;
   print_bits(n+1,num);
   (num & (1<