Bitwise Operations (AND, OR, XOR, NOT & Bit Shifts) Calculator

Inspect bitwise logic gates and bit-shift arithmetic on non-negative integers.

Bitwise XOR (A ^ B) 37
Bitwise AND (A & B) 10
Bitwise OR (A | B) 47
Left Shift (A << 2) 168
Inputs
Try:
First unsigned integer operand A.
Second unsigned integer operand B.
Bit shift count k for A << k and A >> k.
Results & Graph
Bitwise XOR (A ^ B)
37
Bitwise AND (A & B)
10
Bitwise OR (A | B)
47
Left Shift (A << 2)
168
Right Shift (A >>> 2)
10
NOT A (32-bit, ~A)
4,294,967,253
Hamming distance (bits that differ)
3

Bitwise XOR (A ^ B)

Graph 1 • How the inputs feed Bitwise Operations
Bitwise Operations (AND, OR, XOR, NOT & Bit Shifts) Calculator • Custom Domain Schematic (Discrete Math & Logic) intAintBBitwise Operations Int A: 42 • Int B: 15 BITWISE XOR (A ^ B) 37 • Bitwise AND (A & B): 10 • Bitwise OR (A | B): 47 Eq: XOR = (A | B) − (A & B); Hamming
Graph 2 • Bitwise XOR (A ^ B) as Int A changes (40% to 160% of current)
0 32.56 65.12 14.80 16.80 22.20 25.20 29.60 33.60 37 42 44.40 50.40 51.80 58.80 59.20 67.20
Graph 3 • Int A and Int B together Compares Bitwise XOR (A ^ B) when Int B is shifted +25% (solid) vs. −25% (dashed).
0 26.46 52.91 --- Int B @ 75% 25.90 -30% 31.45 -15% 37 +0% 42.55 +15% 48.10 +30%
Summary: For A = 42 and B = 15: A & B = 10, A | B = 47, A ^ B = 37, and A << 2 = 168 (kept to 32 bits). A and B differ in 3 bits.
Summary: For A = 42 and B = 15: A & B = 10, A | B = 47, A ^ B = 37, and A << 2 = 168 (kept to 32 bits). A and B differ in 3 bits.
Interactive chart & math engine powered by CalculatorGraph

Formula: Bitwise Operations Mathematical Identity

XOR = (A | B) − (A & B)
HammingDistance = popcount(A ^ B)
XOR = (A | B) − (A & B); HammingDistance = popcount(A ^ B)
  • intA: Int A (numeric parameter)
  • intB: Int B (numeric parameter)
  • shiftBits: Shift Bits (numeric parameter)

Frequently Asked Questions

Why is 42 >>> 2 equal to 10 and not 10.5?

A right shift drops the bits that move past the lowest position, which is the same as dividing by a power of two and discarding the remainder. 42 divided by 4 is 10 with 2 left over, and that 2 is lost. The result is always a whole number, rounded down for the non negative inputs this calculator accepts.

How can I see the binary form of the results?

The page shows results in decimal only. To read the bits, break the number into powers of two. 37 is 32 + 4 + 1, so in binary it has ones in the 32, 4 and 1 places and zeros elsewhere. Writing the number in hex, 0x25 for 37, also helps because each hex digit is four bits.

Does the order of A and B matter?

For AND, OR and XOR, no. All three give the same answer whichever way round you enter the values: 42 & 15 and 15 & 42 are both 10. The shifts do depend on order, because they only apply to A. Swap the values and the left and right shift lines change while the other three stay put.

What does a XOR result of zero tell me?

It means A and B are identical in every bit. Entering 12 for both gives an XOR of 0, while AND and OR both return 12. Programs often use this as a quick equality test or to clear a register. It also means the Hamming distance between the two values is zero.

Can I enter hexadecimal values like 0x2A?

The input boxes take plain decimal numbers. Convert hex first: 0x2A is 2 times 16 plus 10, which is 42, the default. Each hex digit stands for four bits, so 0xF is 15 and 0xFF is the full low byte. Typing letters into the box will not work.

Why does left shifting 42 by 31 give 0?

Results are kept to 32 bits. Shifting 42 by 31 moves its lowest set bit, worth 2, past the top of the word, and all its higher bits go even further. Nothing is left inside, so the answer is 0. At a shift of 30 one bit survives in the top position, so the result is about 2.1 billion.

Is A << 2 always the same as A times 4?

