Bit and word calculator

Use band, bor, bxor, bnot, shl, lshr, ashr, wadd, wsub or wmul. Ordinary 127+1 is 128; word arithmetic uses the selected width. Exact integer operands must fit, or use word(value) for an explicit wrap. Shift counts are 0 to word size − 1.

Keep word width and signedness explicit

Choose 8, 16, 32 or 64 bits and signed or unsigned interpretation. Operands must be exact integers within that word range. Use word(value) for an explicit wrap; a word from a different width or signedness must be recast explicitly.

A word keeps the lowest selected number of bits. Unsigned interpretation reads those bits as a nonnegative integer. Signed two’s complement subtracts 2^width when the highest bit is 1. Word addition, subtraction and multiplication show both the mathematical intermediate and the wrapped result, including overflow.

A left shift fills low bits with zero and discards high bits outside the width. Logical right shift fills high bits with zero. For signed words, arithmetic right shift copies the sign bit; for unsigned words it fills with zero. The shift count must be an integer from zero to width−1.

Widening a signed negative word preserves its value by filling added high bits with 1; this is sign extension. Details retain the actual masks, bit patterns and discarded bits. Copy value includes the word’s width and signedness, so later reuse keeps its interpretation.

Worked example: 127 + 1 in a signed 8-bit word

Ordinary 127+1 gives 128. With Word size 8 and Signed selected, wadd(127,1) first produces the same mathematical value 128. Its retained pattern is 10000000, or 0x80. Signed interpretation gives 128−256=−128, so the calculation reports arithmetic overflow.

In the same context, −2 has pattern 11111110. lshr(−2,1) gives 01111111, or 127. ashr(−2,1) gives 11111111, or −1, because the signed right shift copies the leading 1. Extending −2 to 16 signed bits gives 1111111111111110 and keeps the value −2.

For band(12,10), combine 00001100 and 00001010: their shared 1 bit gives 00001000, or 8. XOR keeps the differing bits instead, giving 00000110, or 6. A 64-bit unsigned word can hold 18446744073709551615 exactly.

Load example fills the form without calculating. Choose its primary action to work it out.

Sources and related calculators

The method and worked example here are original. Sources describe the underlying mathematics or notation; calculation choices and limits are in the precision and syntax guide.