Works on this device
Bitwise & Binary Calculator
Work with whole numbers the way a computer stores them: in a fixed number of bits. Choose an operation, type the numbers in decimal, binary, hexadecimal or octal, and see every result in binary, hexadecimal, unsigned and signed form.
Enter your values
Check these values
Your values are worked out in this browser. They are not sent to a server.
Your result will show here
Fill in the values, then choose Calculate.
Your result
How it was worked out
How to use this calculator
- Choose what you want to do, for example Bitwise AND, OR, XOR, NAND, NOR or XNOR of two numbers.
- Choose how the numbers are typed (decimal, binary, hexadecimal or octal) and the width in bits, such as 8 or 32. You can also write
0b,0oor0xin front of a number. - Type the numbers. A minus sign is allowed: a negative number is stored in two's complement.
- Choose Calculate. The table shows the numbers and the result bit by bit, in groups of four bits.
What each operation does
- AND, OR, XOR: each bit of the answer comes from the two bits in the same place. AND gives 1 only for 1 and 1. OR gives 1 if at least one bit is 1. XOR gives 1 if the bits are different. NAND, NOR, XNOR are the same with every bit inverted.
- Ones' complement (NOT) inverts every bit. Two's complement is the ones' complement plus 1. It is the negative of the number in the same width.
- Signed reading: the first bit counts as minus 2 to the power of (width − 1). In 8 bits, 1111 1111 is 255 unsigned and −1 signed.
- Shift left by k places multiplies by 2^k. Logical shift right divides an unsigned number by 2^k and rounds down. Arithmetic shift right does the same for a signed number by copying the sign bit. Rotate moves the bits that fall out back in at the other end.
- Overflow: the exact answer does not fit in the width. Unsigned overflow means the exact unsigned answer is outside 0 to 2^w − 1 (a carry or a borrow). Signed overflow means the exact signed answer is outside −2^(w−1) to 2^(w−1) − 1.
- Mask: a pattern with 1 bits from a lowest to a highest bit. OR with a mask sets bits, AND with the inverted mask clears them, XOR flips them, and AND with the mask tests them.
Worked examples
- 12 AND 10 in 8 bits: 0000 1100 AND 0000 1010 = 0000 1000, which is 8. 12 OR 10 = 0000 1110 = 14, and 12 XOR 10 = 0000 0110 = 6.
- Two's complement of 45 in 8 bits: 45 = 0010 1101. Invert every bit: 1101 0010. Add 1: 1101 0011. As an unsigned number this is 211, and as a signed number it is −45.
- 100 + 50 in 8 bits gives 1001 0110. As unsigned numbers 100 + 50 = 150 fits, so there is no unsigned overflow. As signed numbers 150 is above 127, so there is a signed overflow and the register reads −106.
- The 32-bit number 0x12345678 with its bytes reversed (big-endian to little-endian) is 0x78563412.
Limits you should know
- Whole numbers only, in widths of 4, 8, 12, 16, 24, 32, 64 or 128 bits. A typed number must fit the width as a signed or an unsigned number.
- Division gives the whole-number quotient and the remainder. Signed division rounds towards zero, as most programming languages do.
- A shift by the width or more is done in full here (all bits leave). Some programming languages use the count modulo the width instead.
- For plain conversion between number bases, use the Numeral System Converter. For floating-point numbers (IEEE 754), use the Decimal Precision Calculator. For column addition with every carry, use the Binary Adder Calculator.