# 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.

- Calculator id: `bitwise-operations` · Category: Developer & IT (`developer`) · Tool name: `calculate_bitwise_operations`
- Canonical page: https://tttkmbb.com/developer/bitwise-operations · This document: https://tttkmbb.com/developer/bitwise-operations.md · JSON definition: https://tttkmbb.com/developer/bitwise-operations.json

## Purpose

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.

**Use when:** 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).

## Input

| Parameter | Type | Unit | Required | Description |
| --- | --- | --- | --- | --- |
| `a` | integer |  | required | Integer operand; negative values are interpreted in two's complement of the chosen width. (min -2147483648, max 4294967295) |
| `b` | integer |  | optional, default 0 | Second operand for and/or/xor; the number of bit positions for shifts; ignored for not. (min -2147483648, max 4294967295) |
| `operation` | enum: and \| or \| xor \| not \| shift_left \| shift_right_logical \| shift_right_arithmetic |  | required | Bitwise operation to apply. |
| `bit_width` | enum: 8 \| 16 \| 32 |  | optional, default "32" | Register width; results are truncated to this many bits and read as two's complement for the signed value. |

## Output

| Field | Type | Unit | Description |
| --- | --- | --- | --- |
| `result_signed` | integer |  | Result read as a two's-complement signed integer of the chosen width. |
| `result_unsigned` | integer |  | Result read as an unsigned integer of the chosen width. |
| `result_binary` | string |  | Result zero-padded to the bit width. |
| `result_hex` | string |  | Result in hexadecimal, zero-padded to width/4 digits. |
| `a_binary` | string |  | First operand in two's complement at the chosen width. |
| `b_binary` | string |  | Second operand (or shift count) in two's complement at the chosen width. |
| `expression` | string |  | The evaluated expression in C-style notation. |

## 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.

## Data Sources

- Bitwise operation (Wikipedia) — https://en.wikipedia.org/wiki/Bitwise_operation (reference, retrieved 2026-09-24)
- Two's complement (Wikipedia) — https://en.wikipedia.org/wiki/Two%27s_complement (reference, retrieved 2026-09-24)

Data freshness: `static`. Deterministic formula with fixed constants; results never go stale. Inputs supplied by the caller determine the output.

## API

- `GET https://tttkmbb.com/api/v1/calculate/bitwise-operations?a=…&operation=…`
- `POST https://tttkmbb.com/api/v1/calculate/bitwise-operations` with JSON body `{"inputs": {…}}`
- Response: unified envelope (`success`, `request`, `result.values`, `result.units`, `sources`, `freshness`, `timestamp`, `next_actions`, `links`); see https://tttkmbb.com/docs/response-format.md
- Schema: https://tttkmbb.com/api/v1/calculators/bitwise-operations · OpenAPI operationId `calculate_bitwise_operations` in https://tttkmbb.com/openapi.json
- Authentication: none. Rate limit: fair use, see https://tttkmbb.com/docs/rate-limits.md.

## MCP

- Server: `https://tttkmbb.com/mcp` (Streamable HTTP, JSON-RPC 2.0, no auth)
- Tool:  `run_calculator` with `{"calculator_id": "bitwise-operations", "inputs": {…}}`

## Example

- 12 AND 10 (32-bit): inputs `{"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): inputs `{"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
```

## Limitations

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). 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. All values are computed from the formula above; no measurement or live data is involved.

## 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

- [Number Base Converter](https://tttkmbb.com/math/base-converter.md) — Convert the operands or the result between binary, decimal and hexadecimal.
- [IP Subnet Calculator](https://tttkmbb.com/developer/subnet-calculator.md) — Subnet masking is a 32-bit AND/OR.
- [IEEE 754 Floating-Point Converter](https://tttkmbb.com/developer/ieee-754-float.md) — Bit layout of floating-point numbers.
