Bitwise operations with explicit width
Bitwise operations work on corresponding bits rather than on decimal digits. AND keeps a bit only when both inputs contain 1, OR keeps it when either input contains 1, XOR keeps it when the inputs differ, and NOT flips every bit. Shifts move a pattern left or right. These operations are fundamental for masks, flags, permissions, color channels, protocol fields, low-level optimization, and systems programming.
Width cannot be an afterthought. NOT 00000000 is 11111111 in eight bits, but it is sixteen ones in sixteen bits. Left shifts can discard high bits, and signed right shift can replicate the sign bit. This calculator requires 8, 16, 32, or 64 bits, masks every result to that width, and reports both signed and unsigned decimal interpretations of the final pattern.
How to use the bitwise calculator
Set the width and semantics before interpreting the operands.
- Choose AND, OR, XOR, NOT, left shift, or right shift.
- Select decimal, binary, or hexadecimal input. Decimal semantics determine whether negative values are accepted and how right shift behaves.
- Choose unsigned or signed two’s-complement interpretation, then choose 8, 16, 32, or 64 bits.
- Enter value A. Enter value B for binary operations, or a non-negative decimal shift count for shifts. NOT uses only value A.
- Press Calculate and compare the padded bits, hex, unsigned decimal, and signed decimal results.
Bitwise examples
The following examples use eight bits to make masking and sign behavior visible.
| Operation | Result bits | Explanation |
|---|---|---|
| 10101010 AND 11001100 | 10001000 | Only positions that are 1 in both operands remain set. |
| 10101010 OR 11001100 | 11101110 | A position is set when either input contains a 1. |
| 10101010 XOR 11001100 | 01100110 | A position is set when the two input bits differ. |
| NOT 00001111 | 11110000 | Every bit is flipped within the selected eight-bit mask. |
| 10000001 << 1 | 00000010 | The high bit is discarded and a zero enters on the right. |
| 11111110 >> 1 | 11111111 signed / 01111111 unsigned | Arithmetic signed shift extends the sign bit; logical unsigned shift inserts zero. |
Input formats and semantics
Decimal mode validates against the selected interpretation. Unsigned eight-bit input ranges from 0 to 255. Signed eight-bit input ranges from -128 to 127 and is converted to its two’s-complement pattern before the operation. Binary and hex modes are treated as raw non-negative bit patterns and must fit within the chosen width; signedness affects result interpretation and right-shift behavior.
Shift counts are always non-negative decimal integers, regardless of the operand format. Left shift discards bits that move past the selected width and inserts zeros on the right. Unsigned right shift inserts zeros on the left. Signed right shift interprets A as a two’s-complement signed value and extends its sign.
- Binary input accepts 0b prefixes plus spaces or underscores.
- Hex input accepts 0x prefixes and is padded in the result to the selected width.
- AND, OR, and XOR operate on raw patterns; signedness changes only decimal interpretation.
- A shift count greater than or equal to the width produces zero for left/logical right shifts; arithmetic right shift of a negative value remains all ones.
How masking and shifts are implemented
Each operand is converted to an unsigned BigInt pattern between zero and 2ⁿ-1. AND, OR, and XOR combine those patterns directly. NOT first flips the unbounded BigInt bits and then applies an n-bit mask, ensuring that only the selected register width remains.
Left shift moves the pattern by the requested count and applies the mask, which models high-bit truncation in a fixed-width register. Logical right shift operates on the unsigned pattern. Arithmetic right shift first interprets the pattern as signed by subtracting 2ⁿ when the sign bit is set, then uses sign-propagating shift and masks the result back to n bits.
The final pattern is always displayed in four forms: exactly n binary digits, exactly n/4 hexadecimal digits, unsigned decimal, and signed two’s-complement decimal. This makes the operation and its interpretation independently visible.
Practical uses
Bitwise tools are most useful when individual flags or fields carry independent meaning.
Apply masks and flags
Check whether permissions or feature flags are set, combine options with OR, clear fields with AND masks, or toggle bits with XOR.
Decode packed protocol fields
Separate headers, status words, device registers, and binary file fields while preserving an exact documented width.
Work with colors and channels
Extract or combine packed RGB/ARGB channel values and verify shifts used to place bytes into a larger integer.
Test systems code
Create reference results for firmware, parsers, cryptographic preprocessing, hash code, and cross-language shift behavior.
Common mistakes and edge cases
Bitwise results often look wrong when the width or signedness differs from the system being modeled.
Forgetting the mask width
NOT and left shift are undefined without a boundary in ordinary mathematical integers. Match the width used by the register, protocol, or source language.
Mixing arithmetic and logical right shift
Negative signed values replicate a 1 during arithmetic shift. Unsigned values insert zero. Select semantics deliberately.
Entering signed binary text
Binary and hex inputs are raw patterns and do not accept a minus sign. Use signed decimal mode for a mathematical negative value.
Assuming shifts wrap around
These are shifts, not rotates. Bits shifted out are discarded and do not re-enter at the opposite side.
How this differs from related tools
Bitwise operations change patterns under an explicit width, unlike ordinary arithmetic or simple base conversion.
Binary calculator
Addition and multiplication propagate carries and change many bits according to arithmetic. AND, OR, and XOR treat each position independently.
Two’s complement
The converter explains one pattern’s signed meaning. This calculator uses that meaning during signed decimal parsing and arithmetic right shift.
Base converter
Changing base preserves the integer. A bitwise mask or shift can change the integer and can intentionally discard high bits.
IEEE-754 converter
Floating-point bits contain structured sign, exponent, and mantissa fields. Applying arbitrary integer bitwise operations to them changes the encoded float rather than performing floating-point math.