File: bitstring.ms

package info (click to toggle)
elk 3.0-6
  • links: PTS
  • area: main
  • in suites: potato, slink
  • size: 4,068 kB
  • ctags: 3,123
  • sloc: ansic: 20,686; lisp: 5,232; makefile: 419; awk: 91; sh: 21
file content (209 lines) | stat: -rw-r--r-- 6,282 bytes parent folder | download | duplicates (11)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
.so ../util/tmac.scheme
.Ul
.TL
Reference Manual for the
.sp .5
Elk Bit String Extension
.AU
Oliver Laumann
.
.Ch "Introduction"
.
.PP
The bit string extension to Elk defines a new data type \f2bitstring\fP
(a sequence of zero or more bits) and functions to create and
manipulate bit strings.
The bits in a bit string are numbered beginning from zero up to the
number of bits minus one; bit number 0 is the
.Ix "least significant bit"
.Ix LSB
least significant bit (LSB), and the highest numbered bit is the
.Ix "most significant bit"
.Ix MSB
most significant bit (MSB).
.PP
The
.Ix "print representation"
print representation of bit strings is introduced by the sequence
`#*'; the bits are printed starting with the most significant bit.
Likewise, in the reader the sequence `#*' introduces a bit string
constant.
.LP
Example:
.Ss
#*0100110111
.sp .5
#*             \f2(empty bit string)\fP
.Se
.
.Ch "Using the Bit String Extension"
.
.PP
To load the bit string extension, evaluate the expression
.Ss
(require 'bitstring)
.Se
.PP
This causes the files
.Ix bitstring.scm
\f2bitstring.scm\fP and
.Ix bitstring.o
\f2bitstring.o\fP to be loaded (\f2bitstring.o\fP must be statically
linked with the interpreter on platforms that do not support dynamic
loading of object files).
.PP
Loading the bit string extension causes the
.Ix feature
features \f2bitstring\fP and \f2bitstring.o\fP to be provided.
.
.Ch "Creating Bit Strings"
.
.Pr make-bitstring length init
.LP
\f2make-bitstring\fP returns a new bit string of the given length.
If init is #t, all bits are initialized to 1; if init is #f, all
bits are initialized to 0.
.
.Pr bitstring-copy bitstring
.LP
This procedure returns a copy of the specified bit string.
.
.Pr bitstring-append bitstring\*1 bitstring\*2
.LP
\f2bitstring-append\fP returns a new bit string holding the
.Ix concatenation
concatenation of the specified bit string arguments.
.
.Ch "Bit String Predicates"
.
.Pr bitstring? obj
.LP
This
.Ix "type predicate"
type predicate returns #t if \f2obj\fP is a bit string, #f otherwise.
.
.Pr bitstring=? bitstring\*1 bitstring\*2
.LP
This procedure returns #t if the bit string arguments are of the same
length and contain the same bits, #f otherwise.
.
.Pr bitstring-zero? bitstring
.LP
\f2bitstring-zero?\fP returns #t if the specified bit string
contains only 0 bits, #f otherwise.
.
.Ch "Integer Conversions"
.
.[[
.Pr unsigned-integer\(mi>bitstring length i
.Pr signed-integer\(mi>bitstring length i
.]]
.LP
Both procedures convert the exact integer argument \f2i\fP into a bit
string of \f2length\fP bits and return the bit string.
\f2length\fP must be large enough to hold the bit string
representation of \f2i\fP.
The integer argument to \f2unsigned-integer->bitstring\fP must be
non-negative.
\f2signed-integer->bitstring\fP uses
.Ix "two's complement"
two's complement representation for negative integers.
.
.[[
.Pr bitstring\(mi>unsigned-integer bitstring
.Pr bitstring\(mi>signed-integer bitstring
.]]
.LP
Both procedures convert the given bit string into an integer.
\f2bitstring->signed-integer\fP interprets the bit string as the
.Ix "two's complement"
two's complement representation of a signed integer.
.
.Ch "Selecting Components of Bit Strings"
.
.Pr bitstring-length bitstring
.LP
This procedure returns the number of bits in the specified bit string.
.
.Pr bitstring-ref bitstring index
.LP
\f2bitstring-ref\fP returns #t if the \f2index\fP-th bit in the
given bit string is 1, #f otherwise.
.
.Pr bitstring-substring bitstring from to
.LP
This procedure returns a new bit string initialized with the bits
of \f2bitstring\fP starting at the index \f2from\fP (inclusive)
and ending at the index \f2to\fP (exclusive).
.
.Ch "Modifying Bit Strings"
.
.Pr bitstring-fill! bitstring init
.LP
This procedure sets all bits in the specified bit string to 1 if
\f2init\fP is #t, or to 0 if \f2init\fP is #f.
It returns the non-printing object.
.
.Pr bitstring-set! bitstring index init
.LP
\f2bitstring-set!\fP sets the \f2index\fP-th bit in the specified
bit string to 1 if \f2init\fP is #t, or to 0 if \f2init\fP is #f.
It returns the non-printing object.
.
.Pr bitstring-move! dst-bitstring src-bitstring
.LP
\f2bitstring-move!\fP destructively copies the contents of the
bit string \f2src-bitstring\fP into \f2dst-bitstring\fP.
Both bit strings must have the same length.
It returns the non-printing object.
.
.Pr bitstring-substring-move! src-bitstring from\*1 to\*1 dst-bitstring from\*2
.LP
This procedure destructively copies the bits from \f2src-bitstring\fP
starting at index \f2from\*1\fP (inclusive) and ending at index \f2to\*1\fP
(exclusive) into \f2dst-bitstring\fP starting at index \f2from\*2\fP
(inclusive).
.Ix overlapping
Overlapping is handled correctly.
The procedure returns the non-printing object.
.
.Ch "Bitwise Logical Operations"
.
.Pr bitstring-not bitstring
.LP
This procedure returns a new bit string initialized to the
bitwise logical negation of the given bit string.
.
.Pr bitstring-not! dst-bitstring src-bitstring
.LP
This procedure destructively overwrites the contents of \f2dst-bitstring\fP
with the bitwise logical negation of the bits in \f2src-bitstring\fP.
Both bit strings must have the same length.
\f2bitstring-not!\fP returns the non-printing object.
.
.[[
.Pr bitstring-and bitstring\*1 bitstring\*2
.Pr bitstring-andnot bitstring\*1 bitstring\*2
.Pr bitstring-or bitstring\*1 bitstring\*2
.Pr bitstring-xor bitstring\*1 bitstring\*2
.]]
.LP
These procedures return a new bit string initialized to the bitwise logical
\f2and\fP (logical \f2and\fP with the negation, logical \f2or\fP,
logical exclusive \f2or\fP, respectively) of the two bit string arguments.
The two bit strings must have the same length.
.
.[[
.Pr bitstring-and! dst-bitstring src-bitstring
.Pr bitstring-or! dst-bitstring src-bitstring
.Pr bitstring-andnot! dst-bitstring src-bitstring
.Pr bitstring-xor! dst-bitstring src-bitstring
.]]
.LP
These procedures are the destructive versions of the four bitwise
logical procedures described above.
They perform the corresponding logical operation on the two bit string
arguments and overwrite the contents of \f2dst-bitstring\fP with the
result.
Both bit strings must have the same length.
These procedures return the non-printing object.