Open the context
Choose the project folder and name the files or behavior involved.
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.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
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 resultThe 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.