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