]> gitweb.factorcode.org Git - factor.git/blob - basis/math/bitwise/bitwise-docs.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / math / bitwise / bitwise-docs.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs help.markup help.syntax math sequences ;
4 IN: math.bitwise
5
6 HELP: bitfield
7 { $values { "values..." "a series of objects" } { "bitspec" "an array" } { "n" integer } }
8 { $description "Constructs an integer from a series of values on the stack together with a bit field specifier, which is an array whose elements have one of the following shapes:"
9     { $list
10         { { $snippet "{ constant shift }" } " - the resulting bit field is bitwise or'd with " { $snippet "constant" } " shifted to the right by " { $snippet "shift" } " bits" }
11         { { $snippet "{ word shift }" } " - the resulting bit field is bitwise or'd with " { $snippet "word" } " applied to the top of the stack; the result is shifted to the right by " { $snippet "shift" } " bits" }
12         { { $snippet "shift" } " - the resulting bit field is bitwise or'd with the top of the stack; the result is shifted to the right by " { $snippet "shift" } " bits" }
13     }
14 "The bit field specifier is processed left to right, so stack values should be supplied in reverse order." }
15 { $examples
16     "Consider the following specification:"
17     { $list
18         { "bits 0-10 are set to the value of " { $snippet "x" } }
19         { "bits 11-14 are set to the value of " { $snippet "y" } }
20         { "bit 15 is always on" }
21         { "bits 16-20 are set to the value of " { $snippet "fooify" } " applied to " { $snippet "z" } }
22     }
23     "Such a bit field construction can be specified with a word like the following:"
24     { $code
25         ": baz-bitfield ( x y z -- n )"
26         "    {"
27         "        { fooify 16 }"
28         "        { 1 15 }"
29         "        11"
30         "        0"
31         "    } ;"
32     }
33 } ;
34
35 HELP: bits 
36 { $values { "m" integer } { "n" integer } { "m'" integer } }
37 { $description "Keep only n bits from the integer m." }
38 { $example "USING: math.bitwise prettyprint ;" "HEX: 123abcdef 16 bits .h" "cdef" } ;
39
40 HELP: bitroll
41 { $values { "x" integer } { "s" "a shift integer" } { "w" "a wrap integer" } { "y" integer }
42 }
43 { $description "Roll n by s bits to the left, wrapping around after w bits." }
44 { $examples
45     { $example "USING: math.bitwise prettyprint ;" "1 -1 32 bitroll .b" "10000000000000000000000000000000" }
46     { $example "USING: math.bitwise prettyprint ;" "HEX: ffff0000 8 32 bitroll .h" "ff0000ff" }
47 } ;
48
49 HELP: bit-clear?
50 { $values
51      { "x" integer } { "n" integer }
52      { "?" "a boolean" }
53 }
54 { $description "Returns " { $link t } " if the nth bit is set to zero." }
55 { $examples 
56     { $example "USING: math.bitwise prettyprint ;"
57                "HEX: ff 8 bit-clear? ."
58                "t"
59     }
60     { $example "" "USING: math.bitwise prettyprint ;"
61                "HEX: ff 7 bit-clear? ."
62                "f"
63     }
64 } ;
65
66 { bit? bit-clear? set-bit clear-bit } related-words
67
68 HELP: bit-count
69 { $values
70      { "x" integer }
71      { "n" integer }
72 }
73 { $description "Returns the number of set bits as an integer." }
74 { $examples 
75     { $example "USING: math.bitwise prettyprint ;"
76                "HEX: f0 bit-count ."
77                "4"
78     }
79     { $example "USING: math.bitwise prettyprint ;"
80                "-7 bit-count ."
81                "2"
82     }
83 } ;
84
85 HELP: bitroll-32
86 { $values
87      { "n" integer } { "s" integer }
88      { "n'" integer }
89 }     
90 { $description "Rolls the number " { $snippet "n" } " by " { $snippet "s" } " bits to the left, wrapping around after 32 bits." }
91 { $examples 
92     { $example "USING: math.bitwise prettyprint ;"
93                "HEX: 1 10 bitroll-32 .h"
94                "400"
95     }
96     { $example "USING: math.bitwise prettyprint ;"
97                "HEX: 1 -10 bitroll-32 .h"
98                "400000"
99     }
100 } ;
101
102 HELP: bitroll-64
103 { $values
104      { "n" integer } { "s" "a shift integer" }
105      { "n'" integer }
106 }
107 { $description "Rolls the number " { $snippet "n" } " by " { $snippet "s" } " bits to the left, wrapping around after 64 bits." }
108 { $examples 
109     { $example "USING: math.bitwise prettyprint ;"
110                "HEX: 1 10 bitroll-64 .h"
111                "400"
112     }
113     { $example "USING: math.bitwise prettyprint ;"
114                "HEX: 1 -10 bitroll-64 .h"
115                "40000000000000"
116     }
117 } ;
118
119 { bitroll bitroll-32 bitroll-64 } related-words
120
121 HELP: clear-bit
122 { $values
123      { "x" integer } { "n" integer }
124      { "y" integer }
125 }
126 { $description "Sets the nth bit of " { $snippet "x" } " to zero." }
127 { $examples
128     { $example "USING: math.bitwise kernel prettyprint ;"
129         "HEX: ff 7 clear-bit .h"
130         "7f"
131     }
132 } ;
133
134 HELP: flags
135 { $values
136      { "values" sequence }
137 }
138 { $description "Constructs a constant flag value from a sequence of integers or words that output integers. The resulting constant is computed at compile-time, which makes this word as efficient as using a literal integer." }
139 { $examples
140     { $example "USING: math.bitwise kernel prettyprint ;"
141         "IN: scratchpad"
142         "CONSTANT: x HEX: 1"
143         "{ HEX: 20 x BIN: 100 } flags .h"
144         "25"
145     }
146 } ;
147
148 HELP: symbols>flags
149 { $values { "symbols" sequence } { "assoc" assoc } { "flag-bits" integer } }
150 { $description "Constructs an integer value by mapping the values in the " { $snippet "symbols" } " sequence to integer values using " { $snippet "assoc" } " and " { $link bitor } "ing the values together." }
151 { $examples
152     { $example "USING: math.bitwise prettyprint ui.gadgets.worlds ;"
153         "IN: scratchpad"
154         "CONSTANT: window-controls>flags H{"
155         "    { close-button 1 }"
156         "    { minimize-button 2 }"
157         "    { maximize-button 4 }"
158         "    { resize-handles 8 }"
159         "    { small-title-bar 16 }"
160         "    { normal-title-bar 32 }"
161         "}"
162         "{ resize-handles close-button small-title-bar } window-controls>flags symbols>flags ."
163         "25"
164     }
165 } ;
166
167 HELP: mask
168 { $values
169      { "x" integer } { "n" integer }
170      { "?" "a boolean" }
171 }
172 { $description "After the operation, only the bits that were set in both the mask and the original number are set." }
173 { $examples
174     { $example "USING: math.bitwise kernel prettyprint ;"
175         "BIN: 11111111 BIN: 101 mask .b"
176         "101"
177     }
178 } ;
179
180 HELP: mask-bit
181 { $values
182      { "m" integer } { "n" integer }
183      { "m'" integer }
184 }
185 { $description "Turns off all bits besides the nth bit." }
186 { $examples
187     { $example "USING: math.bitwise kernel prettyprint ;"
188         "HEX: ff 2 mask-bit .b"
189         "100"
190     }
191 } ;
192
193 HELP: mask?
194 { $values
195      { "x" integer } { "n" integer }
196      { "?" "a boolean" }
197 }
198 { $description "Returns true if all of the bits in the mask " { $snippet "n" } " are set in the integer input " { $snippet "x" } "." }
199 { $examples
200     { $example "USING: math.bitwise kernel prettyprint ;"
201         "HEX: ff HEX: f mask? ."
202         "t"
203     }
204
205     { $example "USING: math.bitwise kernel prettyprint ;"
206         "HEX: f0 HEX: 1 mask? ."
207         "f"
208     }
209 } ;
210
211 HELP: on-bits
212 { $values
213      { "n" integer }
214      { "m" integer }
215 }
216 { $description "Returns an integer with " { $snippet "n" } " bits set." }
217 { $examples
218     { $example "USING: math.bitwise kernel prettyprint ;"
219         "6 on-bits .h"
220         "3f"
221     }
222     { $example "USING: math.bitwise kernel prettyprint ;"
223         "64 on-bits .h"
224         "ffffffffffffffff"
225     }
226 } ;
227
228 HELP: toggle-bit
229 { $values
230      { "m" integer }
231      { "n" integer }
232      { "m'" integer }
233 }
234 { $description "Toggles the nth bit of an integer." }
235 { $examples
236     { $example "USING: math.bitwise kernel prettyprint ;"
237         "0 3 toggle-bit .b"
238         "1000"
239     }
240     { $example "USING: math.bitwise kernel prettyprint ;"
241         "BIN: 1000 3 toggle-bit .b"
242         "0"
243     }
244 } ;
245
246 HELP: set-bit
247 { $values
248      { "x" integer } { "n" integer }
249      { "y" integer }
250 }
251 { $description "Sets the nth bit of " { $snippet "x" } "." }
252 { $examples
253     { $example "USING: math.bitwise kernel prettyprint ;"
254         "0 5 set-bit .h"
255         "20"
256     }
257 } ;
258
259 HELP: shift-mod
260 { $values
261      { "n" integer } { "s" integer } { "w" integer }
262      { "n" integer }
263 }
264 { $description "Calls " { $link shift } " on " { $snippet "n" } " and " { $snippet "s" } ", wrapping the result to " { $snippet "w" } " bits." } ;
265
266 HELP: unmask
267 { $values
268      { "x" integer } { "n" integer }
269      { "?" "a boolean" }
270 }
271 { $description "Clears the bits in " { $snippet "x" } " if they are set in the mask " { $snippet "n" } "." }
272 { $examples
273     { $example "USING: math.bitwise kernel prettyprint ;"
274         "HEX: ff  HEX: 0f unmask .h"
275         "f0"
276     }
277 } ;
278
279 HELP: unmask?
280 { $values
281      { "x" integer } { "n" integer }
282      { "?" "a boolean" }
283 }
284 { $description "Tests whether unmasking the bits in " { $snippet "x" } " would return an integer greater than zero." }
285 { $examples
286     { $example "USING: math.bitwise kernel prettyprint ;"
287         "HEX: ff  HEX: 0f unmask? ."
288         "t"
289     }
290 } ;
291
292 HELP: w*
293 { $values
294      { "int" integer } { "int" integer }
295      { "int" integer }
296 }
297 { $description "Multiplies two integers and wraps the result to 32 bits." }
298 { $examples
299     { $example "USING: math.bitwise kernel prettyprint ;"
300         "HEX: ffffffff HEX: 2 w* ."
301         "4294967294"
302     }
303 } ;
304
305 HELP: w+
306 { $values
307      { "int" integer } { "int" integer }
308      { "int" integer }
309 }
310 { $description "Adds two integers and wraps the result to 32 bits." }
311 { $examples
312     { $example "USING: math.bitwise kernel prettyprint ;"
313         "HEX: ffffffff HEX: 2 w+ ."
314         "1"
315     }
316 } ;
317
318 HELP: w-
319 { $values
320      { "int" integer } { "int" integer }
321      { "int" integer }
322 }
323 { $description "Subtracts two integers and wraps the result to 32 bits." }
324 { $examples
325     { $example "USING: math.bitwise kernel prettyprint ;"
326         "HEX: 0 HEX: ff w- ."
327         "4294967041"
328     }
329 } ;
330
331 HELP: wrap
332 { $values
333      { "m" integer } { "n" integer }
334      { "m'" integer }
335 }
336 { $description "Wraps an integer " { $snippet "m" } " by modding it by " { $snippet "n" } ". This word is uses bitwise arithmetic and does not actually call the modulus word, and as such can only mod by powers of two." }
337 { $examples "Equivalent to modding by 8:"
338     { $example 
339         "USING: math.bitwise prettyprint ;"
340         "HEX: ffff 8 wrap .h"
341         "7"
342     }
343 } ;
344
345 ARTICLE: "math-bitfields" "Constructing bit fields"
346 "Some applications, such as binary communication protocols and assemblers, need to construct integers from elaborate bit field specifications. Hand-coding this using " { $link shift } " and " { $link bitor } " results in repetitive code. A higher-level facility exists to factor out this repetition:"
347 { $subsection bitfield } ;
348
349 ARTICLE: "math.bitwise" "Additional bitwise arithmetic"
350 "The " { $vocab-link "math.bitwise" } " vocabulary provides bitwise arithmetic words extending " { $link "bitwise-arithmetic" } ". They are useful for efficiency, low-level programming, and interfacing with C libraries."
351 $nl
352 "Setting and clearing bits:"
353 { $subsection set-bit }
354 { $subsection clear-bit }
355 "Testing if bits are set or clear:"
356 { $subsection bit? }
357 { $subsection bit-clear? }
358 "Operations with bitmasks:"
359 { $subsection mask }
360 { $subsection unmask }
361 { $subsection mask? }
362 { $subsection unmask? }
363 "Generating an integer with n set bits:"
364 { $subsection on-bits }
365 "Counting the number of set bits:"
366 { $subsection bit-count }
367 "More efficient modding by powers of two:"
368 { $subsection wrap }
369 "Bit-rolling:"
370 { $subsection bitroll }
371 { $subsection bitroll-32 }
372 { $subsection bitroll-64 }
373 "32-bit arithmetic:"
374 { $subsection w+ }
375 { $subsection w- }
376 { $subsection w* }
377 "Bitfields:"
378 { $subsection flags }
379 { $subsection "math-bitfields" } ;
380
381 ABOUT: "math.bitwise"