|
| 1 | +""" |
| 2 | +Convert between Binary and Gray Code representations. |
| 3 | +
|
| 4 | +Gray Code (also known as reflected binary code) is a binary numeral system where |
| 5 | +two successive values differ in only one bit. This property makes it useful in |
| 6 | +error correction, digital communications, and position encoders. |
| 7 | +
|
| 8 | +Wikipedia: https://en.wikipedia.org/wiki/Gray_code |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +def binary_to_gray(binary_number: int) -> int: |
| 13 | + """ |
| 14 | + Convert a binary number to its Gray Code equivalent. |
| 15 | +
|
| 16 | + The algorithm works by XORing the binary number with itself right-shifted by 1. |
| 17 | + Formula: gray = binary XOR (binary >> 1) |
| 18 | +
|
| 19 | + Args: |
| 20 | + binary_number: A positive integer representing a binary number |
| 21 | +
|
| 22 | + Returns: |
| 23 | + The Gray Code equivalent as an integer |
| 24 | +
|
| 25 | + Examples: |
| 26 | + >>> binary_to_gray(0) |
| 27 | + 0 |
| 28 | + >>> binary_to_gray(1) |
| 29 | + 1 |
| 30 | + >>> binary_to_gray(2) |
| 31 | + 3 |
| 32 | + >>> binary_to_gray(3) |
| 33 | + 2 |
| 34 | + >>> binary_to_gray(4) |
| 35 | + 6 |
| 36 | + >>> binary_to_gray(7) |
| 37 | + 4 |
| 38 | + >>> binary_to_gray(10) |
| 39 | + 15 |
| 40 | + >>> binary_to_gray(15) |
| 41 | + 8 |
| 42 | + >>> binary_to_gray(255) |
| 43 | + 128 |
| 44 | + >>> binary_to_gray(-1) |
| 45 | + Traceback (most recent call last): |
| 46 | + ... |
| 47 | + ValueError: Input must be a non-negative integer |
| 48 | + >>> binary_to_gray(3.5) |
| 49 | + Traceback (most recent call last): |
| 50 | + ... |
| 51 | + TypeError: Input must be an integer |
| 52 | + """ |
| 53 | + if not isinstance(binary_number, int): |
| 54 | + raise TypeError("Input must be an integer") |
| 55 | + if binary_number < 0: |
| 56 | + raise ValueError("Input must be a non-negative integer") |
| 57 | + |
| 58 | + return binary_number ^ (binary_number >> 1) |
| 59 | + |
| 60 | + |
| 61 | +def gray_to_binary(gray_number: int) -> int: |
| 62 | + """ |
| 63 | + Convert a Gray Code number to its binary equivalent. |
| 64 | +
|
| 65 | + The algorithm works by repeatedly XORing the gray code with itself right-shifted, |
| 66 | + until the right-shifted value becomes 0. |
| 67 | +
|
| 68 | + Args: |
| 69 | + gray_number: A positive integer representing a Gray Code number |
| 70 | +
|
| 71 | + Returns: |
| 72 | + The binary equivalent as an integer |
| 73 | +
|
| 74 | + Examples: |
| 75 | + >>> gray_to_binary(0) |
| 76 | + 0 |
| 77 | + >>> gray_to_binary(1) |
| 78 | + 1 |
| 79 | + >>> gray_to_binary(3) |
| 80 | + 2 |
| 81 | + >>> gray_to_binary(2) |
| 82 | + 3 |
| 83 | + >>> gray_to_binary(6) |
| 84 | + 4 |
| 85 | + >>> gray_to_binary(4) |
| 86 | + 7 |
| 87 | + >>> gray_to_binary(15) |
| 88 | + 10 |
| 89 | + >>> gray_to_binary(8) |
| 90 | + 15 |
| 91 | + >>> gray_to_binary(128) |
| 92 | + 255 |
| 93 | + >>> gray_to_binary(-1) |
| 94 | + Traceback (most recent call last): |
| 95 | + ... |
| 96 | + ValueError: Input must be a non-negative integer |
| 97 | + >>> gray_to_binary(5.5) |
| 98 | + Traceback (most recent call last): |
| 99 | + ... |
| 100 | + TypeError: Input must be an integer |
| 101 | + """ |
| 102 | + if not isinstance(gray_number, int): |
| 103 | + raise TypeError("Input must be an integer") |
| 104 | + if gray_number < 0: |
| 105 | + raise ValueError("Input must be a non-negative integer") |
| 106 | + |
| 107 | + binary_number = gray_number |
| 108 | + gray_number >>= 1 |
| 109 | + |
| 110 | + while gray_number: |
| 111 | + binary_number ^= gray_number |
| 112 | + gray_number >>= 1 |
| 113 | + |
| 114 | + return binary_number |
| 115 | + |
| 116 | + |
| 117 | +def decimal_to_gray(decimal_number: int) -> str: |
| 118 | + """ |
| 119 | + Convert a decimal number to its Gray Code representation as a binary string. |
| 120 | +
|
| 121 | + Args: |
| 122 | + decimal_number: A positive integer in decimal |
| 123 | +
|
| 124 | + Returns: |
| 125 | + Gray Code representation as a binary string |
| 126 | +
|
| 127 | + Examples: |
| 128 | + >>> decimal_to_gray(0) |
| 129 | + '0' |
| 130 | + >>> decimal_to_gray(1) |
| 131 | + '1' |
| 132 | + >>> decimal_to_gray(2) |
| 133 | + '11' |
| 134 | + >>> decimal_to_gray(3) |
| 135 | + '10' |
| 136 | + >>> decimal_to_gray(4) |
| 137 | + '110' |
| 138 | + >>> decimal_to_gray(10) |
| 139 | + '1111' |
| 140 | + >>> decimal_to_gray(15) |
| 141 | + '1000' |
| 142 | + >>> decimal_to_gray(-1) |
| 143 | + Traceback (most recent call last): |
| 144 | + ... |
| 145 | + ValueError: Input must be a non-negative integer |
| 146 | + """ |
| 147 | + if not isinstance(decimal_number, int): |
| 148 | + raise TypeError("Input must be an integer") |
| 149 | + if decimal_number < 0: |
| 150 | + raise ValueError("Input must be a non-negative integer") |
| 151 | + |
| 152 | + gray_code = binary_to_gray(decimal_number) |
| 153 | + return bin(gray_code)[2:] # Remove '0b' prefix |
| 154 | + |
| 155 | + |
| 156 | +def gray_to_decimal(gray_string: str) -> int: |
| 157 | + """ |
| 158 | + Convert a Gray Code binary string to its decimal equivalent. |
| 159 | +
|
| 160 | + Args: |
| 161 | + gray_string: A string of 0s and 1s representing Gray Code |
| 162 | +
|
| 163 | + Returns: |
| 164 | + The decimal equivalent as an integer |
| 165 | +
|
| 166 | + Examples: |
| 167 | + >>> gray_to_decimal('0') |
| 168 | + 0 |
| 169 | + >>> gray_to_decimal('1') |
| 170 | + 1 |
| 171 | + >>> gray_to_decimal('11') |
| 172 | + 2 |
| 173 | + >>> gray_to_decimal('10') |
| 174 | + 3 |
| 175 | + >>> gray_to_decimal('110') |
| 176 | + 4 |
| 177 | + >>> gray_to_decimal('1111') |
| 178 | + 10 |
| 179 | + >>> gray_to_decimal('1000') |
| 180 | + 15 |
| 181 | + >>> gray_to_decimal('invalid') |
| 182 | + Traceback (most recent call last): |
| 183 | + ... |
| 184 | + ValueError: Invalid binary string |
| 185 | + >>> gray_to_decimal('') |
| 186 | + Traceback (most recent call last): |
| 187 | + ... |
| 188 | + ValueError: Input string cannot be empty |
| 189 | + """ |
| 190 | + if not gray_string: |
| 191 | + raise ValueError("Input string cannot be empty") |
| 192 | + |
| 193 | + # Validate binary string |
| 194 | + if not all(bit in "01" for bit in gray_string): |
| 195 | + raise ValueError("Invalid binary string") |
| 196 | + |
| 197 | + gray_number = int(gray_string, 2) |
| 198 | + return gray_to_binary(gray_number) |
| 199 | + |
| 200 | + |
| 201 | +if __name__ == "__main__": |
| 202 | + import doctest |
| 203 | + |
| 204 | + doctest.testmod() |
| 205 | + |
| 206 | + # Interactive demonstration |
| 207 | + print("=== Binary to Gray Code Converter ===\n") |
| 208 | + |
| 209 | + # Demonstrate conversions for 0-15 |
| 210 | + print("Decimal | Binary | Gray Code") |
| 211 | + print("--------|----------|----------") |
| 212 | + for i in range(16): |
| 213 | + binary = bin(i)[2:].zfill(4) |
| 214 | + gray = decimal_to_gray(i).zfill(4) |
| 215 | + print(f"{i:7} | {binary:8} | {gray:9}") |
| 216 | + |
| 217 | + print("\n=== Verification: Gray to Binary ===\n") |
| 218 | + print("Gray Code | Binary | Decimal") |
| 219 | + print("----------|----------|--------") |
| 220 | + for i in range(16): |
| 221 | + gray = decimal_to_gray(i).zfill(4) |
| 222 | + decimal = gray_to_decimal(gray) |
| 223 | + binary = bin(decimal)[2:].zfill(4) |
| 224 | + print(f"{gray:9} | {binary:8} | {decimal:7}") |
0 commit comments