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 on the stack" } { "bitspec" "an array" } { "n" integer } }
8 { $description "Constructs an integer (bit field) 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 "{ word shift }" } " - " { $snippet "word" } " is applied to the top of the stack and the result is shifted to the left by " { $snippet "shift" } " bits and bitor'd with the bit field" }
11 { { $snippet "shift" } " - the top of the stack is shifted to the left by " { $snippet "shift" } " bits and bitor'd with the bit field" }
12 { { $snippet "{ constant shift }" } " - " { $snippet "constant" } " is shifted to the left by " { $snippet "shift" } " bits and bitor'd with the bit field" }
14 "The last entry in the bit field specifier is processed in reverse, so stack values are supplied in reverse order, e.g. the leftmost stack value is the last bit field specifier."
17 "Consider the following specification:"
19 { "bits 0-10 are set to the value of " { $snippet "x" } }
20 { "bits 11-14 are set to the value of " { $snippet "y" } }
21 { "bit 15 is always on" }
22 { "bits 16-20 are set to the value of " { $snippet "fooify" } " applied to " { $snippet "z" } }
24 "Such a bit field construction can be specified with a word like the following:"
26 "USING: math math.bitwise prettyprint ;"
27 "IN: math.bitwise.examples"
28 ": fooify ( x -- y ) 0b1111 bitand ;"
29 ": baz-bitfield ( x y z -- n )"
36 "3 2 1 baz-bitfield ."
39 "Square the 3 from the stack and shift 8, place the 1 from the stack at bit 5, and shift a constant 1 to bit 2:"
41 "USING: math math.bitwise prettyprint ;"
53 { $values { "values..." "a series of objects on the stack" } { "bitspec" "an array" } { "n" integer } }
54 { $description "Constructs an integer (bit field) 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:"
56 { { $snippet "{ word shift }" } " - " { $snippet "word" } " is applied to the top of the stack and the result is shifted to the left by " { $snippet "shift" } " bits and bitor'd with the bit field" }
57 { { $snippet "shift" } " - the top of the stack is shifted to the left by " { $snippet "shift" } " bits and bitor'd with the bit field" }
58 { { $snippet "{ constant shift }" } " - " { $snippet "constant" } " is shifted to the left by " { $snippet "shift" } " bits and bitor'd with the bit field" }
60 "The bit field specifier is processed in order, so stack values are taken from left to right."
63 "Consider the following specification:"
65 { "bits 0-10 are set to the value of " { $snippet "x" } }
66 { "bits 11-14 are set to the value of " { $snippet "y" } }
67 { "bit 15 is always on" }
68 { "bits 16-20 are set to the value of " { $snippet "fooify" } " applied to " { $snippet "z" } }
70 "Such a bit field construction can be specified with a word like the following:"
72 "USING: math math.bitwise prettyprint ;"
73 "IN: math.bitwise.examples"
74 ": fooify ( x -- y ) 0b1111 bitand ;"
75 ": baz-bitfield* ( x y z -- n )"
82 "1 2 3 baz-bitfield* ."
85 "Put a 1 at bit 1, put the 1 from the stack at bit 5, square the 3 and put it at bit 8:"
87 "USING: math math.bitwise prettyprint ;"
98 { bitfield bitfield* } related-words
101 { $values { "m" integer } { "n" integer } { "m'" integer } }
102 { $description "Keep only " { $snippet "n" } " bits from the integer " { $snippet "m" } ". For negative numbers, represent the number as two's complement (a positive integer representing a negative integer)." }
105 "USING: math.bitwise prettyprint ;"
106 "0x123abcdef 16 bits .h"
110 "USING: math.bitwise prettyprint ;"
117 { $values { "x" integer } { "high" integer } { "low" integer } { "y" integer } }
118 { $description "Extract a range of bits from an integer, inclusive of each boundary." }
119 { $example "USING: math.bitwise prettyprint ;" "0b1100 3 2 bit-range .b" "0b11" } ;
122 { $values { "x" integer } { "s" "a shift integer" } { "w" "a wrap integer" } { "y" integer }
124 { $description "Roll " { $snippet "n" } " by " { $snippet "s" } " bits to the left, wrapping around after " { $snippet "w" } " bits." }
126 { $example "USING: math.bitwise prettyprint ;" "1 -1 32 bitroll .b" "0b10000000000000000000000000000000" }
127 { $example "USING: math.bitwise prettyprint ;" "0xffff0000 8 32 bitroll .h" "0xff0000ff" }
130 { bit? set-bit clear-bit } related-words
137 { $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." }
139 { $example "USING: math.bitwise prettyprint ;"
143 { $example "USING: math.bitwise prettyprint ;"
144 "-1 32 bits bit-count ."
147 { $example "USING: math.bitwise prettyprint ;"
148 "B{ 1 0 1 } bit-count ."
155 { "m" integer } { "s" integer }
158 { $description "Rolls the number " { $snippet "m" } " by " { $snippet "s" } " bits to the left, wrapping around after 32 bits." }
160 { $example "USING: math.bitwise prettyprint ;"
161 "0x1 10 bitroll-32 .h"
164 { $example "USING: math.bitwise prettyprint ;"
165 "0x1 -10 bitroll-32 .h"
172 { "m" integer } { "s" "a shift integer" }
175 { $description "Rolls the number " { $snippet "m" } " by " { $snippet "s" } " bits to the left, wrapping around after 64 bits." }
177 { $example "USING: math.bitwise prettyprint ;"
178 "0x1 10 bitroll-64 .h"
181 { $example "USING: math.bitwise prettyprint ;"
182 "0x1 -10 bitroll-64 .h"
187 { bitroll bitroll-32 bitroll-64 } related-words
191 { "x" integer } { "n" integer }
194 { $description "Sets the " { $snippet "n" } "th bit of " { $snippet "x" } " to zero." }
196 { $example "USING: math.bitwise kernel prettyprint ;"
197 "0xff 7 clear-bit .h"
203 { $values { "symbols" sequence } { "assoc" assoc } { "flag-bits" integer } }
204 { $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." }
206 { $example "USING: math.bitwise prettyprint ui.gadgets.worlds ;"
208 "CONSTANT: window-controls>flags H{"
209 " { close-button 1 }"
210 " { minimize-button 2 }"
211 " { maximize-button 4 }"
212 " { resize-handles 8 }"
213 " { small-title-bar 16 }"
214 " { normal-title-bar 32 }"
216 "{ resize-handles close-button small-title-bar } window-controls>flags symbols>flags ."
227 { $example "USING: math.bitwise prettyprint ;"
232 { $description "Sets the lowest bit in the integer to 0, which either does nothing or outputs 1 less than the input integer." } ;
240 { $example "USING: math.bitwise prettyprint ;"
245 { $description "Sets the lowest bit in the integer to 1, which either does nothing or outputs 1 more than the input integer." } ;
249 { "x" integer } { "n" integer }
253 { $example "USING: math.bitwise prettyprint ;"
257 { $example "USING: math.bitwise prettyprint ;"
262 { $description "Interprets a number " { $snippet "x" } " as an " { $snippet "n" } "-bit number and converts it to a negative number if the topmost bit is set." } ;
264 { >signed bits } related-words
268 { "x" integer } { "n" integer }
271 { $description "After the operation, only the bits that were set in both the mask and the original number are set." }
273 { $example "USING: math.bitwise kernel prettyprint ;"
274 "0b11111111 0b101 mask .b"
281 { "m" integer } { "n" integer }
284 { $description "Turns off all bits besides the " { $snippet "n" } "th bit." }
286 { $example "USING: math.bitwise kernel prettyprint ;"
294 { "x" integer } { "n" integer }
297 { $description "Returns true if all of the bits in the mask " { $snippet "n" } " are set in the integer input " { $snippet "x" } "." }
299 { $example "USING: math.bitwise kernel prettyprint ;"
304 { $example "USING: math.bitwise kernel prettyprint ;"
315 { $description "Returns true if the number of set bits in an object is even." } ;
322 { $description "Returns true if the number of set bits in an object is odd." } ;
329 { $description "Returns an integer with " { $snippet "m" } " bits set." }
331 { $example "USING: math.bitwise kernel prettyprint ;"
335 { $example "USING: math.bitwise kernel prettyprint ;"
347 { $description "Toggles the " { $snippet "n" } "th bit of an integer." }
349 { $example "USING: math.bitwise kernel prettyprint ;"
353 { $example "USING: math.bitwise kernel prettyprint ;"
354 "0b1000 3 toggle-bit .b"
361 { "x" integer } { "n" integer }
364 { $description "Sets the " { $snippet "n" } "th bit of " { $snippet "x" } "." }
366 { $example "USING: math.bitwise kernel prettyprint ;"
374 { "m" integer } { "s" integer } { "w" integer }
377 { $description "Calls " { $link shift } " on " { $snippet "n" } " and " { $snippet "s" } ", wrapping the result to " { $snippet "w" } " bits." } ;
381 { "x" integer } { "n" integer }
384 { $description "Clears the bits in " { $snippet "x" } " if they are set in the mask " { $snippet "n" } "." }
386 { $example "USING: math.bitwise kernel prettyprint ;"
387 "0xff 0x0f unmask .h"
394 { "x" integer } { "n" integer }
397 { $description "Tests whether unmasking the bits in " { $snippet "x" } " would return an integer greater than zero." }
399 { $example "USING: math.bitwise kernel prettyprint ;"
400 "0xff 0x0f unmask? ."
407 { "x" integer } { "y" integer }
410 { $description "Multiplies two integers and wraps the result to a 32-bit unsigned integer." }
412 { $example "USING: math.bitwise kernel prettyprint ;"
413 "0xffffffff 0x2 w* ."
420 { "x" integer } { "y" integer }
423 { $description "Adds two integers and wraps the result to a 32-bit unsigned integer." }
425 { $example "USING: math.bitwise kernel prettyprint ;"
426 "0xffffffff 0x2 w+ ."
433 { "x" integer } { "y" integer }
436 { $description "Subtracts two integers and wraps the result to a 32-bit unsigned integer." }
438 { $example "USING: math.bitwise kernel prettyprint ;"
446 { "x" integer } { "y" integer }
449 { $description "Multiplies two integers and wraps the result to a 64-bit unsigned integer." }
451 { $example "USING: math.bitwise kernel prettyprint ;"
452 "0xffffffffffffffff 0x2 W* ."
453 "18446744073709551614"
459 { "x" integer } { "y" integer }
462 { $description "Adds two integers and wraps the result to 64-bit unsigned integer." }
464 { $example "USING: math.bitwise kernel prettyprint ;"
465 "0xffffffffffffffff 0x2 W+ ."
472 { "x" integer } { "y" integer }
475 { $description "Subtracts two integers and wraps the result to a 64-bit unsigned integer." }
477 { $example "USING: math.bitwise kernel prettyprint ;"
479 "18446744073709551361"
485 { "m" integer } { "n" integer }
488 { $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." }
489 { $examples "Equivalent to modding by 8:"
491 "USING: math.bitwise prettyprint ;"
498 { $values { "d" "a 64-bit integer" } { "w1" "a 32-bit integer" } { "w2" "a 32-bit integer" } }
499 { $description "Outputs two integers, the least followed by the most significant 32 bits of the input." } ;
502 { $values { "w" "a 32-bit integer" } { "h1" "a 16-bit integer" } { "h2" "a 16-bit integer" } }
503 { $description "Outputs two integers, the least followed by the most significant 16 bits of the input." } ;
506 { $values { "h" "a 16-bit integer" } { "b1" "an 8-bit integer" } { "b2" "an 8-bit integer" } }
507 { $description "Outputs two integers, the least followed by the most significant 8 bits of the input." } ;
509 ARTICLE: "math-bitfields" "Constructing bit fields"
510 "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:"
511 { $subsections bitfield } ;
513 ARTICLE: "math.bitwise" "Additional bitwise arithmetic"
514 "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."
516 "Setting and clearing bits:"
521 "Testing if bits are set:"
525 "Extracting bits from an integer:"
534 "Operations with bitmasks:"
541 "Generating an integer with n set bits:"
542 { $subsections on-bits }
543 "Counting the number of set bits:"
544 { $subsections bit-count }
545 "Testing the parity of an object:"
546 { $subsections even-parity? odd-parity? }
547 "More efficient modding by powers of two:"
548 { $subsections wrap }
567 "Words for taking larger integers apart into smaller integers:"
573 "Converting a number to the nearest even/odd/signed:"
584 ABOUT: "math.bitwise"