Only while the result fits in 32 bits. For the defaults, 42 << 2 is 168, exactly 42 times 4, and 63 << 2 is 252 for the Higher preset. For values above about a billion, shifting by 2 pushes bits past the top of the word, they are dropped, and the result no longer matches ordinary multiplication.

What exactly does the Hamming distance line count?

It counts the positions among the 32 bits where A and B have different values, which is the number of ones in A ^ B. For 42 and 15 the XOR is 37, which is 32 + 4 + 1, so the line shows 3. Two equal values give 0, and A = 0 against the 32 bit maximum gives all 32.

What happens if I type a fraction like 2.7?

The page shows an error asking for a whole number from 0 to the 32 bit maximum, and no results are calculated. Bitwise operations act on individual bits of an integer, so a fraction has no bit pattern to work on. Round the value yourself first if that is what you meant, then enter the whole number.

Why does XOR equal OR minus AND?

OR includes every bit that is on in either number. AND includes only the bits on in both. Subtracting AND from OR removes the shared bits, and since no bit is counted twice in either result, the subtraction never borrows. What remains is the set of bits that differ, which is XOR: 47 minus 10 is 37.

Can I use this to check subnet masks?

Yes. An IPv4 address and its subnet mask are both 32 bit unsigned values, which is exactly what this calculator uses. Convert each dotted address to a single whole number, enter the address as A and the mask as B, and the AND line gives the network address as a number. Converting back to dotted form is a separate step.

Working 42 and 15 out bit by bit

With the defaults of A = 42, B = 15 and a shift of 2, the page shows A & B = 10, A | B = 47, A ^ B = 37, A << 2 = 168 and A >>> 2 = 10. It also gives NOT A as 4,294,967,253 and a Hamming distance of 3. Every one of those can be checked on paper, and doing it once makes the rest of the calculator much easier to trust.

Write each number as a sum of powers of two. 42 is 32 + 8 + 2, so it has the bits worth 32, 8 and 2 switched on. 15 is 8 + 4 + 2 + 1, the lowest four bits all on.

AND keeps only the bits that are on in both. The bits worth 8 and 2 appear in both numbers, so A & B is 8 + 2 = 10. OR keeps every bit that is on in either: 32, 8, 4, 2 and 1, which add up to 47. XOR keeps the bits that are on in exactly one of them: 32 from A, and 4 and 1 from B. That is 37, the headline answer.

The formula on the page, XOR = (A | B) minus (A & B), says the same thing in arithmetic. OR counts every bit present in either number. AND counts the ones they share. Take the shared ones away and what is left is the bits that differ: 47 minus 10 gives 37.

Shifts as multiplying and dividing by powers of two

A left shift moves every bit up by the shift count and fills the bottom with zeros. Each place up doubles a bit's value, so shifting left by 2 multiplies by 4. That is why 42 << 2 gives 168 on the page.

A right shift moves the bits down and drops whatever falls off the bottom. Shifting right by 2 divides by 4 and throws away the remainder. 42 divided by 4 is 10.5, and the half disappears, so 42 >>> 2 is 10. The Lower preset shows the same effect with 25: 25 >> 2 is 6, not 6.25.

Change the shift to 4 and the right shift of 42 becomes 2, because 42 divided by 16 is 2 with 10 left over. That leftover 10 is exactly what you get from 42 & 15. Shifting right by 4 and masking with 15 together split a number into its top part and its bottom four bits, which is how hex digits are pulled out of a byte.

The shift count applies to A only. B is not shifted in any of the outputs, so if you want to see what happens to B, swap the two values.

The right shift shown is the unsigned kind, written >>> in JavaScript and Java. It always fills the top with zeros. Because every value here is treated as unsigned, there is no sign bit to copy down, so the result is simply the number divided by a power of two and rounded down.

Masks: when one input is all ones

The default B of 15 is a mask. In hex it is 0xF, the lowest four bits all set. AND with a mask keeps the bits under the mask and clears everything else. OR with a mask forces those bits on. XOR with a mask flips them.

The Higher preset makes this very visible. With A = 63 and B = 15, A & B comes out as 15, A | B as 63 and A ^ B as 48. Because 63 already has all four low bits on, the AND just returns the mask, the OR adds nothing new, and the XOR switches those four bits off, leaving 63 minus 15.

Programs use the same idea for settings stored as flags. Each option is one bit. To test whether flag 8 is set, AND the value with 8 and see if the answer is zero. With A = 42 and B = 8, the AND is 8, so the flag is on. To switch it off, XOR with 8 again. The calculator lets you check each step before you write it in code.

