Skip to content

Bit shifting

Packages: ARITHMETIC_PACKAGE, FUNCTION_PACKAGE, CONVERTERS_PACKAGE, UOM_PACKAGE, BIGINT_PACKAGE. Registered by createEngine(); 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.

<< and >> shift left and right.

1 << 8 // 256
1 << 10 // 1,024
256 >> 4 // 16

Shifts 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,648
1 << 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,644

The two agree on anything non-negative, so the difference only shows up on the sign bit.

8 >> 1 // 4
8 >>> 1 // 4