HomeDeveloper & IT › Bitwise Operations Calculator

Bitwise Operations Calculator

Applies AND, OR, XOR, NOT, left shift, logical right shift or arithmetic right shift to integers of a chosen width (8, 16 or 32 bits) and reports the result as signed two's complement, unsigned, binary and hexadecimal.

When to use

You need the exact bit pattern of a masked, combined or shifted integer as a CPU register of a given width would hold it, including the signed and unsigned readings of the result.

Do not use when: Values exceed 32 bits (use arbitrary-precision arithmetic), you only need a base change (use base-converter), or you want floating-point bit layouts (use ieee-754-float).

Formula

ua = a mod 2^w; ub = b mod 2^w; and/or/xor/not applied bit by bit; shift_left = (ua·2^b) mod 2^w; shift_right_logical = floor(ua / 2^b); shift_right_arithmetic = floor(sa / 2^b) with sa the signed reading; result_signed = result_unsigned − 2^w when result_unsigned ≥ 2^(w−1)

Operands must fit the width either as signed (−2^(w−1) … 2^(w−1)−1) or unsigned (0 … 2^w−1) integers; shift counts must be 0 … w−1 as in C. All arithmetic is done with JavaScript 32-bit integer operators and masked to the width.

Inputs

ParameterTypeUnitRequiredDescription
aintegeryesInteger operand; negative values are interpreted in two's complement of the chosen width. Range: ≥ -2147483648, ≤ 4294967295
bintegerdefault 0Second operand for and/or/xor; the number of bit positions for shifts; ignored for not. Range: ≥ -2147483648, ≤ 4294967295
operationenum: and | or | xor | not | shift_left | shift_right_logical | shift_right_arithmeticyesBitwise operation to apply.
bit_widthenum: 8 | 16 | 32default 32Register width; results are truncated to this many bits and read as two's complement for the signed value.

Outputs

OutputTypeUnitDescription
result_signedintegerResult read as a two's-complement signed integer of the chosen width.
result_unsignedintegerResult read as an unsigned integer of the chosen width.
result_binarystringResult zero-padded to the bit width.
result_hexstringResult in hexadecimal, zero-padded to width/4 digits.
a_binarystringFirst operand in two's complement at the chosen width.
b_binarystringSecond operand (or shift count) in two's complement at the chosen width.
expressionstringThe evaluated expression in C-style notation.

Example

12 AND 10 (32-bit): {"a":12,"b":10,"operation":"and","bit_width":"32"}{"result_signed":8,"result_unsigned":8,"result_binary":"00000000000000000000000000001000","result_hex":"0x00000008","a_binary":"00000000000000000000000000001100"}

NOT 5 (8-bit): {"a":5,"operation":"not","bit_width":"8"}{"result_unsigned":250,"result_signed":-6,"result_binary":"11111010","result_hex":"0xFA"}

GET https://tttkmbb.com/api/v1/calculate/bitwise-operations?a=12&b=10&operation=and&bit_width=32

Machine access

Sources

FAQ

Why are there two right shifts?

A logical shift fills the vacated high bits with zeros (unsigned division by 2^b); an arithmetic shift copies the sign bit, so negative numbers stay negative (signed division rounding toward −∞).

How is a negative operand handled?

It is converted to its two's-complement pattern at the chosen width, e.g. −1 becomes 11111111 in 8 bits (unsigned 255), before the operation is applied.

What if the shift count is larger than the width?

The calculator rejects it with OUT_OF_RANGE; in C such shifts are undefined behaviour, and JavaScript masks the count to 5 bits.

Related calculators