getbits.c
changeset 130 f4b424b93e45
child 132 54a04f5f141d
equal deleted inserted replaced
129:d82830a449a9 130:f4b424b93e45
       
     1 /**
       
     2  * getbits.c
       
     3  * Copyright (C) 2010 Markus Broeker
       
     4  *
       
     5  */
       
     6 #include <stdio.h>
       
     7 #include <limits.h>
       
     8 #include <string.h>
       
     9 
       
    10 #define BITS 64
       
    11 
       
    12 char *getbits (unsigned long long what)
       
    13 {
       
    14     static char byte[BITS + 1];
       
    15 
       
    16     short i = BITS;
       
    17 
       
    18     memset (byte, '0', BITS);
       
    19 
       
    20     while (what > 0) {
       
    21         byte[--i] = (what % 2) ? '1' : '0';
       
    22         what >>= 1;
       
    23     }
       
    24     byte[BITS] = '\0';
       
    25 
       
    26     return byte;
       
    27 }
       
    28 
       
    29 int main (void)
       
    30 {
       
    31     unsigned int i;
       
    32     for (i = 1; i < 16; i++)
       
    33         printf ("%s\n", getbits (i));
       
    34 
       
    35     printf ("%s\n", getbits (USHRT_MAX));
       
    36     printf ("%s\n", getbits (UINT_MAX));
       
    37     printf ("%s\n", getbits (ULONG_MAX));
       
    38 
       
    39 #ifdef ULLONG_MAX
       
    40     printf ("%s\n", getbits (ULLONG_MAX));
       
    41 #endif
       
    42 
       
    43     return 0;
       
    44 }