"IN: scratchpad"
"CONSTANT: x 0x1"
"flags{ 0x20 x 0b100 } .h"
- "25"
+ "0x25"
}
} ;
HELP: bits
{ $values { "m" integer } { "n" integer } { "m'" integer } }
{ $description "Keep only n bits from the integer m." }
-{ $example "USING: math.bitwise prettyprint ;" "0x123abcdef 16 bits .h" "cdef" } ;
+{ $example "USING: math.bitwise prettyprint ;" "0x123abcdef 16 bits .h" "0xcdef" } ;
HELP: bit-range
{ $values { "x" integer } { "high" integer } { "low" integer } { "y" integer } }
{ $description "Extract a range of bits from an integer, inclusive of each boundary." }
-{ $example "USING: math.bitwise prettyprint ;" "0b1100 3 2 bit-range .b" "11" } ;
+{ $example "USING: math.bitwise prettyprint ;" "0b1100 3 2 bit-range .b" "0b11" } ;
HELP: bitroll
{ $values { "x" integer } { "s" "a shift integer" } { "w" "a wrap integer" } { "y" integer }
}
{ $description "Roll n by s bits to the left, wrapping around after w bits." }
{ $examples
- { $example "USING: math.bitwise prettyprint ;" "1 -1 32 bitroll .b" "10000000000000000000000000000000" }
- { $example "USING: math.bitwise prettyprint ;" "0xffff0000 8 32 bitroll .h" "ff0000ff" }
+ { $example "USING: math.bitwise prettyprint ;" "1 -1 32 bitroll .b" "0b10000000000000000000000000000000" }
+ { $example "USING: math.bitwise prettyprint ;" "0xffff0000 8 32 bitroll .h" "0xff0000ff" }
} ;
{ bit? set-bit clear-bit } related-words
{ $examples
{ $example "USING: math.bitwise prettyprint ;"
"0x1 10 bitroll-32 .h"
- "400"
+ "0x400"
}
{ $example "USING: math.bitwise prettyprint ;"
"0x1 -10 bitroll-32 .h"
- "400000"
+ "0x400000"
}
} ;
{ $examples
{ $example "USING: math.bitwise prettyprint ;"
"0x1 10 bitroll-64 .h"
- "400"
+ "0x400"
}
{ $example "USING: math.bitwise prettyprint ;"
"0x1 -10 bitroll-64 .h"
- "40000000000000"
+ "0x40000000000000"
}
} ;
{ $examples
{ $example "USING: math.bitwise kernel prettyprint ;"
"0xff 7 clear-bit .h"
- "7f"
+ "0x7f"
}
} ;
{ $examples
{ $example "USING: math.bitwise kernel prettyprint ;"
"0b11111111 0b101 mask .b"
- "101"
+ "0b101"
}
} ;
{ $examples
{ $example "USING: math.bitwise kernel prettyprint ;"
"0xff 2 mask-bit .b"
- "100"
+ "0b100"
}
} ;
{ $examples
{ $example "USING: math.bitwise kernel prettyprint ;"
"6 on-bits .h"
- "3f"
+ "0x3f"
}
{ $example "USING: math.bitwise kernel prettyprint ;"
"64 on-bits .h"
- "ffffffffffffffff"
+ "0xffffffffffffffff"
}
} ;
{ $examples
{ $example "USING: math.bitwise kernel prettyprint ;"
"0 3 toggle-bit .b"
- "1000"
+ "0b1000"
}
{ $example "USING: math.bitwise kernel prettyprint ;"
"0b1000 3 toggle-bit .b"
- "0"
+ "0b0"
}
} ;
{ $examples
{ $example "USING: math.bitwise kernel prettyprint ;"
"0 5 set-bit .h"
- "20"
+ "0x20"
}
} ;
{ $examples
{ $example "USING: math.bitwise kernel prettyprint ;"
"0xff 0x0f unmask .h"
- "f0"
+ "0xf0"
}
} ;
{ $example
"USING: math.bitwise prettyprint ;"
"0xffff 8 wrap .h"
- "7"
+ "0x7"
}
} ;
: error-in-pprint ( obj -- str )
class-of name>> "~pprint error: " "~" surround ;
-: .b ( n -- ) >bin print ;
-: .o ( n -- ) >oct print ;
-: .h ( n -- ) >hex print ;
+: .b ( n -- ) >bin "0b" prepend print ;
+: .o ( n -- ) >oct "0o" prepend print ;
+: .h ( n -- ) >hex "0x" prepend print ;
: stack. ( seq -- )
[
{ $values { "x" integer } { "y" integer } { "z" integer } }
{ $description "Outputs a new integer where each bit is set if and only if the corresponding bit is set in both inputs." }
{ $examples
- { $example "USING: math prettyprint ;" "0b101 0b10 bitand .b" "0" }
- { $example "USING: math prettyprint ;" "0b110 0b10 bitand .b" "10" }
+ { $example "USING: math prettyprint ;" "0b101 0b10 bitand .b" "0b0" }
+ { $example "USING: math prettyprint ;" "0b110 0b10 bitand .b" "0b10" }
}
{ $notes "This word implements bitwise and, so applying it to booleans will throw an error. Boolean and is the " { $link and } " word." } ;
{ $values { "x" integer } { "y" integer } { "z" integer } }
{ $description "Outputs a new integer where each bit is set if and only if the corresponding bit is set in at least one of the inputs." }
{ $examples
- { $example "USING: math prettyprint ;" "0b101 0b10 bitor .b" "111" }
- { $example "USING: math prettyprint ;" "0b110 0b10 bitor .b" "110" }
+ { $example "USING: math prettyprint ;" "0b101 0b10 bitor .b" "0b111" }
+ { $example "USING: math prettyprint ;" "0b110 0b10 bitor .b" "0b110" }
}
{ $notes "This word implements bitwise inclusive or, so applying it to booleans will throw an error. Boolean inclusive or is the " { $link and } " word." } ;
{ $values { "x" integer } { "y" integer } { "z" integer } }
{ $description "Outputs a new integer where each bit is set if and only if the corresponding bit is set in exactly one of the inputs." }
{ $examples
- { $example "USING: math prettyprint ;" "0b101 0b10 bitxor .b" "111" }
- { $example "USING: math prettyprint ;" "0b110 0b10 bitxor .b" "100" }
+ { $example "USING: math prettyprint ;" "0b101 0b10 bitxor .b" "0b111" }
+ { $example "USING: math prettyprint ;" "0b110 0b10 bitxor .b" "0b100" }
}
{ $notes "This word implements bitwise exclusive or, so applying it to booleans will throw an error. Boolean exclusive or is the " { $link xor } " word." } ;
HELP: shift
{ $values { "x" integer } { "n" integer } { "y" integer } }
{ $description "Shifts " { $snippet "x" } " to the left by " { $snippet "n" } " bits if " { $snippet "n" } " is positive, or " { $snippet "-n" } " bits to the right if " { $snippet "n" } " is negative. A left shift of a fixnum may overflow, yielding a bignum. A right shift may result in bits “falling off” the right hand side and being discarded." }
-{ $examples { $example "USING: math prettyprint ;" "0b101 5 shift .b" "10100000" } { $example "USING: math prettyprint ;" "0b11111 -2 shift .b" "111" } } ;
+{ $examples { $example "USING: math prettyprint ;" "0b101 5 shift .b" "0b10100000" } { $example "USING: math prettyprint ;" "0b11111 -2 shift .b" "111" } } ;
HELP: bitnot
{ $values { "x" integer } { "y" integer } }