getbits.c
author Markus Bröker<broeker.markus@googlemail.com>
Sun, 10 Feb 2019 13:17:01 +0100
changeset 173 374a86886bc5
parent 141 0b5befeb361e
permissions -rw-r--r--
LAST-DIGIT-BUG: INCREMENT before LF
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
 */
140
05d42a3737a4 Comments corrected: misspellings and other trivial things
Markus Bröker <mbroeker@volpe.spectre.org>
parents: 132
diff changeset
     5
130
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
132
54a04f5f141d Adjusted to show side effects and their handling
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 130
diff changeset
     7
#include <stdlib.h>
130
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
132
54a04f5f141d Adjusted to show side effects and their handling
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 130
diff changeset
    10
#define BITS 8
130
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
132
54a04f5f141d Adjusted to show side effects and their handling
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 130
diff changeset
    12
char *getbits (unsigned int what)
130
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;
141
0b5befeb361e getbits: calculates the proper value but showed a different one
Markus Bröker <mbroeker@volpe.spectre.org>
parents: 140
diff changeset
    32
    char *s = strdup (getbits (0));
130
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
132
54a04f5f141d Adjusted to show side effects and their handling
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 130
diff changeset
    34
    for (i = 240; i < 255; i++)
54a04f5f141d Adjusted to show side effects and their handling
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 130
diff changeset
    35
        printf ("%s %s %s %s (%d)\n", s, s, s, getbits (i), i);
54a04f5f141d Adjusted to show side effects and their handling
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 130
diff changeset
    36
    free (s);
130
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
    return 0;
f4b424b93e45 getbits added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
}