]> gitweb.factorcode.org Git - factor.git/blob - basis/math/bitwise/bitwise-docs.factor
Use flags{ instead of flags all over the place
[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 kernel ;
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      { "obj" object }
71      { "n" integer }
72 }
73 { $description "Returns the number of set bits as an object. This word only works on non-negative integers or objects that can be represented as a byte-array." }
74 { $examples 
75     { $example "USING: math.bitwise prettyprint ;"
76                "HEX: f0 bit-count ."
77                "4"
78     }
79     { $example "USING: math.bitwise prettyprint ;"
80                "-1 32 bits bit-count ."
81                "32"
82     }
83     { $example "USING: math.bitwise prettyprint ;"
84                "B{ 1 0 1 } bit-count ."
85                "2"
86     }
87 } ;
88
89 HELP: bitroll-32
90 { $values
91      { "m" integer } { "s" integer }
92      { "n" integer }
93 }     
94 { $description "Rolls the number " { $snippet "m" } " by " { $snippet "s" } " bits to the left, wrapping around after 32 bits." }
95 { $examples 
96     { $example "USING: math.bitwise prettyprint ;"
97                "HEX: 1 10 bitroll-32 .h"
98                "400"
99     }
100     { $example "USING: math.bitwise prettyprint ;"
101                "HEX: 1 -10 bitroll-32 .h"
102                "400000"
103     }
104 } ;
105
106 HELP: bitroll-64
107 { $values
108      { "m" integer } { "s" "a shift integer" }
109      { "n" integer }
110 }
111 { $description "Rolls the number " { $snippet "m" } " by " { $snippet "s" } " bits to the left, wrapping around after 64 bits." }
112 { $examples 
113     { $example "USING: math.bitwise prettyprint ;"
114                "HEX: 1 10 bitroll-64 .h"
115                "400"
116     }
117     { $example "USING: math.bitwise prettyprint ;"
118                "HEX: 1 -10 bitroll-64 .h"
119                "40000000000000"
120     }
121 } ;
122
123 { bitroll bitroll-32 bitroll-64 } related-words
124
125 HELP: clear-bit
126 { $values
127      { "x" integer } { "n" integer }
128      { "y" integer }
129 }
130 { $description "Sets the nth bit of " { $snippet "x" } " to zero." }
131 { $examples
132     { $example "USING: math.bitwise kernel prettyprint ;"
133         "HEX: ff 7 clear-bit .h"
134         "7f"
135     }
136 } ;
137
138 HELP: symbols>flags
139 { $values { "symbols" sequence } { "assoc" assoc } { "flag-bits" integer } }
140 { $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." }
141 { $examples
142     { $example "USING: math.bitwise prettyprint ui.gadgets.worlds ;"
143         "IN: scratchpad"
144         "CONSTANT: window-controls>flags H{"
145         "    { close-button 1 }"
146         "    { minimize-button 2 }"
147         "    { maximize-button 4 }"
148         "    { resize-handles 8 }"
149         "    { small-title-bar 16 }"
150         "    { normal-title-bar 32 }"
151         "}"
152         "{ resize-handles close-button small-title-bar } window-controls>flags symbols>flags ."
153         "25"
154     }
155 } ;
156
157 HELP: mask
158 { $values
159      { "x" integer } { "n" integer }
160      { "?" "a boolean" }
161 }
162 { $description "After the operation, only the bits that were set in both the mask and the original number are set." }
163 { $examples
164     { $example "USING: math.bitwise kernel prettyprint ;"
165         "BIN: 11111111 BIN: 101 mask .b"
166         "101"
167     }
168 } ;
169
170 HELP: mask-bit
171 { $values
172      { "m" integer } { "n" integer }
173      { "m'" integer }
174 }
175 { $description "Turns off all bits besides the nth bit." }
176 { $examples
177     { $example "USING: math.bitwise kernel prettyprint ;"
178         "HEX: ff 2 mask-bit .b"
179         "100"
180     }
181 } ;
182
183 HELP: mask?
184 { $values
185      { "x" integer } { "n" integer }
186      { "?" "a boolean" }
187 }
188 { $description "Returns true if all of the bits in the mask " { $snippet "n" } " are set in the integer input " { $snippet "x" } "." }
189 { $examples
190     { $example "USING: math.bitwise kernel prettyprint ;"
191         "HEX: ff HEX: f mask? ."
192         "t"
193     }
194
195     { $example "USING: math.bitwise kernel prettyprint ;"
196         "HEX: f0 HEX: 1 mask? ."
197         "f"
198     }
199 } ;
200
201 HELP: even-parity?
202 { $values
203     { "obj" object }
204     { "?" boolean }
205 }
206 { $description "Returns true if the number of set bits in an object is even." } ;
207
208 HELP: odd-parity?
209 { $values
210     { "obj" object }
211     { "?" boolean }
212 }
213 { $description "Returns true if the number of set bits in an object is odd." } ;
214
215 HELP: on-bits
216 { $values
217      { "m" integer }
218      { "n" integer }
219 }
220 { $description "Returns an integer with " { $snippet "m" } " bits set." }
221 { $examples
222     { $example "USING: math.bitwise kernel prettyprint ;"
223         "6 on-bits .h"
224         "3f"
225     }
226     { $example "USING: math.bitwise kernel prettyprint ;"
227         "64 on-bits .h"
228         "ffffffffffffffff"
229     }
230 } ;
231
232 HELP: toggle-bit
233 { $values
234      { "m" integer }
235      { "n" integer }
236      { "m'" integer }
237 }
238 { $description "Toggles the nth bit of an integer." }
239 { $examples
240     { $example "USING: math.bitwise kernel prettyprint ;"
241         "0 3 toggle-bit .b"
242         "1000"
243     }
244     { $example "USING: math.bitwise kernel prettyprint ;"
245         "BIN: 1000 3 toggle-bit .b"
246         "0"
247     }
248 } ;
249
250 HELP: set-bit
251 { $values
252      { "x" integer } { "n" integer }
253      { "y" integer }
254 }
255 { $description "Sets the nth bit of " { $snippet "x" } "." }
256 { $examples
257     { $example "USING: math.bitwise kernel prettyprint ;"
258         "0 5 set-bit .h"
259         "20"
260     }
261 } ;
262
263 HELP: shift-mod
264 { $values
265      { "m" integer } { "s" integer } { "w" integer }
266      { "n" integer }
267 }
268 { $description "Calls " { $link shift } " on " { $snippet "n" } " and " { $snippet "s" } ", wrapping the result to " { $snippet "w" } " bits." } ;
269
270 HELP: unmask
271 { $values
272      { "x" integer } { "n" integer }
273      { "?" "a boolean" }
274 }
275 { $description "Clears the bits in " { $snippet "x" } " if they are set in the mask " { $snippet "n" } "." }
276 { $examples
277     { $example "USING: math.bitwise kernel prettyprint ;"
278         "HEX: ff  HEX: 0f unmask .h"
279         "f0"
280     }
281 } ;
282
283 HELP: unmask?
284 { $values
285      { "x" integer } { "n" integer }
286      { "?" "a boolean" }
287 }
288 { $description "Tests whether unmasking the bits in " { $snippet "x" } " would return an integer greater than zero." }
289 { $examples
290     { $example "USING: math.bitwise kernel prettyprint ;"
291         "HEX: ff  HEX: 0f unmask? ."
292         "t"
293     }
294 } ;
295
296 HELP: w*
297 { $values
298      { "x" integer } { "y" integer }
299      { "z" integer }
300 }
301 { $description "Multiplies two integers and wraps the result to 32 bits." }
302 { $examples
303     { $example "USING: math.bitwise kernel prettyprint ;"
304         "HEX: ffffffff HEX: 2 w* ."
305         "4294967294"
306     }
307 } ;
308
309 HELP: w+
310 { $values
311      { "x" integer } { "y" integer }
312      { "z" integer }
313 }
314 { $description "Adds two integers and wraps the result to 32 bits." }
315 { $examples
316     { $example "USING: math.bitwise kernel prettyprint ;"
317         "HEX: ffffffff HEX: 2 w+ ."
318         "1"
319     }
320 } ;
321
322 HELP: w-
323 { $values
324      { "x" integer } { "y" integer }
325      { "z" integer }
326 }
327 { $description "Subtracts two integers and wraps the result to 32 bits." }
328 { $examples
329     { $example "USING: math.bitwise kernel prettyprint ;"
330         "HEX: 0 HEX: ff w- ."
331         "4294967041"
332     }
333 } ;
334
335 HELP: wrap
336 { $values
337      { "m" integer } { "n" integer }
338      { "m'" integer }
339 }
340 { $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." }
341 { $examples "Equivalent to modding by 8:"
342     { $example 
343         "USING: math.bitwise prettyprint ;"
344         "HEX: ffff 8 wrap .h"
345         "7"
346     }
347 } ;
348
349 ARTICLE: "math-bitfields" "Constructing bit fields"
350 "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:"
351 { $subsections bitfield } ;
352
353 ARTICLE: "math.bitwise" "Additional bitwise arithmetic"
354 "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."
355 $nl
356 "Setting and clearing bits:"
357 { $subsections
358     set-bit
359     clear-bit
360 }
361 "Testing if bits are set or clear:"
362 { $subsections
363     bit?
364     bit-clear?
365 }
366 "Toggling a bit:"
367 { $subsections
368     toggle-bit
369 }
370 "Operations with bitmasks:"
371 { $subsections
372     mask
373     unmask
374     mask?
375     unmask?
376 }
377 "Generating an integer with n set bits:"
378 { $subsections on-bits }
379 "Counting the number of set bits:"
380 { $subsections bit-count }
381 "Testing the parity of an object:"
382 { $subsections even-parity? odd-parity? }
383 "More efficient modding by powers of two:"
384 { $subsections wrap }
385 "Bit-rolling:"
386 { $subsections
387     bitroll
388     bitroll-32
389     bitroll-64
390 }
391 "32-bit arithmetic:"
392 { $subsections
393     w+
394     w-
395     w*
396 }
397 "Bitfields:"
398 { $subsections
399     "math-bitfields"
400 } ;
401
402 ABOUT: "math.bitwise"