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 ;
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:"
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" }
14 "The bit field specifier is processed left to right, so stack values should be supplied in reverse order." }
16 "Consider the following specification:"
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" } }
23 "Such a bit field construction can be specified with a word like the following:"
25 ": baz-bitfield ( x y z -- n )"
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" } ;
41 { $values { "x" integer } { "s" "a shift integer" } { "w" "a wrap integer" } { "y" integer }
43 { $description "Roll n by s bits to the left, wrapping around after w bits." }
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" }
51 { "x" integer } { "n" integer }
54 { $description "Returns " { $link t } " if the nth bit is set to zero." }
56 { $example "USING: math.bitwise prettyprint ;"
57 "HEX: ff 8 bit-clear? ."
60 { $example "USING: math.bitwise prettyprint ;"
61 "HEX: ff 7 bit-clear? ."
66 { bit? bit-clear? set-bit clear-bit } related-words
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." }
75 { $example "USING: math.bitwise prettyprint ;"
79 { $example "USING: math.bitwise prettyprint ;"
80 "-1 32 bits bit-count ."
83 { $example "USING: math.bitwise prettyprint ;"
84 "B{ 1 0 1 } bit-count ."
91 { "m" integer } { "s" integer }
94 { $description "Rolls the number " { $snippet "m" } " by " { $snippet "s" } " bits to the left, wrapping around after 32 bits." }
96 { $example "USING: math.bitwise prettyprint ;"
97 "HEX: 1 10 bitroll-32 .h"
100 { $example "USING: math.bitwise prettyprint ;"
101 "HEX: 1 -10 bitroll-32 .h"
108 { "m" integer } { "s" "a shift integer" }
111 { $description "Rolls the number " { $snippet "m" } " by " { $snippet "s" } " bits to the left, wrapping around after 64 bits." }
113 { $example "USING: math.bitwise prettyprint ;"
114 "HEX: 1 10 bitroll-64 .h"
117 { $example "USING: math.bitwise prettyprint ;"
118 "HEX: 1 -10 bitroll-64 .h"
123 { bitroll bitroll-32 bitroll-64 } related-words
127 { "x" integer } { "n" integer }
130 { $description "Sets the nth bit of " { $snippet "x" } " to zero." }
132 { $example "USING: math.bitwise kernel prettyprint ;"
133 "HEX: ff 7 clear-bit .h"
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." }
142 { $example "USING: math.bitwise prettyprint ui.gadgets.worlds ;"
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 }"
152 "{ resize-handles close-button small-title-bar } window-controls>flags symbols>flags ."
163 { $example "USING: math.bitwise prettyprint ;"
168 { $description "Sets the lowest bit in the integer to 0, which either does nothing or outputs 1 less than the input integer." } ;
176 { $example "USING: math.bitwise prettyprint ;"
181 { $description "Sets the lowest bit in the integer to 1, which either does nothing or outputs 1 more than the input integer." } ;
185 { "x" integer } { "n" integer }
189 { $example "USING: math.bitwise prettyprint ;"
190 "HEX: ff 8 >signed ."
194 { $description "Interprets a number " { $snippet "x" } " as an " { $snippet "n" } "-bit number and converts it to a negative number " { $snippet "n" } "-bit number if the topmost bit is set." } ;
198 { "x" integer } { "n" integer }
201 { $description "After the operation, only the bits that were set in both the mask and the original number are set." }
203 { $example "USING: math.bitwise kernel prettyprint ;"
204 "BIN: 11111111 BIN: 101 mask .b"
211 { "m" integer } { "n" integer }
214 { $description "Turns off all bits besides the nth bit." }
216 { $example "USING: math.bitwise kernel prettyprint ;"
217 "HEX: ff 2 mask-bit .b"
224 { "x" integer } { "n" integer }
227 { $description "Returns true if all of the bits in the mask " { $snippet "n" } " are set in the integer input " { $snippet "x" } "." }
229 { $example "USING: math.bitwise kernel prettyprint ;"
230 "HEX: ff HEX: f mask? ."
234 { $example "USING: math.bitwise kernel prettyprint ;"
235 "HEX: f0 HEX: 1 mask? ."
245 { $description "Returns true if the number of set bits in an object is even." } ;
252 { $description "Returns true if the number of set bits in an object is odd." } ;
259 { $description "Returns an integer with " { $snippet "m" } " bits set." }
261 { $example "USING: math.bitwise kernel prettyprint ;"
265 { $example "USING: math.bitwise kernel prettyprint ;"
277 { $description "Toggles the nth bit of an integer." }
279 { $example "USING: math.bitwise kernel prettyprint ;"
283 { $example "USING: math.bitwise kernel prettyprint ;"
284 "BIN: 1000 3 toggle-bit .b"
291 { "x" integer } { "n" integer }
294 { $description "Sets the nth bit of " { $snippet "x" } "." }
296 { $example "USING: math.bitwise kernel prettyprint ;"
304 { "m" integer } { "s" integer } { "w" integer }
307 { $description "Calls " { $link shift } " on " { $snippet "n" } " and " { $snippet "s" } ", wrapping the result to " { $snippet "w" } " bits." } ;
311 { "x" integer } { "n" integer }
314 { $description "Clears the bits in " { $snippet "x" } " if they are set in the mask " { $snippet "n" } "." }
316 { $example "USING: math.bitwise kernel prettyprint ;"
317 "HEX: ff HEX: 0f unmask .h"
324 { "x" integer } { "n" integer }
327 { $description "Tests whether unmasking the bits in " { $snippet "x" } " would return an integer greater than zero." }
329 { $example "USING: math.bitwise kernel prettyprint ;"
330 "HEX: ff HEX: 0f unmask? ."
337 { "x" integer } { "y" integer }
340 { $description "Multiplies two integers and wraps the result to a 32-bit unsigned integer." }
342 { $example "USING: math.bitwise kernel prettyprint ;"
343 "HEX: ffffffff HEX: 2 w* ."
350 { "x" integer } { "y" integer }
353 { $description "Adds two integers and wraps the result to a 32-bit unsigned integer." }
355 { $example "USING: math.bitwise kernel prettyprint ;"
356 "HEX: ffffffff HEX: 2 w+ ."
363 { "x" integer } { "y" integer }
366 { $description "Subtracts two integers and wraps the result to a 32-bit unsigned integer." }
368 { $example "USING: math.bitwise kernel prettyprint ;"
369 "HEX: 0 HEX: ff w- ."
376 { "x" integer } { "y" integer }
379 { $description "Multiplies two integers and wraps the result to a 64-bit unsigned integer." }
381 { $example "USING: math.bitwise kernel prettyprint ;"
382 "HEX: ffffffffffffffff HEX: 2 W* ."
383 "18446744073709551614"
389 { "x" integer } { "y" integer }
392 { $description "Adds two integers and wraps the result to 64-bit unsigned integer." }
394 { $example "USING: math.bitwise kernel prettyprint ;"
395 "HEX: ffffffffffffffff HEX: 2 W+ ."
402 { "x" integer } { "y" integer }
405 { $description "Subtracts two integers and wraps the result to a 64-bit unsigned integer." }
407 { $example "USING: math.bitwise kernel prettyprint ;"
408 "HEX: 0 HEX: ff W- ."
409 "18446744073709551361"
415 { "m" integer } { "n" integer }
418 { $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." }
419 { $examples "Equivalent to modding by 8:"
421 "USING: math.bitwise prettyprint ;"
422 "HEX: ffff 8 wrap .h"
427 ARTICLE: "math-bitfields" "Constructing bit fields"
428 "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:"
429 { $subsections bitfield } ;
431 ARTICLE: "math.bitwise" "Additional bitwise arithmetic"
432 "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."
434 "Setting and clearing bits:"
439 "Testing if bits are set or clear:"
448 "Operations with bitmasks:"
455 "Generating an integer with n set bits:"
456 { $subsections on-bits }
457 "Counting the number of set bits:"
458 { $subsections bit-count }
459 "Testing the parity of an object:"
460 { $subsections even-parity? odd-parity? }
461 "More efficient modding by powers of two:"
462 { $subsections wrap }
481 "Converting a number to the nearest even/odd:"
491 ABOUT: "math.bitwise"