OtherCalculators

Bitwise Calculator

Choose AND, OR, XOR, NOT or a shift, enter the operands in binary, hex or decimal, and see the result at 8, 16, 32 or 64 bits.

The operands and the result are stacked bit over bit so every column lines up, and the result is read as hex, an unsigned number and a two's complement signed number.

Last updated: ยท Reviewed by Shakeel Alvi, OtherCalculators

Calculator mode

Operands

Patterns shorter than the width are padded on the left with zeros. A negative decimal is stored as two's complement.

Operation
Width
Enter numbers as

A AND B

1000 1000

Hex
0x88
Unsigned
136
Signed (two's complement)
-120

Bit by bit

A

B

A AND B

AND gives 1 only where both bits are 1, so it keeps the bits of A that B selects. That is how a mask clears bits.

Exact at any size

Whole numbers are worked digit by digit, so 64-bit values and beyond never round off after 15 or 16 digits. Floating-point mode prints the exact value the bits store.

Runs in your browser

What you enter is worked out on this page and is not sent anywhere. Copy the address to share a conversion.

AND / OR / XORNOTShift Left / RightArithmetic Shift8 to 64-bit

Quick answer

Bitwise operators work on each bit position separately: AND gives 1 only where both bits are 1, OR where either is, and XOR where they differ; NOT flips every bit. So 1100 AND 1010 = 1000, OR = 1110 and XOR = 0110. Shifting left by n multiplies by 2โฟ until bits fall off the top of the width.
Guide

How the bitwise calculator works

Pick an operation, a width and how you want to type numbers. Operands shorter than the width are padded with zeros, and a negative decimal is stored as its two's complement pattern, the way a signed integer variable holds it. The result card shows the result bits with their hex, unsigned and signed values, and the bit-by-bit panel stacks A, B and the result so you can check each column.

For shifts, the vacated positions are shaded and the bits pushed off the end are listed. Switching the input between binary, hex and decimal rewrites the operands without changing their bits. For ordinary addition and multiplication, use the binary calculator.

How the maths works

The bitwise operators

AND: 1 only if both bits are 1
OR: 1 if either bit is 1; XOR: 1 if the bits differ
NOT x = (2โฟ โˆ’ 1) โˆ’ x; x << k = x ร— 2แต mod 2โฟ

Example: NOT 00000101 at 8 bits is 255 โˆ’ 5 = 250 = 11111010, which reads as โˆ’6 in two's complement.

Worked example

Worked examples

Example 1 โ€” masking the low nibble

A:
1011 0110 (0xB6)
Mask:
0000 1111 (0x0F)
  1. High four bits AND 0: 0000
  2. Low four bits AND 1: 0110

A AND 0x0F: 0000 0110 (6)

AND with 0x0F keeps the low four bits โ€” the second hex digit โ€” and clears the rest.

Example 2 โ€” arithmetic versus logical shift

A (8-bit):
1111 0000 (โˆ’16)
Shift:
right by 2
  1. Logical >>>: fill with 0: 0011 1100 (60)
  2. Arithmetic >>: copy the sign bit: 1111 1100 (โˆ’4)

โˆ’16 >> 2: โˆ’4

The arithmetic shift divides a signed value by 4; the logical shift treats it as unsigned 240 and gives 60.

Reference data

Truth table for AND, OR, XOR and NOT

Every combination of two input bits with the AND, OR and XOR results, and NOT of the first bit
ABA AND BA OR BA XOR BNOT A
000001
010111
100110
111100
Source: OtherCalculators โ€” the Boolean operator definitions, computed โ€” each operator is applied to every bit position independently

Key facts

  • A bitwise AND gives 1 in a position only when both input bits are 1, so ANDing with a mask keeps the masked bits and clears the rest.
  • In two's complement, NOT x equals โˆ’x โˆ’ 1, so NOT 5 is โˆ’6.
  • An arithmetic right shift copies the sign bit into the vacated positions, while a logical right shift fills them with zeros.
  • In JavaScript, a shift count on ordinary numbers is taken modulo 32, so 1 << 32 evaluates to 1.
When you need this

Where bitwise operations are used

Bitwise operators pack and unpack data: flags stored as single bits in a permissions value, colour channels in a 32-bit pixel, fields in a network header or a hardware register. Programmers use this calculator to check a mask before putting it in code, to see why a shift gave a surprising answer, or to decode a value read from a device. The language rules matter: JavaScript converts numbers to 32-bit integers and takes the shift count mod 32 (source: MDN), and its >> copies the sign bit (source: MDN). Hex input pairs well with the binary to hex converter.

Tips

Common bitwise mistakes

  • Confusing & with &&. Bitwise AND works bit by bit (6 & 3 = 2); logical AND only asks whether both sides are non-zero.
  • Ignoring the width. NOT 5 is 250 in 8 bits but 65,530 in 16 bits; the width decides how many bits flip.
  • Using >> on unsigned data. A pattern with its top bit set gets ones shifted in; use the logical shift for unsigned values.
  • Counting bits from the left. Bit 0 is the rightmost, least significant bit, so the mask for bit n is 1 << n.
FAQ

Frequently Asked Questions

What is 5 AND 3?

5 AND 3 is 1. In binary 5 is 101 and 3 is 011; only the last bit is 1 in both, so the result is 001. 5 OR 3 is 111 (7), and 5 XOR 3 is 110 (6).

What is the difference between >> and >>>?

>> is the arithmetic (sign-propagating) right shift: it fills the vacated top bits with copies of the sign bit, so a negative number stays negative, and โˆ’9 >> 2 is โˆ’3. >>> is the logical right shift: it always fills with zeros, so it treats the pattern as unsigned.

Why is NOT 5 equal to โˆ’6?

NOT flips every bit. 5 in 8 bits is 00000101, and flipped it is 11111010. Read as unsigned that is 250; read as two's complement it is โˆ’6. In two's complement, NOT x is always โˆ’x โˆ’ 1, which is why ~5 is โˆ’6 in most programming languages.

What happens when you shift by the width or more?

Mathematically every bit is pushed out, so a left or logical right shift gives 0 and an arithmetic right shift of a negative number gives all ones; that is what this calculator shows. Languages differ: JavaScript takes the shift count mod 32, so 1 << 32 is 1, and in C shifting by the width or more is undefined behaviour.

How do you use AND to check whether a bit is set?

AND the value with a mask that has a 1 only in that bit's position. If the result is non-zero the bit is set. To test bit 3 of 10110110, AND it with 00001000: the result is 00000000, so bit 3 is clear. Bits are counted from 0 on the right.

Why does XOR swap and toggle bits?

XOR with 1 flips a bit and XOR with 0 leaves it alone, so XOR with a mask toggles exactly the masked bits. Doing it twice restores the original, because x XOR m XOR m = x. That property is used for toggling flags, simple checksums and parity.

Keep going

Notes & Assumptions

  • Operands are fixed-width patterns of 8, 16, 32 or 64 bits; a value that needs more bits than the width is reported rather than cut down.
  • Shifts are computed mathematically: shifting by the width or more empties the register, which differs from JavaScript's mod-32 rule and from C, where it is undefined.
  • Negative decimal input is stored as two's complement at the chosen width.

This tool is for education and debugging. Check how a specific language or processor treats shift counts and signed values against its documentation.