|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "213ad744", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Number systems\n", |
| 9 | + "Number systems use symbols to represent numbers in a compact way.\n", |
| 10 | + "\n", |
| 11 | + "[Additive number systems](#Additive-number-systems) \n", |
| 12 | + "[Exponents](#Exponents) \n", |
| 13 | + "[Positional number systems](#Positional-number-systems) \n", |
| 14 | + "[Binary numbers](#Binary-numbers) \n", |
| 15 | + "[Operations on binary numbers](#Operations-on-binary-numbers) \n", |
| 16 | + "[Boolean logic](#Boolean-logic)\n", |
| 17 | + "\n", |
| 18 | + "\n", |
| 19 | + "<a id=\"Additive-number-systems\"></a>\n", |
| 20 | + "## Additive number systems\n", |
| 21 | + "\n", |
| 22 | + "The original way of counting, once the obvious limits of counting on fingers was reached, was using *tally marks* or notches on a stick, stone, or piece of clay as symbols each representing a single thing.\n", |
| 23 | + "These were added to together to create a *number*, which symbolizes a quantity of things.\n", |
| 24 | + "Though this was less limited than using fingers, these marks likewise reached a limit as numbers increased.\n", |
| 25 | + "\n", |
| 26 | + "The Roman approach to this was to use symbols or *numerals* to count groups, then these numerals were added together to get numbers.\n", |
| 27 | + "\n", |
| 28 | + "|Number |1 |2 |3 |4 |5 |9 |10 |50 |100 |500 |1000\n", |
| 29 | + "|:- |:- |:- |:- |:- |:- |:- |:- |:- |:- |:- |:-\n", |
| 30 | + "|Roman |I |II |III |IV |V |IX |X |L |C |D |M\n", |
| 31 | + "\n", |
| 32 | + "Larger numbers could be represented using fewer symbols. \n", |
| 33 | + "This is an example of adding these numberals together.\n", |
| 34 | + "\n", |
| 35 | + "DCCXIV is 500 + 200 + 10 + 4 or 714.\n", |
| 36 | + "\n", |
| 37 | + "Very large numbers still need quite a few symbols. \n", |
| 38 | + "Adding numbers is straightforward, but becomes cumbersome.\n", |
| 39 | + "Substracting numbers is a little harder, and multiplying and dividing numbers is harder yet.\n", |
| 40 | + "\n", |
| 41 | + "In India, a new number system, the *Hindu* system, was created that used fewer symbols but could represent large numbers in a more compact way. Some background is needed to discuss this system.\n", |
| 42 | + "\n", |
| 43 | + "<a id=\"Positional-number-systems\"></a>\n", |
| 44 | + "## Positional number systems\n", |
| 45 | + "\n", |
| 46 | + "Positional number systems use fewer numerals than additive number systems, \n", |
| 47 | + "but the numerals have different values depending on their *position* in a number.\n", |
| 48 | + "The Hindu number system uses the numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. \n", |
| 49 | + "We inherited the number system from the Arabic cultures, and the number system uses Hindu-Arabic numerals or what we call Arabic numerals.\n", |
| 50 | + "The number system is called *decimal* because it uses 10 different numerals, each of which we call *digits*.\n", |
| 51 | + "We use [*exponents*](./Arithmetic.ipynb#Exponents) to show the different powers of 10 multiplied by each digit in different positions, so \n", |
| 52 | + "\n", |
| 53 | + "$123 = 100 + 20 + 3 = 1 \\cdot 100 + 2 \\cdot 10 + 3 \\cdot 1 = 1 \\cdot 10^2 + 2 \\cdot 10^1 + 3 \\cdot 10^0$,\n", |
| 54 | + "\n", |
| 55 | + "|position |3 |2 |1\n", |
| 56 | + "|:-|:-|:-|:-\n", |
| 57 | + "|digit |1 | 2 | 3\n", |
| 58 | + "|multiplier |100 |10 |1\n", |
| 59 | + "|using exponents |$10^2$ |$10^1$ |$10^0$ \n", |
| 60 | + "|value |100 |20 |3\n", |
| 61 | + "\n", |
| 62 | + "To decide what digit goes in a position that has no value, the Hindu system had to invent the idea of a zero, the thing that is nothing and if multiplied by any number gives nothing. \n", |
| 63 | + "The concept of zero was new because it was not used in additive systems. \n", |
| 64 | + "As an example, the number 305 needs zero in the second position to work, so that we have \n", |
| 65 | + "\n", |
| 66 | + "3 x 100 + 0 x 10 + 5 x 1 = 300 + 0 + 5 = 305\n", |
| 67 | + "\n", |
| 68 | + "digit |3 |0 |5\n", |
| 69 | + "|:- |:- |:- |:-\n", |
| 70 | + "position |3 |2 |1\n", |
| 71 | + "multiplier |100 |10 |1\n", |
| 72 | + "value |3 x 100 = 300 |0 x 10 = 0 |5 x 1 = 5\n", |
| 73 | + "\n", |
| 74 | + "In the decimal system, adding numbers is simplified, you add each position separately.\n", |
| 75 | + "There are fewer numerals than in the Roman system, so addition is faster.\n", |
| 76 | + "For 305 + 262 = 567\n", |
| 77 | + "\n", |
| 78 | + "position |1 |2 |3\n", |
| 79 | + "|:- |:- |:- |:-\n", |
| 80 | + "| |3 |0 |5\n", |
| 81 | + "|+ |2 |6 |2\n", |
| 82 | + "| |--- |--- |---\n", |
| 83 | + "| |5 |6 |7\n", |
| 84 | + "\n", |
| 85 | + "When adding numbers in a position gives more than 9, we introduce the idea of a *carry*.\n", |
| 86 | + "For 7 + 35 = 42\n", |
| 87 | + "\n", |
| 88 | + "|position |1 |2\n", |
| 89 | + "|:- |:- |:- \n", |
| 90 | + "|carry |1 |\n", |
| 91 | + "| | |7\n", |
| 92 | + "|+ |3 |5\n", |
| 93 | + "| |--- |--- |---\n", |
| 94 | + "| |4 |2\n", |
| 95 | + "\n", |
| 96 | + "Subtraction, multiplication, and division are similarly simplified.\n", |
| 97 | + "\n", |
| 98 | + "<a id=\"Binary-numbers\"></a>\n", |
| 99 | + "## Binary numbers\n", |
| 100 | + "In the decimal system, we use a base of 10 for the exponents at each numeral position and the digits 0 through 9.\n", |
| 101 | + "There are as many different digits as the number base.\n", |
| 102 | + "There is no reason why we can't use another number for the base, such as 2.\n", |
| 103 | + "We call a base 2 number system a *binary* system.\n", |
| 104 | + "There are two numerals in a binary system, 0 and 1.\n", |
| 105 | + "We can give the value for a binary number 10110, which we show as $10110_2$ to indicate it is base 2, as\n", |
| 106 | + "\n", |
| 107 | + "digit |1 |0 |1 |1 |0\n", |
| 108 | + ":- |:- |:- |:- |:- |:-\n", |
| 109 | + "position |5 |4 |3 |2 |1\n", |
| 110 | + "multiplier |16 |8 |4 |2 |1\n", |
| 111 | + "using exponents |$2^4$ |$2^3$ |$2^2$ |$2^1$ |$2^0$\n", |
| 112 | + "position value |16 |0 |4 |2 |0\n", |
| 113 | + "\n", |
| 114 | + "so the final number is 16 + 0 + 4 + 2 + 0 = 22. \n", |
| 115 | + "We can show decimal 22 as $22_{10}$ to show the number base.\n", |
| 116 | + "\n", |
| 117 | + "We call each binary numeral a *binary digit* or *bit*.\n", |
| 118 | + "Computers are able to represent two values, 0 and 1, well. \n", |
| 119 | + "In electronic circuits 0 corresponds to \"no power\" or \"no voltage\" and 1 corresponds to \"positive voltage\".\n", |
| 120 | + "It turns out trying to electrically detect 10 voltage levels for 10 different decimal digits is unreliable.\n", |
| 121 | + "\n", |
| 122 | + "We can add numbers like we do in the decimal system. We add each position separately.\n", |
| 123 | + "\n", |
| 124 | + "$101_2 + 10_2 = 111_2$\n", |
| 125 | + "\n", |
| 126 | + "position |1 |2 |3\n", |
| 127 | + "|:- |:- |:- |:-\n", |
| 128 | + "| |1 |0 |1\n", |
| 129 | + "|+ | |1 |0\n", |
| 130 | + "| |--- |--- |---\n", |
| 131 | + "| |1 |1 |1\n", |
| 132 | + "\n", |
| 133 | + "When adding numbers in a position gives more than 1, we introduce the idea of a *carry*.\n", |
| 134 | + "\n", |
| 135 | + "$101_2 + 1_2 = 110_2$\n", |
| 136 | + "\n", |
| 137 | + "|position |1 |2 |3\n", |
| 138 | + "|:- |:- |:- |:- \n", |
| 139 | + "|carry | |1 |\n", |
| 140 | + "| | 1| 0| 1\n", |
| 141 | + "|+ | | |1\n", |
| 142 | + "| |--- |--- |---\n", |
| 143 | + "| |1 |1 | 0\n", |
| 144 | + "\n", |
| 145 | + "Subtraction, multiplication, and division are similarly simplified.\n", |
| 146 | + "\n", |
| 147 | + "Using binary numbers can also get tedious.\n", |
| 148 | + "A shorthand for binary numbers is using base 16 numbers, or *hexadecimal* numbers.\n", |
| 149 | + "Hexadecimal numbers use 16 numerals.\n", |
| 150 | + "We can use decimal digits for the first 10, 0 through 9, but we need different numberals for the last 6.\n", |
| 151 | + "We use the six letters A through F for them.\n", |
| 152 | + "We typically put '0x' in from on the number to show it is hexademical.\n", |
| 153 | + "\n", |
| 154 | + "|decimal |binary |hexadecimal\n", |
| 155 | + "|:-|:-|:- \n", |
| 156 | + "|0 |$0_2$ |0x0|\n", |
| 157 | + "|1 |$1_2$ |0x1 |\n", |
| 158 | + "|2 |$10_2$ |0x2 |\n", |
| 159 | + "|3 |$11_2$ |0x3 |\n", |
| 160 | + "|4 |$100_2$ |0x4 |\n", |
| 161 | + "|5 |$101_2$ |0x5 |\n", |
| 162 | + "|6 |$110_2$ |0x6 |\n", |
| 163 | + "|7 |$111_2$ |0x7 |\n", |
| 164 | + "|8 |$1000_2$ |0x8 |\n", |
| 165 | + "|9 |$1001_2$ |0x9 |\n", |
| 166 | + "|10 |$1010_2$ |0xA |\n", |
| 167 | + "|11 |$1011_2$ |0xB |\n", |
| 168 | + "|12 |$1100_2$ |0xC |\n", |
| 169 | + "|13 |$1101_2$ |0xD |\n", |
| 170 | + "|14 |$1110_2$ |0xE |\n", |
| 171 | + "|15 |$1111_2$ |0xF |\n", |
| 172 | + "\n", |
| 173 | + "In computers, only a fixed number of bits are allocated for numbers.\n", |
| 174 | + "This limits the size of number that can be stored.\n", |
| 175 | + "Typically sizes for numbers are 8, 16, 32, or 64 bits.\n", |
| 176 | + "The term for an 8 bit number is a *byte*.\n", |
| 177 | + "We can find the maximum size for a number from the value of a number with all 1 bits.\n", |
| 178 | + "The largest number that can be stored in 8 bits is\n", |
| 179 | + "\n", |
| 180 | + "$11111111_2 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255$\n", |
| 181 | + "\n", |
| 182 | + "This value is $2^8 - 1$. The names and maximum values for different types of numbers of different sizes are given by\n", |
| 183 | + "\n", |
| 184 | + "|size |bits |bytes |maximum value\n", |
| 185 | + "|:- |:- |:- |:- \n", |
| 186 | + "|byte |8 |1 |$2^8 - 1$ |\n", |
| 187 | + "|short |16 |1 |$2^{16} - 1$ |\n", |
| 188 | + "|int |32 |1 |$2^{32} - 1$ |\n", |
| 189 | + "|long |64 |1 |$2^{64} - 1$ |\n", |
| 190 | + "\n", |
| 191 | + "Numbers being limited by the number of bits requires that programs must anticipate the largest possible value and use a size large enough to store it.\n", |
| 192 | + "When a number too large for the number of bits is stored, we say an *overflow* occurs.\n", |
| 193 | + "In that case, the bits that cannot be stored are, for example, thrown away and the value is incorrect.\n", |
| 194 | + "\n", |
| 195 | + "<a id=\"Operations-on-binary-numbers\"></a>\n", |
| 196 | + "## Operations on binary numbers\n", |
| 197 | + "Adding binary numbers is done like in the decimal system.\n", |
| 198 | + "To add the binary numbers, we get\n", |
| 199 | + "\n", |
| 200 | + "$101_2 + 1_2 = 110_2$.\n", |
| 201 | + "\n", |
| 202 | + "position |1 |2 |3 |4\n", |
| 203 | + ":- |:- |:- |:- |:- \n", |
| 204 | + "|carry | | |1 | | |\n", |
| 205 | + "| | |1 |0 |1 |\n", |
| 206 | + "|+ | | | |1\n", |
| 207 | + "| |--- |--- |--- |---\n", |
| 208 | + "| | |1 |1 |0\n", |
| 209 | + "\n", |
| 210 | + "Subtraction, multiplication, and division are similarly done as with decimal numbers. \n", |
| 211 | + "\n", |
| 212 | + "<a id=\"Boolean-logic\"></a>\n", |
| 213 | + "## Boolean logic\n", |
| 214 | + "The binary numerals 1 and 0 can conveniently represent truth values, true and false.\n", |
| 215 | + "A logic based on these values, *Boolean logic* (invented by mathematician George Boole),\n", |
| 216 | + "gives new operations on binary numbers.\n", |
| 217 | + "The basic operations are *AND*, *OR*, *NOT*, and *XOR*.\n", |
| 218 | + "\n", |
| 219 | + "|operation | description |example\n", |
| 220 | + "|:- |:- |:-\n", |
| 221 | + "|AND |result is true if each operand is true|1 AND 1 = true\n", |
| 222 | + "|OR |result is true if either operand is true|0 AND 1 = true\n", |
| 223 | + "|NOT |result is the opposite of the operand |NOT 1 = false\n", |
| 224 | + "|XOR |result is false if both operands are the same, and true if both are different|1 AND 1 = false\n", |
| 225 | + "\n", |
| 226 | + "The results of these operations can be shown in *truth tables*.\n", |
| 227 | + "This is the truth table for AND.\n", |
| 228 | + "\n", |
| 229 | + "|AND |1 |0\n", |
| 230 | + "|:-|:- |:- \n", |
| 231 | + "|1 |1 | 0\n", |
| 232 | + "|0 |0 | 0\n", |
| 233 | + "\n", |
| 234 | + "This is the truth table for OR.\n", |
| 235 | + "\n", |
| 236 | + "|OR |1 |0\n", |
| 237 | + "|:-|:- |:- \n", |
| 238 | + "|1 |1 | 1\n", |
| 239 | + "|0 |1 | 0\n", |
| 240 | + "\n", |
| 241 | + "This is the truth table for NOT.\n", |
| 242 | + "\n", |
| 243 | + "|NOT | |\n", |
| 244 | + "|:-|:- |\n", |
| 245 | + "|1 |0 |\n", |
| 246 | + "|0 |1 |\n", |
| 247 | + "\n", |
| 248 | + "This is the truth table for XOR.\n", |
| 249 | + "\n", |
| 250 | + "|XOR |1 |0\n", |
| 251 | + "|:-|:- |:- \n", |
| 252 | + "|1 |0 | 1\n", |
| 253 | + "|0 |1 | 0\n", |
| 254 | + "\n", |
| 255 | + "These operations are used in operations on binary numbers in programs." |
| 256 | + ] |
| 257 | + }, |
| 258 | + { |
| 259 | + "cell_type": "code", |
| 260 | + "execution_count": null, |
| 261 | + "id": "43d3987e", |
| 262 | + "metadata": {}, |
| 263 | + "outputs": [], |
| 264 | + "source": [] |
| 265 | + } |
| 266 | + ], |
| 267 | + "metadata": { |
| 268 | + "kernelspec": { |
| 269 | + "display_name": "Python 3 (ipykernel)", |
| 270 | + "language": "python", |
| 271 | + "name": "python3" |
| 272 | + }, |
| 273 | + "language_info": { |
| 274 | + "codemirror_mode": { |
| 275 | + "name": "ipython", |
| 276 | + "version": 3 |
| 277 | + }, |
| 278 | + "file_extension": ".py", |
| 279 | + "mimetype": "text/x-python", |
| 280 | + "name": "python", |
| 281 | + "nbconvert_exporter": "python", |
| 282 | + "pygments_lexer": "ipython3", |
| 283 | + "version": "3.11.5" |
| 284 | + } |
| 285 | + }, |
| 286 | + "nbformat": 4, |
| 287 | + "nbformat_minor": 5 |
| 288 | +} |
0 commit comments