A mask does not have to be in the low bits. B = 32 would test the top bit of 42. Any power of two picks out one position; any sum of powers of two picks out a group.

Checking the Hamming distance line against the XOR

The last result line counts the bit positions where A and B differ. For the defaults it reads 3. That count comes straight from the XOR result.

XOR sets a bit wherever the two inputs disagree. So the Hamming distance is simply the number of set bits in A ^ B. For the defaults, 37 is 32 + 4 + 1, three bits, which matches the 3 on the page.

Check it from the other side. 42 has bits 32, 8 and 2. 15 has bits 8, 4, 2 and 1. They share 8 and 2. The 32 is only in A, and the 4 and 1 are only in B. Three differences.

With A = 63 and B = 15 the XOR is 48, which is 32 + 16, so the distance is 2, as the Higher preset shows. When A and B are equal, the XOR is 0 and so is the distance. Try A = 12 and B = 12 and you get 0 for both, while AND and OR return 12. Change B to 1 with A = 42 and the distance rises to 4, because the lowest bit now differs too.

The unsigned 32 bit range and what it refuses

Every value here is an unsigned 32 bit integer. Inputs can run from 0 up to 2 to the power 32 minus 1, just under 4.3 billion, and every result stays inside that range. Nothing is ever shown as negative.

That makes large values behave the way they do in C with an unsigned int, or in a network packet. A of three billion with B = 15 gives an XOR of three billion and 15, the same as adding 15, since three billion has none of the low four bits set. IPv4 addresses and colour values with an alpha channel, which use the full 32 bits, work as expected.

Anything outside that range is refused with a message asking for a whole number from 0 to the 32 bit maximum. Negative numbers fall into that group, so the page cannot show two's complement behaviour for signed values. Decimals are refused too: 2.7 gives the same error rather than being quietly rounded, since bitwise operators only make sense on whole numbers.

If you are porting code that uses signed 32 bit integers, results with the top bit set will differ in how they print. The bit pattern is the same; only the reading as a decimal changes.

Shift counts from 0 to 31, and bits that fall off

The shift box accepts 0 to 31. A count of 32 or more returns an error, because shifting a 32 bit word by its own width is undefined in several languages and would empty the word anyway.

Left shifts keep only the lowest 32 bits, so bits pushed past the top are lost. 42 << 2 is 168, an exact multiplication by 4. At 42 << 30 only one bit of 42 survives, the one that lands in the top position, and the result is 2 to the power 31, about 2.1 billion. At 42 << 31 every set bit has gone past the top and the result is 0.

Right shifts run out the other way. 42 >>> 30 is 0, because every bit of 42 has moved off the bottom. A shift of 0 leaves A unchanged in both directions.

If you are reproducing a shift from a language with 64 bit integers, expect the left shift results here to differ once bits cross position 31, since those bits are kept there and dropped here.

Quick checks that use AND, OR and XOR

A handful of common programming tricks can be tested here with small numbers before you rely on them.

To check whether a number is odd, AND it with 1. With A = 42 and B = 1, the AND is 0, so 42 is even. The XOR with 1 gives 43, which shows XOR with 1 flips the lowest bit.

To test whether a number is a power of two, AND it with itself minus one. A = 64 and B = 63 gives an AND of 0, so 64 is a power of two. A = 40 and B = 39 gives 32, not zero, so 40 is not.

XOR undoes itself. 42 ^ 15 is 37, and 37 ^ 15 brings you back to 42. That property is the basis of simple XOR ciphers, checksums and the old swap trick with no temporary variable. It also means that if you know any two of A, B and A ^ B, you can always recover the third.

OR combines flags without disturbing ones that are already set. 42 | 8 stays 42, because the 8 bit was already on.

NOT A flips all 32 bits

The NOT line inverts every one of the 32 bits of A. For A = 42 it reads 4,294,967,253 in decimal. That looks enormous next to 42, but it is just the 32 bit maximum minus 42: every bit that was off is now on, including the 26 high bits that 42 never used.

The same rule explains the presets. NOT 63 is 4,294,967,232, and NOT 25 is 4,294,967,270 on the Lower preset. Adding A to NOT A always gives the 32 bit maximum, which is a quick way to check the line.

Often you only want to flip the bits in a small field, such as the low four bits of a flag byte. For that, XOR with a mask of ones instead. With A = 42 and B = 15 the XOR of 37 is 42 with only those four bits flipped. For the low six bits, use 63 as the mask.

In a language with signed integers, ~42 prints as minus 43. It is the same bit pattern as the 4,294,967,253 shown here, read as signed rather than unsigned.