author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Fri, 27 Dec 2013 16:00:09 +0100 | |
changeset 167 | 7c6b221900bf |
parent 141 | 0b5befeb361e |
permissions | -rw-r--r-- |
130 | 1 |
/** |
2 |
* getbits.c |
|
3 |
* Copyright (C) 2010 Markus Broeker |
|
4 |
*/ |
|
140
05d42a3737a4
Comments corrected: misspellings and other trivial things
Markus Bröker <mbroeker@volpe.spectre.org>
parents:
132
diff
changeset
|
5 |
|
130 | 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 | 8 |
#include <string.h> |
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 | 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 | 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; |
|
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 | 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 | 37 |
|
38 |
return 0; |
|
39 |
} |