ForHosting KIT · Developer Utilities

Hamming Distance Between Integers Calculator

The Hamming distance between two integers is the number of bit positions in which their binary representations differ.

● BetaFree · in your browser
Use it from WebAPIEmailTelegramApp soon

This calculator performs the standard operation directly: it computes the bitwise XOR of the two values, then counts the set bits in that result. Use it to compare flags, masks, compact identifiers, encoded states, or test vectors without manually aligning binary strings. Inputs may be any non-negative JavaScript safe integers, including zero, and the result includes both the XOR value and its binary form for inspection.

What integer Hamming distance measures

Hamming distance was originally described for strings of equal length, but integers provide a natural bit-string interpretation. Each integer is written in base two, and leading zeroes are understood wherever one representation is shorter. A position contributes one to the distance when one integer has a zero bit there and the other has a one bit. Matching zero-zero and one-one positions contribute nothing. For example, comparing two permission masks reveals how many individual flags changed, while comparing two compact states reveals the number of toggled binary features. The result is a count of positions, not the arithmetic gap between the numbers. Two numerically close values can differ in many bits, and two values far apart can differ in only one. This capability accepts non-negative safe integers so that each submitted number has an exact JavaScript representation in both the API handler and the browser runner. Zero is valid, and its distance from another integer equals the number of set bits in that other integer.

How XOR and set-bit counting produce the answer

Exclusive OR, usually written XOR, is the key operation because its truth table matches the definition of a differing bit. XOR produces zero when two aligned bits match and one when they differ. After applying XOR to the two integers, the problem becomes a population count: count how many one bits appear in the result. The implementation uses Brian Kernighan's set-bit method. On every iteration, subtracting one from the current value and applying bitwise AND clears its lowest set bit. The loop therefore runs once per differing position rather than once per possible bit position. BigInt arithmetic is used internally, which avoids JavaScript's ordinary bitwise operators silently narrowing values to signed 32-bit integers. The returned XOR remains safe to expose as a regular number because XOR cannot set a bit above those already present in either validated safe integer. The binary XOR string is also returned, making the counted positions easy to verify during debugging or teaching.

Choosing inputs and interpreting the result

Provide the two values as the fields a and b. Each must be an integer from zero through the largest exactly represented JavaScript integer; fractions, negative values, numeric strings, infinities, and missing fields are rejected as invalid input. The response contains xor, binary_xor, and hamming_distance. The decimal xor field is convenient for downstream bit-mask operations, binary_xor shows exactly which positions differ, and hamming_distance gives their count. Leading zeroes are intentionally omitted from binary_xor because they never change the distance. If your source values are wider than the safe-integer range, preserve them outside this capability rather than rounding them into numbers, since rounding can change low bits and yield a misleading distance. Common integrations use the count as a threshold, such as accepting a code within one changed bit, flagging a state with several toggles, or checking a known implementation against fixed vectors. The operation is symmetric, so exchanging a and b always produces the same output, and comparing any value with itself always yields zero.

Compare feature or permission masks

Count how many individual flags changed between two stored integer masks and inspect the XOR mask that identifies them.

Validate bit-manipulation code

Generate a deterministic expected distance and XOR representation for unit tests, coding exercises, or algorithm checks.

Measure encoded state changes

Summarize how many binary features differ between two compact states without confusing bit changes with numeric subtraction.

What does this capability cost?

API requests cost $0.002 each. The same deterministic calculation can also run in the browser.

Why use XOR to calculate Hamming distance?

XOR places a one exactly where the two input bits differ, so counting its set bits gives the distance directly.

Are leading zeroes counted?

No. Leading zeroes match the implicit leading zeroes of the other non-negative integer and therefore add nothing to the distance.

Can I submit negative integers?

No. Negative values require a chosen signed width and representation, so this capability deliberately accepts only non-negative integers.

What is the largest accepted value?

Each input may be any non-negative JavaScript safe integer, up to 9,007,199,254,740,991.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/dev/hamming-distance-int

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/dev/hamming-distance-int \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"a":29,"b":15}'
{
  "a": 29,
  "b": 15
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "dev.hamming_distance_int",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Read the full KIT documentation →