FLXCode Bitwise Calculator

FLXCode apps

Appearance

Bitwise Calculator

Combine two numbers bit by bit. Type them in binary, hex or decimal, choose AND, OR, XOR, a shift or a rotate, and click the bits to see how the pattern changes.

Type with the keyboard or the keys below, or paste a number. Click a row to type in that base.

Bits (click one to flip it)

Try an example

History (this visit only)

    Nothing yet. Results appear here, newest first.

    Keyboard shortcuts
    0–9, A–Ftype digits (digits outside the input base are refused)
    + − * / %add, subtract, multiply, divide, modulo
    & | ^ ~AND, OR, XOR, NOT
    q wNAND, NOR
    < > sshift left, shift right (logical), shift right (arithmetic)
    r Shift+Rrotate left, rotate right
    nchange sign
    Enter or =equals (ENTER in RPN mode)
    Backspacedelete the last digit
    Escclear the number; press again to clear everything
    x k DeleteRPN: swap X and Y, roll the stack down, drop X

    No ads. No account. Nothing you type or record is uploaded. We count page events with a daily code. Check it.

    Problem or idea? Email us

    Quick start

    1. BIN is the input row. Type the first number, or click bits in the grid to build it.
    2. Press AND, OR, XOR, NAND or NOR, type the second number and press =. NOT acts on the number on screen at once.
    3. For a shift or a rotate, the second number is the count.
    4. Read the answer in the BIN row. The HEX row gives the same pattern in a quarter of the digits.

    Worked examples

    OperationWordResult in binaryHexDecimalWhat it does
    1100 AND 10108-bit unsigned0000 1000088A bit survives only where both numbers have one.
    1100 OR 10108-bit unsigned0000 11100E14A bit is set where either number has one.
    1100 XOR 10108-bit unsigned0000 0110066A bit is set where the numbers differ.
    NAND 1100, 10108-bit unsigned1111 0111F7247AND, then every bit flipped, up to the word width.
    NOT 0101 10108-bit unsigned1010 0101A5165At 8 bits this is the same as XOR with 1111 1111.
    1 SHL 78-bit unsigned1000 000080128Bit 0 moves to bit 7: 1 × 2⁷ = 128.
    1000 0001 ROL 18-bit unsigned0000 0011033The top bit comes round to the bottom.
    1000 0000 SAR 2 (signed)8-bit signed1110 0000E0-32Sign bits are copied in: -128 becomes -32.

    Truth tables

    Each gate looks at one bit from each number. The columns are the four input pairs.

    Gate0, 00, 11, 01, 1
    AND0001
    OR0111
    XOR0110
    NAND1110
    NOR1000
    InputNOT
    01
    10

    Mask recipes

    All five start from the value 0100 0001 (0x41, the letter A in ASCII).

    GoalOperationMaskResult
    Set bit 3OR000010000100 1001
    Clear bit 6AND101111110000 0001
    Flip bit 0XOR000000010100 0000
    Keep the low nibbleAND000011110000 0001
    Test bit 6 (non-zero if set)AND010000000100 0000

    Setting a bit that is already set, or clearing one that is already clear, changes nothing. XOR is the only one of the three that always changes the bit.

    Shifts and rotates

    • SHL n multiplies an unsigned number by 2 to the power of n, until bits fall off the top. The carry flag shows the last bit that fell off.
    • SHR n divides by 2 to the power of n and drops the remainder.
    • SAR n does the same for a signed number by copying the sign bit.
    • ROL and ROR turn the bits round the word, so no bit is lost.

    Signed and unsigned

    Bit operations do not care how you read the bits, but the decimal row does. 1111 1111 is 255 unsigned and -1 signed. The AND, OR and XOR results are the same bits either way. Shifts to the right differ: SHR is logical and SAR is arithmetic. Switch the reading with the Signed button.

    Numbers this uses

    • Word sizes 8, 16, 32 and 64 bits, exact integers (BigInt). This page starts at 8 bits.
    • A shift count of the word size or more shifts every bit out. A rotate count is taken modulo the word size.
    • NAND and NOR are AND and OR followed by NOT, cut to the word size.

    For ordinary arithmetic with carry and borrow see the binary calculator and the hex calculator. The programmer calculator adds RPN, a history and every word size.

    Nothing you type is uploaded. We count events without cookies (details).

    FAQ

    What is a bitwise operation?

    An operation that works on each bit position on its own. AND of two numbers looks at bit 0 of both, then bit 1 of both, and so on, and builds a new number from the answers. Nothing carries from one position to the next, which is what separates it from addition.

    What is the difference between AND and OR?

    AND gives 1 only where both inputs have a 1, so it is used to keep or test bits. OR gives 1 where either input has a 1, so it is used to turn bits on. The truth tables above show all four input pairs for each.

    How do I use a bit mask?

    To keep certain bits, AND with a mask that has ones in those positions. To set bits, OR with a mask. To flip them, XOR with a mask. To clear them, AND with the mask turned inside out, which is NOT of the bits you want to clear. The mask table above has one example of each.

    What does XOR do that OR does not?

    XOR is 1 only when the two bits differ. 1 XOR 1 is 0, where 1 OR 1 is 1. That makes XOR a toggle: XOR twice with the same mask gives the original value back. It also makes XOR a quick equality check, because x XOR x is 0.

    Why does NOT depend on the word size?

    NOT flips every bit in the word, and the word has a fixed number of them. NOT 0000 0101 is 1111 1010 at 8 bits, but 1111 1111 1111 1010 at 16 bits. The calculator masks the answer to the chosen size, so at 8 bits NOT x is 0xFF XOR x.

    Why does shifting left multiply by two?

    Each place in binary is worth twice the one on its right, so moving every bit one place left doubles the value. Shifting right by one divides by two and drops the remainder. SHL by n multiplies by 2 to the power of n, until bits fall off the top.

    When do I use SAR instead of SHR?

    Use SAR for signed numbers. It copies the sign bit into the empty space, so a negative number stays negative: -128 SAR 2 is -32. SHR puts zeros in, which turns a negative number into a large positive one.

    Is anything I type saved or sent?

    No. Everything runs in your browser. What you type is not put in the address bar, not stored and not counted. Only your last word size and base are remembered on this device.

    More from FLXCode

    Share Bitwise Calculator