Skip to content

Commit d3fd785

Browse files
authored
Create bianry_to_excessthree.py
1 parent e2a78d4 commit d3fd785

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def binary_to_excess3(binary_str: str) -> str:
2+
"""
3+
Convert a binary number (as a string) to its Excess-3 code.
4+
5+
Args:
6+
binary_str (str): Binary number as a string (e.g., "1010").
7+
8+
Returns:
9+
str: Excess-3 code as a binary string.
10+
11+
Example:
12+
>>> binary_to_excess3("1010")
13+
'1101'
14+
"""
15+
# Convert binary to decimal
16+
decimal_value = int(binary_str, 2)
17+
18+
# Add 3 (Excess-3 encoding)
19+
excess3_value = decimal_value + 3
20+
21+
# Convert back to 4-bit binary
22+
excess3_binary = format(excess3_value, '04b')
23+
24+
return excess3_binary
25+
26+
27+
if __name__ == "__main__":
28+
binary_input = input("Enter a 4-bit binary number: ")
29+
excess3_output = binary_to_excess3(binary_input)
30+
print(f"Excess-3 code of {binary_input} is: {excess3_output}")

0 commit comments

Comments
 (0)