1 | /* +++Date last modified: 05-Jul-1997 */ |
---|
2 | |
---|
3 | /* |
---|
4 | ** Macros and prototypes for bit operations |
---|
5 | ** |
---|
6 | ** public domain for SNIPPETS by: |
---|
7 | ** Scott Dudley |
---|
8 | ** Auke Reitsma |
---|
9 | ** Ratko Tomic |
---|
10 | ** Aare Tali |
---|
11 | ** J. Blauth |
---|
12 | ** Bruce Wedding |
---|
13 | ** Bob Stout |
---|
14 | */ |
---|
15 | |
---|
16 | #ifndef BITOPS__H |
---|
17 | #define BITOPS__H |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | #include <stdlib.h> /* For size_t */ |
---|
21 | #include <limits.h> /* For CHAR_BIT */ |
---|
22 | #include "bitcount-sniptype.h" /* For TOBOOL() */ |
---|
23 | #include "bitcount-extkword.h" /* For CDECL */ |
---|
24 | |
---|
25 | /* |
---|
26 | ** Macros to manipulate bits in any integral data type. |
---|
27 | */ |
---|
28 | |
---|
29 | #define BitSet(arg,posn) ((arg) | (1L << (posn))) |
---|
30 | #define BitClr(arg,posn) ((arg) & ~(1L << (posn))) |
---|
31 | #define BitFlp(arg,posn) ((arg) ^ (1L << (posn))) |
---|
32 | #define BitTst(arg,posn) TOBOOL((arg) & (1L << (posn))) |
---|
33 | |
---|
34 | /* |
---|
35 | ** Macros to manipulate bits in an array of char. |
---|
36 | ** These macros assume CHAR_BIT is one of either 8, 16, or 32. |
---|
37 | */ |
---|
38 | |
---|
39 | #define MASK CHAR_BIT-1 |
---|
40 | #define SHIFT ((CHAR_BIT==8)?3:(CHAR_BIT==16)?4:8) |
---|
41 | |
---|
42 | #define BitOff(a,x) ((void)((a)[(x)>>SHIFT] &= ~(1 << ((x)&MASK)))) |
---|
43 | #define BitOn(a,x) ((void)((a)[(x)>>SHIFT] |= (1 << ((x)&MASK)))) |
---|
44 | #define BitFlip(a,x) ((void)((a)[(x)>>SHIFT] ^= (1 << ((x)&MASK)))) |
---|
45 | #define IsBit(a,x) ((a)[(x)>>SHIFT] & (1 << ((x)&MASK))) |
---|
46 | |
---|
47 | /* |
---|
48 | ** BITARRAY.C |
---|
49 | */ |
---|
50 | |
---|
51 | char *alloc_bit_array(size_t bits); |
---|
52 | int getbit(char *set, int number); |
---|
53 | void setbit(char *set, int number, int value); |
---|
54 | void flipbit(char *set, int number); |
---|
55 | |
---|
56 | /* |
---|
57 | ** BITFILES.C |
---|
58 | */ |
---|
59 | |
---|
60 | typedef struct { |
---|
61 | FILE * file; /* for stream I/O */ |
---|
62 | char rbuf; /* read bit buffer */ |
---|
63 | char rcnt; /* read bit count */ |
---|
64 | char wbuf; /* write bit buffer */ |
---|
65 | char wcnt; /* write bit count */ |
---|
66 | } bfile; |
---|
67 | |
---|
68 | bfile * bfopen(char *name, char *mode); |
---|
69 | int bfread(bfile *bf); |
---|
70 | void bfwrite(int bit, bfile *bf); |
---|
71 | void bfclose(bfile *bf); |
---|
72 | |
---|
73 | /* |
---|
74 | ** BITSTRNG.C |
---|
75 | */ |
---|
76 | |
---|
77 | void bitstring(char *str, long byze, int biz, int strwid); |
---|
78 | |
---|
79 | /* |
---|
80 | ** BSTR_I.C |
---|
81 | */ |
---|
82 | |
---|
83 | unsigned int bstr_i(char *cptr); |
---|
84 | |
---|
85 | /* |
---|
86 | ** BITCNT_1.C |
---|
87 | */ |
---|
88 | |
---|
89 | int CDECL bit_count(long x); |
---|
90 | |
---|
91 | /* |
---|
92 | ** BITCNT_2.C |
---|
93 | */ |
---|
94 | |
---|
95 | int CDECL bitcount(long i); |
---|
96 | |
---|
97 | /* |
---|
98 | ** BITCNT_3.C |
---|
99 | */ |
---|
100 | |
---|
101 | int CDECL ntbl_bitcount(long int x); |
---|
102 | int CDECL BW_btbl_bitcount(long int x); |
---|
103 | int CDECL AR_btbl_bitcount(long int x); |
---|
104 | |
---|
105 | /* |
---|
106 | ** BITCNT_4.C |
---|
107 | */ |
---|
108 | |
---|
109 | int CDECL ntbl_bitcnt(long x); |
---|
110 | int CDECL btbl_bitcnt(long x); |
---|
111 | |
---|
112 | #endif /* BITOPS__H */ |
---|