Number bases
Packages:
ARITHMETIC_PACKAGE,FUNCTION_PACKAGE,CONVERTERS_PACKAGE,UOM_PACKAGE,BIGINT_PACKAGE. Registered bycreateEngine(); for a slimmer engine, register them explicitly (see choosing packages).
A base is the number of distinct digits a number is written with: everyday decimal has ten, hexadecimal has sixteen, binary two, octal eight. The same value can be written in any of them, mixed freely with ordinary decimals, and shown back in whichever base you want.
Writing a number in another base
Section titled “Writing a number in another base”0xFF // 2550b1010 // 100o17 // 150xDEADBEEF // 3,735,928,559The prefix is case-insensitive and so are hex digits, so 0XFF, 0xff and
0xFF are the same number.
0xff // 2550XFF // 255A literal in any base is just a number, so bases mix in one expression and the result comes back in decimal.
0xFF + 0b1010 + 0o17 // 2800x1F + 1 // 32Showing a number in another base
Section titled “Showing a number in another base”as converts the display, and there is a function form for each base.
255 as hex // 0xFF255 as binary // 0b11111111255 as octal // 0o377hex(4095) // 0xFFFhex(255) // 0xFFbin(10) // 0b1010bin(5) // 0b101int goes the other way, though a literal is already a number so it is rarely
needed.
int(0xFF) // 255A base is still a number
Section titled “A base is still a number”Converting a number to another base changes how it is written, not what it is, so the result keeps doing arithmetic.
hex(255) + 1 // 256(255 as binary) + 1 // 256~hex(255) // -256A negative keeps its sign outside the literal, and a fraction is truncated, since there is no useful way to write a fractional hex digit.
hex(-255) // -0xFF255.7 as hex // 0xFF