Update
This commit is contained in:
parent
25181f2ca3
commit
d765507e6a
6 changed files with 29 additions and 5 deletions
11
binary.md
11
binary.md
|
@ -21,6 +21,17 @@ At the basic level binary works just like the [decimal](decimal.md) (base 10) sy
|
|||
|
||||
**Conversion to decimal**: let's see an example that utilizes the facts mentioned above. Let's have a number that's written as 10135 in decimal. The first digit from the right (5) says the number of 10^(0)s (1s) in the number, the second digit (3) says the number of 10^(1)s (10s), the third digit (1) says the number of 10^(2)s (100s) etc. Similarly if we now have a number **100101** in binary, the first digit from the right (1) says the number of 2^(0)s (1s), the second digit (0) says the number of 2^(1)s (2s), the third digit (1) says the number of 2^(2)s (4s) etc. Therefore this binary number can be converted to decimal by simply computing 1 * 2^0 + 0 * 2^1 + 1 * 2^2 + 0 * 2^3 + 0 * 2^4 + 1 * 2^5 = 1 + 4 + 32 = **37**.
|
||||
|
||||
```
|
||||
100101 = 1 + 4 + 32 = 37
|
||||
||||||
|
||||
\\\\\\__number of 2^0s (= 1s)
|
||||
\\\\\__number of 2^1s (= 2s)
|
||||
\\\\__number of 2^2s (= 4s)
|
||||
\\\__number of 2^3s (= 8s)
|
||||
\\__number of 2^4s (= 16s)
|
||||
\__number of 2^5s (= 32s)
|
||||
```
|
||||
|
||||
To **convert from decimal** to binary we can use a simple [algorithm](algorithm.md) that's again derived from the above. Let's say we have a number *X* we want to write in binary. We will write digits from right to left. The first (rightmost) digit is the remainder after integer division of *X* by 2. Then we divide the number by 2. The second digit is again the remainder after division by 2. Then we divide the number by 2 again. This continues until the number is 0. For example let's convert the number 22 to binary: first digit = 22 % 2 = **0**; 22 / 2 = 11, second digit = 11 % 2 = **1**; 11 / 2 = 5; third digit = 5 % 2 = **1**; 5 / 2 = 2; 2 % 2 = **0**; 2 / 2 = 1; 1 % 2 = **1**; 1 / 2 = 0. The result is **10110**.
|
||||
|
||||
TODO: operations in binary
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue