Symmetric Bijective Algorithm for Integers
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A symmetric bijective algorithm for integers usually means a reversible mapping where every integer maps to exactly one value and every output maps back to exactly one input. A common practical example is mapping all integers, including negatives, onto the non-negative integers in a way that is easy to invert.
This kind of mapping is useful in encoding schemes, serialization formats, and algorithms that need a reversible translation between signed and unsigned domains.
A Classic Integer Bijection
One standard mapping from all integers to the natural numbers is:
- non-negative integers go to even numbers
- negative integers go to odd numbers
Written as code, the forward mapping looks like this:
This produces a one-to-one mapping such as:
- '
0 -> 0' - '
1 -> 2' - '
2 -> 4' - '
-1 -> 1' - '
-2 -> 3' - '
-3 -> 5'
Every integer gets a unique non-negative output.
The Inverse Mapping
A bijection is only useful if you can reverse it cleanly. The inverse of the previous mapping is:
This works because even outputs came from non-negative integers and odd outputs came from negative integers. The parity of the encoded value tells you which inverse branch to use.
Why This Mapping Is Bijective
The mapping is injective because two different integers cannot produce the same encoded value. Positive and negative inputs land on disjoint parity classes, and within each branch the arithmetic is one-to-one.
It is surjective onto the non-negative integers because every non-negative integer is either even or odd, so every output value belongs to one of the two branches.
Being both injective and surjective makes the mapping bijective.
Why People Call It Symmetric
The mapping is often described as symmetric because it treats values on both sides of zero in a balanced, structured way. Zero starts the sequence, positive values occupy the even positions, and negative values occupy the odd positions.
The pattern is regular and easy to reason about, which is one reason it is so popular in practical encodings.
A Full Round-Trip Test
A quick test is a good way to confirm the algorithm is truly reversible:
If the round-trip succeeds for all tested values, the forward and inverse functions are aligned correctly.
Common Pitfalls
The biggest mistake is writing a forward mapping without also defining and testing the inverse. A reversible algorithm is only as good as its decode step.
Another pitfall is mixing integer division rules carelessly when translating the formula between languages. Make sure the implementation uses the integer arithmetic semantics you expect.
A third issue is using the word symmetric loosely. Not every reversible mapping is symmetric in structure, so it helps to define the property you actually care about rather than relying only on the label.
Summary
- A bijective integer mapping must be one-to-one and fully reversible.
- A common encoding maps non-negative integers to even numbers and negative integers to odd numbers.
- The inverse mapping uses parity to recover the original sign and magnitude.
- Round-trip tests are the simplest way to validate the implementation.
- This kind of mapping is useful whenever signed integers must be encoded into a non-negative domain.

