-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarynumber.cpp
109 lines (79 loc) · 3.78 KB
/
binarynumber.cpp
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
/*Lecture 5.1: Binary Number System */
/*
1. Decimal Number System (base(10))
1000 100 10 1
_________________________________________
| 1 | 2 | 3| 4 |
|_________________________________________|
10^3 10^2 10^1 10^0
Decimal number system me hamara base hota hai 10
Agar humko 1234 ko represent karna hai 10 base pe too hum aisee karege,
1234= 1*1000+2*100+3*10+4*1
1234= 1*10^3 + 2*10^2+ 3*10^1+ 4*10^0
Aisee hum decimal number system me kisi bhi number ko 10 ke power me represent kar sakte hai
2. Binary Number System (base(2))
<-- 32 16 8 4 2 1
___________________________________________________________
| 1 | 0 | 1 | 1 | 0 | 1 |
|_________________________________________|________________|
2^5 2^4 2^3 2^2 2^1 2^0
45= 1*32+ 0*16+ 1*8+ 1*4+0*2+1*1
32+8+4+1=45
45= 1*2^5+ 0*2^4+ 1*2^3+ 1*2^2 +0*2^1 +1*2^0
so,
45 in binary is= 101101
3. Convert decimal to Binary Number
Example: 1
1234(10)
______________________________________________
| N | Quotient(N/10) | Remainder(N%10) |
|______________________________________________|
______________________________________________
| 1234 | 123 | 4 |
|______________________________________________|
______________________________________________
| 123 | 12 | 3 |
|______________________________________________|
______________________________________________
| 12 | 1 | 2 |
|______________________________________________|
______________________________________________
| 1 | 0 | 1 |
|______________________________________________|
whenever the quotient will be zero i will stop and see the remainder section in reverse order,
1234
Example: 2
45(2)
______________________________________________
| N | Quotient(N/2) | Remainder(N%2) |
|______________________________________________|
______________________________________________
| 45 | 22 | 1 |
|______________________________________________|
______________________________________________
| 22 | 11 | 0 |
|______________________________________________|
______________________________________________
| 11 | 5 | 1 |
|______________________________________________|
______________________________________________
| 5 | 2 | 1 |
|______________________________________________|
______________________________________________
| 2 | 1 | 0 |
|______________________________________________|
______________________________________________
| 1 | 0 | 1 |
|______________________________________________|
Reverse Order:= 45(2)--> 101101
4. Convert Binary to decimal
<-- 32 16 8 4 2 1
___________________________________________________________
| 1 | 0 | 1 | 1 | 0 | 1 |
|_________________________________________|________________|
2^5 2^4 2^3 2^2 2^1 2^0
1*2^5 + 0*2^4 + 1*2^3+ 1*2^2+ 0*2^! + 1*2^0
32+8+4+2= 45
(45)
10
*/