-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary_to_decimal.py
138 lines (120 loc) · 2.03 KB
/
Binary_to_decimal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#WAP a program to convert Binary to integers
N= int(input())
ans =0
last_digit= 0
pow_of_two = 1
while N>0:
last_digit = N % 10
ans+= last_digit*pow_of_two
pow_of_two*=2
n//=2
print(and)
"""
Here the logic used is that if a binary is given then for every digit in the binary we will assign it placeholder with the power of 2, where the zeros place one will have the power of two as 1, tenths place will have 2, hundreds will have 4 and so on and we will first multiply the placeholder digits with the
power of 2 assigned to the same placeholder and lastly, we will add all of their output to find the result as over binary conversion to integer.
"""
"""
Refined Explanation of the Logic
When converting a binary number to its integer equivalent, each digit in the binary number represents a power of 2, based on its position (or placeholder). Here's how it works:
Placeholder and Power of 2:
Each digit in the binary number corresponds to a placeholder, starting from the rightmost digit (which is the least significant bit).
The rightmost digit (ones place) has a placeholder value of
2
0
=
1
2
0
=1.
The next digit to the left (tens place) has a placeholder value of
2
1
=
2
2
1
=2.
The next digit (hundreds place) has a placeholder value of
2
2
=
4
2
2
=4.
This pattern continues, with each subsequent placeholder being a power of 2 that increases by 1 as you move left.
Multiply and Sum:
For each digit in the binary number, multiply the digit (either 0 or 1) by its corresponding placeholder value (power of 2).
Add up all the results of these multiplications to get the final integer value.
Example
Let’s take the binary number 1011:
Break it down digit by digit, starting from the right:
Rightmost digit (1):
1
×
2
0
=
1
×
1
=
1
1×2
0
=1×1=1
Next digit (1):
1
×
2
1
=
1
×
2
=
2
1×2
1
=1×2=2
Next digit (0):
0
×
2
2
=
0
×
4
=
0
0×2
2
=0×4=0
Leftmost digit (1):
1
×
2
3
=
1
×
8
=
8
1×2
3
=1×8=8
Add up the results:
1
+
2
+
0
+
8
=
11
1+2+0+8=11
So, the binary number 1011 is equivalent to the integer 11.
"""