getbits.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Wed, 02 Jun 2010 00:22:32 +0200
changeset 131 b5ad49852adc
parent 130 f4b424b93e45
child 132 54a04f5f141d
permissions -rw-r--r--
check for the return values committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
130
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * getbits.c
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2010 Markus Broeker
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 *
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 */
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <limits.h>
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <string.h>
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#define BITS 64
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
char *getbits (unsigned long long what)
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
{
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
    static char byte[BITS + 1];
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
    short i = BITS;
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    memset (byte, '0', BITS);
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
    while (what > 0) {
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
        byte[--i] = (what % 2) ? '1' : '0';
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
        what >>= 1;
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    }
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    byte[BITS] = '\0';
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    return byte;
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
}
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
int main (void)
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
{
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    unsigned int i;
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    for (i = 1; i < 16; i++)
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        printf ("%s\n", getbits (i));
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
    printf ("%s\n", getbits (USHRT_MAX));
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    printf ("%s\n", getbits (UINT_MAX));
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    printf ("%s\n", getbits (ULONG_MAX));
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
#ifdef ULLONG_MAX
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    printf ("%s\n", getbits (ULLONG_MAX));
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
#endif
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
    return 0;
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
}