Hamming Distance Calculator
- Written in binary, the first number is 1001 1100 and the second is 0110 0011. The Hamming distance counts the bit positions where the two strings disagree.
- XOR the pair and the disagreements light up as 1s: 1111 1111. Counting those 1s gives the distance 8, which is also the Hamming weight of the XOR value 255.
- The first number carries 4 set bits and the second carries 4. Hamming distance is the standard measure of how far apart two code words sit, and it sets how many bit flips an error-correcting code can detect or repair.
- Two identical numbers have distance 0; flipping every bit of an 8-bit value gives the maximum distance of 8.
This hamming distance calculator tells you how many bit positions two numbers differ in. Enter any two values and the tool compares them bit by bit, so you get a clear count instead of a guess. The results are easy to check against real examples. The numbers 156 and 99 differ in all 8 bit positions, and their XOR is 255. The numbers 1024 and 1025 are a Hamming distance of 1 apart, differing only in the lowest bit. The numbers 255 and 0 sit the maximum distance of 8 apart for a byte. In the first example, 156 and 99 carry 4 set bits each, which shows why counting differing positions is a separate question from counting set bits in either number alone.
The method behind the calculator is the accepted standard for this kind of calculation: Hamming distance is the Hamming weight of the XOR of the two numbers. XOR marks exactly the positions where the two inputs disagree, and the Hamming weight counts how many of those positions are set, so the result is the number of differing bits by definition rather than by approximation. This same measure matters in practice, because error-correcting codes use Hamming distance to set how many bit flips they can detect or repair. This page shows the formula, step-by-step workings, worked examples and sources, so you can follow every result back to the definition and confirm it yourself.
Worked examples
156 vs 99
(156 · 99)
| Hamming distance | 8 |
| XOR of the two numbers | 255 |
| Set bits in the first number | 4 |
| Set bits in the second number | 4 |
255 vs 0
(255 · 0)
| Hamming distance | 8 |
| XOR of the two numbers | 255 |
| Set bits in the first number | 8 |
| Set bits in the second number | 0 |
1024 vs 1025
(1024 · 1025)
| Hamming distance | 1 |
| XOR of the two numbers | 1 |
| Set bits in the first number | 1 |
| Set bits in the second number | 2 |
How Hamming distance is calculated
To see how a hamming distance calculator works, line the two binary strings up so their bits sit in the same positions, combine them with XOR, and count the 1s that come out. XOR is the right operation because it outputs 1 only where the two bits disagree and 0 where they match. Every 1 in the result marks one bit position where the strings differ, so the count of 1s is the distance. Stated formally, Hamming distance is the Hamming weight of the XOR of the two numbers.
The worked example is 156 against 99. Written as bytes, 156 is 10011100 and 99 is 01100011. Aligning them bit by bit shows no position where the two agree: every column holds a 1 on one side and a 0 on the other. XORing the pair therefore returns 11111111, which is 255 in decimal, and counting the 1s gives a distance of 8. That matches the description of the two numbers as differing in all 8 bit positions, and it is worth noting that each of the original numbers carries 4 set bits — the distance is not the sum of those weights but the weight of their XOR.
A second example shows how small the distance can be. The numbers 1024 and 1025 are a Hamming distance of 1 apart, because they differ only in the lowest bit: changing that single bit on one number produces the other. At the other extreme, 255 and 0 sit the maximum distance of 8 apart for a byte, since 255 is 11111111 and 0 is 00000000 and every one of the eight positions disagrees. A byte can therefore be at most 8 apart from another byte, and the distance between two equal values is always 0.
The same three steps — align, XOR, count — scale to any string length, and they explain why the measure matters in practice. Error-correcting codes use Hamming distance to set how many bit flips they can detect or repair, because the distance between valid codewords determines how far a corrupted message can drift before it is mistaken for a different valid one.
- Write both values as binary strings of equal length and align them position by position.
- XOR the aligned strings; a 1 appears wherever the bits disagree.
- Count the 1s in the XOR result — that count is the Hamming distance.
- Check it on the example: 156 XOR 99 = 255, which has eight 1s, so the distance is 8.
Hamming distance and Hamming weight
The quickest way to understand what a hamming distance calculator is doing is to look at two related ideas: Hamming distance and Hamming weight. Hamming weight is simply the count of set bits — the 1s — in a number's binary form. Hamming distance is the count of positions where two numbers' binary forms disagree. The link between them is an operation you already know: XOR. Hamming distance is the Hamming weight of the XOR of the two numbers. XOR produces a 1 in exactly the positions where the two inputs differ, so counting the set bits of that result counts the disagreements. Every distance the calculator reports can be read this way.
Take the pair 156 and 99. These two numbers differ in all 8 bit positions, so their XOR is 255 — eight set bits, the largest possible result for a byte. Yet the first example's numbers carry 4 set bits each. That is the key contrast: the two weights are equal at 4 and 4, but the distance between them is 8. Weight tells you how many 1s a single number has; distance tells you how far apart two numbers are. Equal weights say nothing about distance, and here two numbers with identical set-bit counts sit as far apart as a byte allows.
Small distances show the same rule from the other side. 1024 and 1025 are a Hamming distance of 1 apart, differing only in the lowest bit. Their XOR has a single set bit, so its weight is 1 — the distance. At the other extreme, 255 and 0 sit the maximum distance of 8 apart for a byte, because every one of the eight positions disagrees. The range of possible answers for a byte therefore runs from 0, for two identical values, up to 8.
This is more than arithmetic. Error-correcting codes use Hamming distance to set how many bit flips they can detect or repair. The distance between valid codewords determines that capacity, which is why counting differing bits accurately matters in storage and transmission as well as in the classroom.
- Hamming weight: the number of set bits in one number.
- Hamming distance: the number of positions where two numbers differ.
- The rule: distance equals the weight of the XOR.
- 156 and 99: weights of 4 and 4, XOR of 255, distance of 8.
- 1024 and 1025: distance of 1, differing only in the lowest bit.
- 255 and 0: distance of 8, the maximum for a byte.
Error detection and correction
Error-correcting codes use Hamming distance to set how many bit flips they can detect or repair. The key number is the minimum distance of a code: the smallest Hamming distance between any two valid code words in the set. If every pair of valid code words is at least 3 bits apart, the code has a minimum distance of 3. That single number decides what the code can do, and a hamming distance calculator lets you measure the gap between any two words you are comparing.
A minimum distance of 3 detects up to 2 errors or corrects 1. Detection works because a single flipped bit, or even two flipped bits, cannot turn one valid code word into another: the result is still at least 1 bit away from every valid word, so the error is visible. Correction works because a word that is 1 bit away from a valid code word is closer to that word than to any other, since every other valid word is at least 3 away. The receiver snaps the damaged word back to its nearest valid neighbor and recovers the original. With two flips, the damaged word can sit 2 bits from its source and 1 bit from a different valid word, so the nearest-neighbor choice becomes ambiguous — which is why distance 3 corrects only 1 error.
When two code words are identical, their Hamming distance is 0. This follows directly from the definition: Hamming distance is the Hamming weight of the XOR of the two numbers, and a number XORed with itself gives 0, which has no set bits. Zero distance is the floor of the scale and simply confirms the two values are the same word. It is not an error condition; it is the baseline against which every other pair is measured.
The scale runs from that floor up to the width of the word. For a byte, 255 and 0 sit the maximum distance of 8 apart, since every one of the eight bit positions differs. At the other end, 1024 and 1025 are a Hamming distance of 1 apart, differing only in the lowest bit. Somewhere in between, 156 and 99 differ in all 8 bit positions: their XOR is 255, and both numbers carry 4 set bits each — a reminder that equal set-bit counts say nothing about how far apart two words are. Only the XOR tells you that.
These examples show why the minimum distance, not any single pair, is what a code is built around. A code whose valid words are all far apart can afford to detect or repair more flips; a code with words packed close together can do less. When you check a pair with the hamming distance calculator, you are measuring exactly the quantity that sets those limits.
Where Hamming distance is used
Error-correcting memory is one of the clearest places Hamming distance does real work. When a computer stores a byte, it can also store extra check bits alongside it. If a single bit later flips, the stored pattern no longer matches what was written, and the distance between the read value and the original tells the system something went wrong. Error-correcting codes use Hamming distance to set how many bit flips they can detect or repair, so the spacing between valid codewords is a design decision, not an accident. The larger the minimum distance between allowed patterns, the more protection the code provides.
Coding theory leans on the same idea. A code is a chosen set of valid bit strings, and the distance between any two of them measures how many positions must change to turn one into the other. A hamming distance calculator makes that measurement quick for any pair you type in. Take 1024 and 1025: they are a Hamming distance of 1 apart, differing only in the lowest bit. That single-bit gap is the smallest possible for two different numbers, and it is exactly the kind of small difference a code must avoid between valid codewords if it is to catch errors reliably.
Comparing binary fingerprints is another common use. Systems that reduce data to short binary strings — hashes, signatures, feature vectors — often ask how similar two strings are, and counting differing bits answers that directly. The method is consistent because Hamming distance is the Hamming weight of the XOR of the two numbers: XOR the pair, then count the 1s in the result. For 156 and 99, the numbers differ in all 8 bit positions and their XOR is 255, so the distance is 8. At the other extreme, 255 and 0 sit the maximum distance of 8 apart for a byte. In both cases the XOR result makes the count easy to see.
These examples show why the measure is useful across fields. A distance of 1, as with 1024 and 1025, means near-identical patterns. A distance of 8 across a byte means nothing in common at the bit level. The first example's numbers carry 4 set bits each, which is a reminder that the count of set bits in a single number (its Hamming weight) is a separate quantity from the distance between two numbers. Keeping those two ideas straight helps when you read code specifications or compare fingerprints.
In practice, you rarely compute these counts by hand. You XOR the two values and count the set bits, or let a tool do it. Whether the context is memory protection, code design, or fingerprint comparison, the underlying question is the same: how many bit positions differ? That single number tells you how much change separates two patterns, and in error-correcting work it tells you how much change a system is built to survive.
FAQs
What is the Hamming distance between 156 and 99?
It is 8. Every bit position differs between the two numbers, and their XOR is 255, which has all eight bits set to 1. Counting those 1 bits gives 8, the full width of a byte. So 156 and 99 are as far apart as two byte values can be.
How is Hamming distance calculated?
XOR the two numbers and count the 1 bits in the result. XOR sets a bit to 1 only where the two inputs differ, so the count of 1 bits equals the number of differing positions. That count is the Hamming distance.
What is the distance between identical numbers?
It is 0. No bit positions differ between a number and itself, so XORing the two values produces zero and there are no 1 bits to count. A distance of 0 means the two strings are exactly the same.
What is the maximum distance for a byte?
It is 8, since a byte has eight bit positions. The maximum is reached by 255 against 0, where every bit flips: 255 is all 1 bits and 0 is all 0 bits, so all eight positions differ and the distance is 8.
How many errors can a code correct?
A code with minimum distance d corrects (d - 1) / 2 errors, which is why distance is the design parameter. The minimum distance between valid codewords sets how many bit errors can be detected and corrected, so codes are chosen with a target distance in mind.
Is Hamming distance the same as Hamming weight?
No. The weight counts 1 bits in one string; the distance is the weight of the XOR of two strings. So weight describes a single value, while distance compares two values by counting the 1 bits in their XOR result.