Why a Gray code changes one bit at a time
Ordinary binary counting can change several bits at a boundary. If the bits do not switch simultaneously, a device may briefly observe an intermediate pattern. A reflected Gray sequence changes only one bit between adjacent values, which can reduce that transition ambiguity in encoders and similar systems. The benefit concerns adjacent transitions, not every possible pair of values.
Enter a nonnegative integer
Enter a nonnegative integer within the calculator’s supported 32-bit range. The tool encodes the value supplied; it does not accept an arbitrary binary string or choose a number of output bits from a hardware specification. If a device requires a fixed width, preserve that width when displaying or transmitting the resulting code.
Apply the binary-to-Gray transform
The binary-reflected transform is G = n XOR (n >> 1), where >> shifts the integer one bit to the right and XOR compares corresponding bits. The leading positions align to the integer representation, and the XOR leaves a one wherever neighboring binary positions differ. IEEE technical material describes the one-bit adjacency property of reflective binary codes.
Read the encoded word as a code
Gray code is a code representation, not a decimal conversion or a checksum. Its bit pattern can look numerically unrelated to the input integer when interpreted as ordinary binary. To recover a number, use a Gray-to-binary decoding process; interpreting the encoded digits as a plain base-two integer does not reverse the transform.
Use the code in measurement systems
Rotary encoders, position sensors, and state transitions are common contexts where a one-bit change between adjacent positions is useful. A real device still needs electrical timing, noise handling, mechanical calibration, and a defined bit width. The encoding alone does not guarantee that readings are error-free or that a sensor is accurate.
The single-bit adjacency property is most useful when a sensor reports a changing position and a boundary may be sampled mid-transition. It does not mean one-bit errors are impossible, nor does it add redundancy for correction. If reliability matters, combine the encoding with appropriate sampling, filtering, diagnostics, and a separate error-detection strategy specified for the system.
Gray-to-binary conversion is a separate cumulative operation: the most significant binary bit matches the leading Gray bit, and each following binary bit is the previous binary bit XOR the next Gray bit. This reverse rule is useful when reading a sensor output, but it is intentionally outside this encoder. Keep encoded and decoded values labeled separately when checking a register or logging position data.
Worked examples
The calculations use the integer transform directly; the resulting code is shown as a base-two pattern where helpful.
Encode decimal ten
Inputs: Unsigned integer n = 10.
Calculation: Binary n = 1010₂; n >> 1 = 0101₂; Gray = 1010 XOR 0101 = 1111₂ = 15.
The Gray-code integer value is 15, represented by the four-bit word 1111.
Encode decimal twenty-three
Inputs: Unsigned integer n = 23.
Calculation: Binary n = 10111₂; n >> 1 = 01011₂; Gray = 10111 XOR 01011 = 11100₂ = 28.
The encoded Gray value is 28, displayed as the five-bit word 11100.
Limits of this integer encoder
The sequence is cyclic at a chosen fixed width only when the full code space and the wraparound convention are considered. If a system uses fewer states than the full range, its transition order and decoding table should be checked against the device design. This calculator returns the standard reflected encoding for the given integer, not a custom state assignment.
The implementation uses JavaScript bitwise operations, which operate on signed 32-bit integer representations. Keep the input within the supported nonnegative integer range and verify the runtime’s handling of boundary values before using them in hardware. Wider counters require a different implementation with explicit big-integer or bit-array logic.