1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
/*
** bitstring(): print bit pattern of bytes formatted to string.
**
** By J. Blauth, Sept. 1992. Hereby placed into the public domain.
**
** byze: value to transform to bitstring.
** biz: count of bits to be shown (counted from lowest bit, can be any
** even or odd number).
** strwid: total width the string shall have. Since between every 4 bits a
** blank (0x20) is inserted (not added after lowest bit), width of
** bitformat only is (biz+(biz/4-1)). Bits are printed right aligned,
** positions from highest bit to start of string filled with blanks.
** If value of strwid smaller than space needed to print all bits,
** strwid is ignored (e.g.:
** bitstr(s,b,16,5) results in 19 chars +'\0').
**
** EXAMPLE:
** for (j = 1; j <= 16; j++) { bitstring(s, j, j, 16); puts(s); }
** 1: 1
** 2: 10
** 3: 011
** d: 0 0000 0000 1101
** e: 00 0000 0000 1110
** f: 000 0000 0000 1111
*/
void bitstring(char *str, long byze, int biz, int strwid)
{
int i, j;
j = strwid - (biz + (biz >> 2)- (biz % 4 ? 0 : 1));
for (i = 0; i < j; i++)
*str++ = ' ';
while (--biz >= 0)
{
*str++ = ((byze >> biz) & 1) + '0';
if (!(biz % 4) && biz)
*str++ = ' ';
}
*str = '\0';
}
#ifdef TEST
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char s[80]; long j;
for (j = 1L; j <= 16L; j++)
{
bitstring(s, (long)j, (int)j, 16);
printf("%2ld: %s\n", j, s);
}
return EXIT_SUCCESS;
}
#endif
|