Dec 122017
Unique bit operation functions in C. From an older Byte article. | |||
---|---|---|---|
File Name | File Size | Zip Size | Zip Type |
BITSTR.C | 978 | 478 | deflated |
BITSTR.DOC | 1793 | 921 | deflated |
BITSTR.H | 261 | 156 | deflated |
Download File BITSTR.ZIP Here
Contents of the BITSTR.DOC file
bitstr.doc: documentation for the "bitstr" C routines
The routines in bitstr.c and bitstr.h arose from an article on pages 34-
43 of the April, 1989 Computer Language issue. I thought that the routines
given there were somewhat inefficient, and so I took the idea and rewrote the
functions into much simpler (and, perhaps, less portable) versions.
The idea behind the routines is to implement, in C, a way to store an
array of "boolean" values, without using entire characters or even bitfields
(whose values must be individually named). Three functions are included in
bitstr.c: clear_bit(), set_bit(), and test_bit. In all three, there are two
parameters: a pointer to one (or more) unsigned ints, and the bit number to be
operated upon.
The array of unsigned ints is the bit-array. The size of an unsigned int,
in bits, is set to 16 in the US macro. Therefore, the number of data values
able to be stored in a one-unsigned int-long array is 16. To store more than
16 values, two or more unsigned ints may be declared, and the pointer should
be passed to the first one.
In memory, the bits are stored in a left-to-right fashion (bit 1 is on
the left) -- at least on i80x86 CPUs, as the correctness of the routines
depends on the orientation of the "(unsigned) 0x0001." Also, unsigned ints are
used because they are, at least on the 8086, 8088, and 80286, the CPU internal
bit width.
I have strived to make these routines as bug-free as possible, and they
have worked well in one of my large programming projects. However, if you do
find an error (or just want to make comments, etc.), I can be contacted:
Erik Mavrinac
5333 Deerwood Dr.
Shingle Springs, CA 95682
CIS [73507,3523]
The routines in bitstr.c and bitstr.h arose from an article on pages 34-
43 of the April, 1989 Computer Language issue. I thought that the routines
given there were somewhat inefficient, and so I took the idea and rewrote the
functions into much simpler (and, perhaps, less portable) versions.
The idea behind the routines is to implement, in C, a way to store an
array of "boolean" values, without using entire characters or even bitfields
(whose values must be individually named). Three functions are included in
bitstr.c: clear_bit(), set_bit(), and test_bit. In all three, there are two
parameters: a pointer to one (or more) unsigned ints, and the bit number to be
operated upon.
The array of unsigned ints is the bit-array. The size of an unsigned int,
in bits, is set to 16 in the US macro. Therefore, the number of data values
able to be stored in a one-unsigned int-long array is 16. To store more than
16 values, two or more unsigned ints may be declared, and the pointer should
be passed to the first one.
In memory, the bits are stored in a left-to-right fashion (bit 1 is on
the left) -- at least on i80x86 CPUs, as the correctness of the routines
depends on the orientation of the "(unsigned) 0x0001." Also, unsigned ints are
used because they are, at least on the 8086, 8088, and 80286, the CPU internal
bit width.
I have strived to make these routines as bug-free as possible, and they
have worked well in one of my large programming projects. However, if you do
find an error (or just want to make comments, etc.), I can be contacted:
Erik Mavrinac
5333 Deerwood Dr.
Shingle Springs, CA 95682
CIS [73507,3523]
December 12, 2017
Add comments