· 5 min read
How to Calculate Binary AND, OR, and XOR
Manesh Jayawardhana
CIO & Co-founder
You are reading a permissions field where 12 means 1100 in binary, and you need to know what happens when it is XORed with 10 (1010). Doing the operation in decimal hides which bits change. Doing it by hand is clearer, but one misaligned digit can give you the wrong flag mask and send you debugging in the wrong direction.
A programmer’s calculator can handle this, yet it often buries the binary representation behind modes and unfamiliar buttons. For a quick check, it is more useful to enter ordinary decimal integers, choose AND, OR, XOR, left shift, or right shift, and see the decimal and binary results together.
What bitwise operations actually do
Bitwise operations compare or move the individual binary digits—the bits—inside whole numbers. Line up two values on the right, then evaluate each column independently.
With 12 (1100) and 10 (1010), AND keeps a 1 only where both values contain 1, producing 1000, or 8. OR keeps a 1 where either value contains one, producing 1110, or 14. XOR keeps a 1 where the values differ, producing 0110, or 6.
Shifts work differently. A left shift moves bits left and adds zeros on the right, which resembles multiplying by powers of two while the result stays within range. A signed right shift moves bits right and preserves the sign bit for negative 32-bit values.
| Operation | Bit Rule | 12 and 10 | Decimal |
|---|---|---|---|
| AND | Both bits are 1 | 1100 & 1010 = 1000 | 8 |
| OR | Either bit is 1 | 1100 | 1010 = 1110 | 14 |
| XOR | Bits are different | 1100 ^ 1010 = 0110 | 6 |
| Left shift | Move bits left | 1100 << 2 = 110000 | 48 |
For shifts, the second input is a shift count rather than another value to compare bit by bit.
Why people get stuck here
The first problem is alignment. 10 in decimal is 1010 in binary, not the text “10,” and shorter binary values must be aligned at the least-significant bit on the right. Padding 110 as 0110 before comparing it with 1010 makes the columns visible.
The second problem is the integer model. The site’s calculator uses the browser’s JavaScript bitwise behavior, so values are treated as 32-bit integers. Large decimal values can therefore wrap into a signed 32-bit result. Shift counts are effectively limited to the low five bits, which means counts repeat modulo 32. This is useful for matching typical JavaScript behavior, but it is not an arbitrary-precision binary calculator.
Negative results can also surprise people. The decimal display may show a signed number, while the binary representation reflects its 32-bit two’s-complement bit pattern. Both views describe the same bits using different interpretations.
What a good calculation looks like
Inputs are confirmed as integers
Bitwise operations act on whole numbers. A value such as 3.5 has no direct place in this calculator’s operation, and silently rounding it would obscure what was actually tested. Use valid integer inputs and decide whether signed 32-bit behavior matches your real system.
Binary columns are easy to align
Grouped or padded output makes a manual check possible. If the calculator says 12 XOR 10 = 6, writing 1100, 1010, and 0110 beneath one another lets you verify every bit instead of trusting an unexplained decimal.
The operation matches the intent
Use AND to test or clear selected flags, OR to set flags, and XOR to toggle bits or detect differences. A left shift is suitable for moving a bit mask to a higher position; it is not interchangeable with XOR simply because both can change several bits.
If you are still getting comfortable moving between representations, this guide to converting text to binary and back provides helpful background. For regular arithmetic in base two, see how to use a binary calculator.
Common mistakes to avoid
- Treating decimal
10as though its binary representation were10; decimal 10 is binary1010. - Aligning binary digits on the left instead of at the least-significant bit on the right.
- Using XOR when the real goal is to test whether a flag is set; that check normally uses AND.
- Expecting unlimited integer size even though the calculation follows signed 32-bit JavaScript rules.
- Reading a negative decimal result without checking its full two’s-complement binary pattern.
How to do it with Binary Operations Calculator
Open the Binary Operations Calculator when you want a fast, inspectable result rather than a hidden calculator mode.
- Enter the first whole number in decimal form.
- Select AND, OR, XOR, Left shift, or Right shift.
- Enter the second whole number. For a shift, this value is the number of bit positions.
- Choose Calculate.
- Read the signed decimal result, then inspect the padded binary output and the working line.
Try 12, XOR, and 10. The expected decimal result is 6, and the low four binary bits should read 0110. Everything runs locally in the page, with no input upload or sign-up.
Frequently asked questions
What is the difference between OR and XOR?
OR returns 1 when either input bit—or both—is 1. XOR returns 1 only when the two bits differ. For 1 and 1, OR returns 1, while XOR returns 0.
Why can a left shift produce a negative number?
In a signed 32-bit calculation, shifting a 1 into the highest bit makes that pattern represent a negative value. It is not a display error; it is the signed interpretation of the resulting 32-bit pattern.
Does this calculator accept binary input strings?
The current inputs are decimal number fields. Enter ordinary whole numbers and use the binary result for inspection. If your starting value is a binary string, convert it to decimal first using a suitable tool from the tool directory.
Final thought
The safest way to check a bitwise result is to keep both views visible: decimal for the value your code may log, and aligned binary for the bits your operation actually changed. If those two stories agree, the mask is much easier to trust.