Fixed-width integer workflow

Bitwise operations: explicit width, signed ranges and full-width shifts

The same bits can describe a positive unsigned integer or a negative signed integer. State width and signedness before interpreting a register, flag or protocol field.

Content updated: · Maintainer and corrections

Reject input overflow before applying the result mask

For width N, unsigned input must be 0 through 2^N−1. Signed input must be −2^(N−1) through 2^(N−1)−1. At eight bits, 255 is valid unsigned input and -1 is valid signed input; both can produce the bit pattern 11111111. Signed 255 and unsigned -1 are rejected instead of silently wrapping.

Complete decimal, 0x hexadecimal, 0b binary and 0o octal are accepted, with an optional sign. Prefixes do not bypass the signed range. Fractions, exponent notation, underscores and partial parses are rejected. Each operand is bounded to 140 characters. BigInt avoids the precision loss that JavaScript Number causes above its safe integer range.

Read logical and arithmetic shifts differently

Left shift fills low bits with zero and drops high bits outside the result mask. Logical right shift fills high bits with zero. Arithmetic right shift repeats the original top bit, treating the bit pattern as two’s complement even when the input mode is unsigned.

At eight bits, 128 logically shifted right once yields 64. The same 10000000 pattern arithmetically shifted right once yields 11000000, unsigned 192 and signed -64. The tool accepts counts from zero through the full width. A full-width logical or left shift yields zero; a full-width arithmetic shift yields zero or all-one sign fill. It does not use JavaScript Number’s modulo-32 shift count rules.

Rotate bits without inventing precision

Rotations move dropped bits to the opposite side and reduce the count modulo width. Eight-bit 10000001 rotated left once becomes 00000011; rotation by eight leaves the original pattern unchanged. Only counts 0…N are exposed in this tool, so the rule remains easy to inspect.

The output shows both signed and unsigned decimal, padded Hex, all bits and the explicit width mask. Aligned rows scroll locally for 64 or 128 bits, and complete JSON exports retain every bit. These fixed-width rules are the tool’s contract; compare them with the target language or CPU before copying into production code. They do not model C undefined behavior or every hardware instruction.

Data provenance

Sources