GGraySorrel
Workflows

Coding workflows · Fixed-width Gray code conversion

03
Built around your work

A concrete starting point

Convert integer words to Gray code and back with an explicit width from one through 16 bits. Test zero and the largest fitting value, rejecting Booleans, invalid widths and values that do not fit.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Fixed-width Gray code conversion

A concrete starting point
def checked_word(value, width):
    if type(width) is not int or not 1 <= width <= 16:
        raise ValueError("word.width")
    if type(value) is not int or not 0 <= value < (1 << width):
        raise ValueError("word.value")
    return value

def to_gray(value, width):
    value = checked_word(value, width)
    return value ^ (value >> 1)

def from_gray(value, width):
    value = checked_word(value, width)
    result = 0
    while value:
        result ^= value
        value >>= 1
    return result

The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.

AI coding workspace

Your next change starts here.

Get the Windows app ↗