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