Programmer math
Numbers can be written in hexadecimal, binary or octal, 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) // 0xFFFbin(10) // 0b1010int goes the other way, though a literal is already a number so it is rarely
needed.
int(0xFF) // 255Shifting
Section titled “Shifting”<< and >> shift left and right.
1 << 8 // 2561 << 10 // 1,024256 >> 4 // 16Shifts work on 32-bit signed integers, which is worth knowing at the edges. The shift count is taken modulo 32, so shifting by 32 shifts by nothing at all, and bit 31 is the sign bit.
1 << 31 // -2,147,483,6481 << 32 // 1>> keeps the sign rather than filling with zeros, so a negative number stays
negative. >>> fills with zeros instead, which turns a negative into a large
positive one.
-16 >> 2 // -4-1 >> 1 // -1-8 >>> 1 // 2,147,483,644The two agree on anything non-negative, so the difference only shows up on the sign bit.
8 >> 1 // 48 >>> 1 // 4Bitwise operators
Section titled “Bitwise operators”& and | are and and or, ~ complements every bit, and exclusive or is the
word xor.
0xFF & 0x0F // 150xF0 | 0x0F // 2550xFF xor 0x0F // 2400b1010 & 0b0110 // 20b1010 | 0b0110 // 140b1010 xor 0b0110 // 12~5 // -6~0 // -1~ flips all 32 bits, which for a positive number means ~n is -(n+1).
Exclusive or is a word because ^ is already exponentiation, which is the far
more common thing to want on a page of sums. 2^10 is a thousand and change,
not three.
2^10 // 1,024Precedence
Section titled “Precedence”These operators follow the precedence order that C, JavaScript, Python and
their relatives share, so an expression that mixes them means what a programmer
reads it as. Loosest to tightest: |, then xor, then &, then the
comparisons, then the shifts, then + and -, then * and /.
1 | 2 << 3 // 171 + 2 << 3 // 244 & 3 + 1 // 44 | 6 & 3 // 6Read those as 1 | (2 << 3), (1 + 2) << 3, 4 & (3 + 1) and 4 | (6 & 3).
The arithmetic happens first, then the shift, then the bitwise operators, and
& wins against |.
Brackets still cost nothing, and on a line that mixes three or four of these they read better than a precedence table does.
(0xF0 | 0x0F) & 0xFF // 255(1 << 8) | 1 // 257Data sizes
Section titled “Data sizes”Byte and bit units are ordinary units, so they convert like any other
measurement. Decimal and binary prefixes are both there and are kept distinct:
kB is 1,000 bytes and KiB is 1,024.
1 kB in bytes // 1000.00 bytes1 KiB in bytes // 1024.00 bytes1 GB in MB // 1000.00 MB1 TiB in GiB // 1024.00 GiB1 byte in bits // 8.00 bits1.5 MB in KB // 1500.00 KBCase matters, and it matters more here than almost anywhere: MB is megabytes
and Mb is megabits, a factor of eight apart.
1 GB in bits // 8000000000.00 bits1 Gb in Mb // 1000.00 MbSee units and conversions for the full list.
Big integers
Section titled “Big integers”Bitwise work that overflows the 32-bit range needs ordinary integer arithmetic
instead. Suffix an integer with n to keep full precision.
123n * 2 // 246A 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 // 0xFFOne word to watch
Section titled “One word to watch”and is not a bitwise operator. It is the plain English word, and it adds.
5 and 3 // 8