File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 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 } " )
You can’t perform that action at this time.
0 commit comments