Bitwise Calculator
Free bitwise calculator: AND, OR, XOR, NAND, NOR, XNOR, NOT and shifts on decimal numbers, with a bit-by-bit breakdown, truth table, bit masks and flags, and results in binary, octal, decimal and hex across 4-32 bit registers.
https://hexacalculator.com/calculators/other/computers/bitwise-calculator
Other
Computers
Bitwise Calculator
Free bitwise calculator: AND, OR, XOR, NAND, NOR, XNOR, NOT and shifts on decimal numbers, with a bit-by-bit breakdown, truth table, bit masks and flags, and results in binary, octal, decimal and hex across 4-32 bit registers.
Bitwise Calculator
Bitwise operation
AND, OR, XOR, NAND, NOR and XNOR combine two numbers one bit at a time. NOT inverts a single number. The shifts slide the bits of the first number and use only a shift amount.
The number of bit positions in the register. NOT, NAND, NOR, XNOR and the shifts fill or clear bits up to this width, and it sets where the sign bit lives.
- First number in binary
- Second number in binary
- Result in octal
- Set bits (1s in the result)
Result and breakdown
Binary: 00001000
Octal: 10
Decimal: 8
Hexadecimal: 0x8
In hexadecimal, the way a programmer usually reads these: 0xC and 0xA give 0x8.
Bit (2n) | A | B | Result |
|---|---|---|---|
| 2^7 | 0 | 0 | 0 |
| 2^6 | 0 | 0 | 0 |
| 2^5 | 0 | 0 | 0 |
| 2^4 | 0 | 0 | 0 |
| 2^3 | 1 | 1 | 1 |
| 2^2 | 1 | 0 | 0 |
| 2^1 | 0 | 1 | 0 |
| 2^0 | 0 | 0 | 0 |
A | B | Result |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Bitwise operations directly affect the 1s and 0s of a number's value and work on them bit by bit. In low-level code this is used to mask, set, clear or invert individual bits, and it forms the basis for flags, permissions, graphics, networking and cryptography.
When you enter two decimal numbers and select an operation this tool will show the result in decimal, binary, octal and hexadecimal representation. Bitwise tables and truth tables allow you to check exactly how each bit is affected.
Seven types of logical operations:
AND returns 1 only if both bits are 1, which can be used to retain or mask certain bits. OR returns 1 if either bit is a 1, which can be used to set bits. XOR (exclusive OR) returns 1 only when the two bits differ, which can be used for inverting bits and making simple checksums.
NAND and NOR are the inverses of AND and OR respectively. With NAND alone all other logic gates can be realized. XNOR returns 1 if both bits are equal, which can be interpreted as a test for equality. NOT takes one number and inverts every bit in the register.
A | B | AND | OR | XOR | NAND | NOR | XNOR |
|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
0 | 1 | 0 | 1 | 1 | 1 | 0 | 0 |
1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
1 | 1 | 1 | 1 | 0 | 0 | 0 | 1 |
Here is an example of an 8-bit register: A = 12 (binary 1100), B = 10 (binary 1010).
Bit Set, Clear, Toggle and Check
Many actual bit operations are based on one of four operations with masks. A mask is a value that only sets the bits you want to set. To check if a particular bit is set, an AND operation is performed with the mask and the result is checked for non-zero. To set a particular bit, an OR operation is performed with the mask. To invert a particular bit, an XOR operation is performed with the mask.
To clear a particular bit, an AND operation is performed with the complement of the mask. This brings in NOT. As these operations are very commonly used, masks are usually represented in hexadecimal numbers. So the mask for lower nibble will be 0x0F, and for single bit it is shifted to appropriate position e.g. 1.
Goal | Operation | Example |
|---|---|---|
Check a bit | value AND mask | 6 AND 4 = 4 (bit is set) |
Set a bit | value OR mask | 1 OR 4 = 5 |
Toggle a bit | value XOR mask | 5 XOR 4 = 1 |
Clear a bit | value AND NOT mask | 5 AND NOT 4 = 1 |
Keep low nibble | value AND 0x0F | 171 AND 15 = 11 |
Flags and Permissions
Storing multiple yes/no settings in a single number is a typical use of bit logic. If each setting has its own bit, all the settings can be stored, combined and tested with a single operation. Unix file permissions work exactly this way: read is 4, write is 2, execute is 1.
A value for a file that has both read and write permissions is 6 which is binary 110. To check the read permission, an AND operation between 6 and 4 is performed resulting in 4. Since the result is not zero, reading is allowed. To check the execute permission, an AND operation between 6 and 1 is performed resulting in 0. Hence, execution is prohibited.
Register width and non-operation
A single binary digit is called a bit, and hardware groups bits together in fixed-size registers. A nibble consists of 4 bits, a byte has 8 bits, and processors often use registers with 16 or 32 bits. The width chosen determines the number of positions where an inversion or insertion can occur.
The width is especially important for non bitwise and other inversion operations. Inverting a byte with the bits 00000101 gives 11111010, or 250. However if the same value is inverted in a register with four bits there are only four positions so the result will be 1010, or 10. If you choose a width that fits your data this calculator will fill or clear the bits up to the limit.
Two's complement and signed results
Since fixed-width registers have no sign, negative numbers are stored as two's complement. This means that the absolute value of the number is represented in binary, then each bit is inverted and 1 is added. In an 8-bit register, -1 is equal to 11111111, using the same bit pattern as the unsigned value 255.
Since inversion operations often set the most significant bit, this calculator will output two types of results at once. The decimal answer is the original unsigned register value. If the most significant bit was set, then the corresponding signed value represented by that same bit pattern will also be shown.
Sliding operations
A left shift moves all bits towards the higher order positions and fills the lower order bit with a 0. The value is doubled for each shift. A right shift moves the bits toward the lower order position, halving the value. The bits shifted off the end are discarded.
Since shift operations are the cheapest way to multiply and divide by powers of two, compilers use them constantly. For arithmetic shifts which preserve the sign bit, for rotations, and to represent the state of a register before and after a shift, please consult a dedicated tool for computing shifts.
Octal and hexadecimal systems
In the octal system binary numbers are divided into groups of three bits each while in hexadecimal they are grouped by fours. Both systems allow for a compact representation of long binary strings. In hexadecimal letters A to F are assigned to the numbers ten through fifteen respectively. For this reason masks and memory addresses are almost always represented in hexadecimal. This tool accepts decimal input and displays all results also in binary, octal and hexadecimal form.
Here's how to use this calculator.
Enter two decimal numbers and select an operation. For the operations AND, OR, XOR, NAND, NOR, and XNOR, the calculator combines the two numbers bitwise. NOT inverts only the first number, while for the shift operation each bit of the first number is shifted according to the set amount.
Adjust the register width to match your data and check beside each base's results also the bitwise breakdown, truth table for the selected operation as well as number of bits set.
Frequently asked questions
- What are bitwise operations?
Bitwise operations combine numbers by processing a single bit of each. AND, OR and XOR take two numbers and apply a rule to each pair of bits so for example 12 AND 10 is 8. These are building blocks for masks, flags and low level data manipulation and can be done in one step on a processor.
- What is the difference between AND, OR and XOR?
AND can be used to preserve or mask bits since it only returns a 1 if both bits in the same position are 1. OR can be used to set bits since it returns a 1 if either bit in the same position is 1. XOR can be used to invert bits or detect changes since it only returns a 1 if the two bits are different. For example, 12 AND 10 gives 8, 12 OR 10 gives 14 and 12 XOR 10 gives 6.
- How to use bitwise operations for flags and permissions?
Assign a unique bit to each parameter. To combine flags, do an OR operation. To check for a flag, do an AND operation with that bit and check if the result is nonzero. To unset a flag, do an AND operation with the complement of that bit. In Unix permissions 4 means read, 2 means write, and 1 means execute. So a value of 6 means you have permission to both read and write but not execute.
- How does register width affect the result?
The width determines the number of bit positions available. NOT, NAND, NOR, XNOR and Shift fill or clear bits up to that width. Inverting 5 gives 10 (in a 4-bit register). Inverting the same value gives 250 (in an 8-bit register). Choosing an appropriate width allows you to work with values from a nibble of 4-bits, through integers of 32-bits.
- Can this calculator handle hexadecimal or negative numbers?
Any result is shown in hexadecimal representation but since the characters A through F cannot be entered into a normal numeric field, inputs are accepted as decimal numbers. Negative numbers are read by the register as bit patterns of two's complement representation and the result with highest bit set is displayed both as unsigned and signed number.
Related calculators






Disclaimer: This calculator is provided for general informational and educational purposes only. Our calculators are under active development, and results may be inaccurate, incomplete, or unsuitable for your situation. Always verify the figures independently and seek advice from a qualified professional before relying on them. We make no warranties and accept no liability for any loss or decision arising from use of this tool.
References
- MDN: Bitwise operators
Reference for the AND, OR, XOR, NOT, and shift operators on integers.
- Wikipedia: Bitwise operation
Definitions and truth tables for the bitwise and logical operations.
- Wikipedia: Mask (computing)
How bit masks set, clear, toggle, and test individual bits and flags.
- Wikipedia: Two's complement
How fixed-width registers represent signed numbers and their sign bit.