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