Bit shifting
Packages:
ARITHMETIC_PACKAGE,FUNCTION_PACKAGE,CONVERTERS_PACKAGE,UOM_PACKAGE,BIGINT_PACKAGE. Registered bycreateEngine(); for a slimmer engine, register them explicitly (see choosing packages).
A shift slides the binary digits of a whole number sideways: left to make it bigger, right to make it smaller, each place a doubling or a halving. It is a low-level operation programmers reach for, and Solve writes it the way C and its relatives do.
Shifting
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 // 4