Skip to content

Commit 072fb80

Browse files
authored
GrayCodetoBinary.py
python code for converting gray code to binary
1 parent 93e97d7 commit 072fb80

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

GrayCodetoBinary.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def flip_num(my_nu):
2+
return '1' if(my_nu == '0') else '0';
3+
4+
def gray_to_binary(gray):
5+
binary_code=""
6+
binary_code += gray[0]
7+
for i in range(1,len(gray)):
8+
9+
if (gray[i]=='0'):
10+
binary_code += binary_code[i-1]
11+
else:
12+
binary_code += flip_num(binary_code[i-1])
13+
14+
return binary_code
15+
16+
# gray_code="01101001"
17+
18+
19+
gray_code=input("please enter the gray code\n")
20+
print("the gray code is : ")
21+
print(gray_code)
22+
# x=gray_to_binary(gray_code)
23+
print("binary code of", gray_code, "is",gray_to_binary(gray_code))
24+
25+
# for converting binary numb to decimal
26+
value=0
27+
b_num=list(gray_to_binary(gray_code))
28+
29+
for i in range(len(b_num)):
30+
digit=b_num.pop()
31+
if digit =='1':
32+
value = value + pow(2,i)
33+
34+
print("the decimal value of the number is ", value)
35+
36+
37+
# print(12//5)

0 commit comments

Comments
 (0)