about summary refs log tree commit diff
path: root/doc/libstddjb/bitarray.html
blob: 9f3bb767979240c453bb8a80955b7942499da3d7 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="en" />
    <title>skalibs: the bitarray library interface</title>
    <meta name="Description" content="skalibs: the bitarray library interface" />
    <meta name="Keywords" content="skalibs c unix bitarray library libstddjb" />
    <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> -->
  </head>
<body>

<p>
<a href="index.html">libstddjb</a><br />
<a href="../libskarnet.html">libskarnet</a><br />
<a href="../index.html">skalibs</a><br />
<a href="//skarnet.org/software/">Software</a><br />
<a href="//skarnet.org/">skarnet.org</a>
</p>

<h1> The <tt>bitarray</tt> library interface </h1>

<p>
 The following functions are declared in the <tt>skalibs/bitarray.h</tt> header,
and implemented in the <tt>libskarnet.a</tt> or <tt>libskarnet.so</tt> library.
</p>

<h2> General information </h2>

<p>
 <tt>bitarray</tt> is a set of primitives to operate efficiently on
large bitfields.
</p>

<p>
 A bitfield is represented by a pre-allocated block of
<tt>unsigned char</tt>; <tt>bitarray</tt> does not care if that
block has been BSS-, stack- or heap-allocated. Bitfields that
can grow in size should be stored in a
<a href="stralloc.html">stralloc</a>.
</p>

<p>
 Bits in a bitfield of length <em>n</em> are numbered from 0 to <em>n</em>-1.
</p>

<h2> Functions </h2>

<p>
<code> size_t bitarray_div8 (size_t n) </code> <br />
Returns the minimum number of bytes needed to store a field of <em>n</em> bits.

</p>

<p>
<code> void bitarray_clearsetn (unsigned char *s, size_t start, size_t len, int h) </code> <br />
Sets (if <em>h</em> is nonzero) or clears (if <em>h</em> is zero)
<em>len</em> bits in field <em>s</em>, starting at bit <em>start</em>.
</p>

<p>
<code> void bitarray_clearn (unsigned char *s, size_t start, size_t len) </code> <br />
Clears <em>len</em> bits in field <em>s</em>, starting at bit <em>start</em>.
</p>

<p>
<code> void bitarray_setn (unsigned char *s, size_t start, size_t len) </code> <br />
Sets <em>len</em> bits in field <em>s</em>, starting at bit <em>start</em>.
</p>

<p>
<code> int bitarray_peek (unsigned char const *s, size_t n) </code> <br />
Returns the value of the <em>n</em>th bit in field <em>s</em>.
</p>

<p>
<code> void bitarray_poke (unsigned char *s, size_t n, int h) </code> <br />
Sets (if <em>h</em> is nonzero) or clears (if <em>h</em> is zero)
the <em>n</em>th bit in field <em>s</em>.
</p>

<p>
<code> void bitarray_clear (unsigned char *s, size_t n) </code> <br />
Clears the <em>n</em>th bit in field <em>s</em>.
</p>

<p>
<code> void bitarray_set (unsigned char *s, size_t n) </code> <br />
Sets the <em>n</em>th bit in field <em>s</em>.
</p>

<p>
<code> int bitarray_testandpoke (unsigned char *s, size_t n, int h) </code> <br />
Sets (if <em>h</em> is nonzero) or clears (if <em>h</em> is zero)
the <em>n</em>th bit in field <em>s</em>,
and returns the previous value of that bit.
</p>

<p>
<code> int bitarray_testandclear (unsigned char *s, size_t n) </code> <br />
Clear the <em>n</em>th bit in field <em>s</em>,
and returns the previous value of that bit.
</p>

<p>
<code> int bitarray_testandset (unsigned char *s, size_t n) </code> <br />
Sets the <em>n</em>th bit in field <em>s</em>,
and returns the previous value of that bit.
</p>

<p>
<code> size_t bitarray_first (unsigned char const *s, size_t len, int h) </code> <br />
Returns the number of the first set (if <em>h</em> is nonzero) or clear
(if <em>h</em> is zero) bit in <em>s</em>, <em>len</em> being
the total number of bits. If all bits in <em>s</em> are the negation of
<em>h</em>, then <em>len</em> is returned.
</p>

<p>
<code> size_t bitarray_firstclear (unsigned char const *s, size_t len) </code> <br />
Returns the number of the first clear bit in <em>s</em>, <em>len</em> being
the total number of bits. If all bits in <em>s</em> are set, <em>len</em> is returned.
</p>

<p>
<code> size_t bitarray_firstclear_skip (unsigned char const *s, size_t len, size_t skip) </code> <br />
Like <tt>bitarray_firstclear</tt>, but the first <em>skip</em> bits are
ignored: the function cannot return less than <em>skip</em>. It is a
programming error if <em>skip</em> &gt; <em>len</em>.
</p>

<p>
<code> size_t bitarray_firstset (unsigned char const *s, size_t len) </code> <br />
Returns the number of the first set bit in <em>s</em>, <em>len</em> being
the total number of bits. If all bits in <em>s</em> are clear, <em>len</em> is returned.
</p>

<p>
<code> size_t bitarray_firstset_skip (unsigned char const *s, size_t len, size_t skip) </code> <br />
Like <tt>bitarray_firstset</tt>, but the first <em>skip</em> bits are
ignored: the function cannot return less than <em>skip</em>. It is a
programming error if <em>skip</em> &gt; <em>len</em>.
</p>

<p>
<code> size_t bitarray_countones (unsigned char const *s, size_t len) </code> <br />
Returns the number of set bits in <em>s</em>, <em>len</em> being the
total number of bits being tested.
</p>

</body>
</html>