------------------------------------------- Basic Arithmetic Operations and Terminology ------------------------------------------- -Ian! D. Allen - idallen@idallen.ca - www.idallen.com Arithmetic terminology augend + addend = sum minuend - subtrahend = difference multiplicand * multiplier = product dividend / divisor = quotient (+ remainder) Arithmetic Operator Precedence Remember BEDMAS ? See: http://en.wikipedia.org/wiki/Order_of_operations Arithmetic with numbers with exponents Notation: 2**10 or 2^10 means "2 to the power 10" Notation: 10**3 or 10^3 means "10 to the power 3" Notation: 2**-10 or 2^-10 means "2 to the power -10", or "1 / (2**10)" Exponents have high precedence, so read 1/2**9 as 1/(2**9). Note that unary minus -2**2 usually means -(2**2) not (-2)**2. 2**-9 = 1 / (2**9) [negative exponent means reciprocal] 2**10 * 2**15 = 2**25 [multiplication means add the exponents] 2**15 / 2**10 = 2**15 * 2**-10 = 2**(15-10) = 2**5 [divide means subtract] 2**10 / 2**12 = 2**10 * 2**-12 = 2**-2 = 1 / (2**2) = 1/4 = 0.25 (2**3)**4 = 2**3 * 2**3 * 2**3 * 2**3 = 2**12 [multiply the exponents] If you know your powers-of-two table (up to 2**16 or 65,536), then it's easy to rewrite other powers-of-two expressions to know their value. For example: What is the decimal value of 8 * 16**3 ? 8 * 16**3 = 2**3 * (2**4)**3 [rewrite everything as powers of two] = 2**3 * 2**12 [multiply two rightmost exponents] = 2**15 [add exponents] = 32,768 [a famous number in computer science] New and old prefixes: http://en.wikipedia.org/wiki/Binary_prefix SI prefixes (e.g. 5 kilometres, 100 megahertz): 10**3 = 1,000 = Kilo 10**6 = 1,000,000 = Mega 10**9 = 1,000,000,000 = Giga 10**12 = 1,000,000,000,000 = Tera 10**15 = 1,000,000,000,000,000 = Peta (there are more, but in this course we stop at Peta) There are 1,000,000 Mega in a Tera: 10**12 / 10**6 = 10**6 - therefore a Tera is a Mega times a Mega or (10**6)**2 10**-3 = 1/1,000 = Milli 10**-6 = 1/1,000,000 = Micro 10**-9 = 1/1,000,000,000 = Nano 10**-12 = 1/1,000,000,000,000 = Pico (there are more, but in this course we stop at Pico) There are 1,000,000 Pico in a Micro: 10**-6 / 10**-12 = 10**6 - therefore a Pico is a Micro times a Micro or (10**-6)**2 Unfortunately, the SI prefixes and abbreviations have also been used historically in a confusing way to indicate *binary* multipliers, e.g. 8 GB of memory should really be written as 8 GiB not 8 GB. Eventually, someone decided to stop confusing the two, and new "binary only" prefixes were invented, but they are not widely used yet: New binary prefixes (e.g. 8 GiB RAM is more correct than 8 GB RAM): 2**10 = 1,024 = Kibi (Kilo-Binary) 2**20 = 2**10 * 2**10 = Kibi * Kibi = 1,048,576 = Mebi (Mega-Binary) 2**30 = 2**10 * 2**20 = Kibi * Mebi = 1,073,741,824 = Gibi (Giga-Binary) 2**40 = 2**10 * 2**30 = Kibi * Gibi = 1,099,511,627,776 = Tebi (Tera-Binary) Computer memory is always measured using binary powers of two, even though history has us writing the measurement using SI power-of-ten. An 8 GB memory module is actually 8 GiB (power-of-two), and you should really write 8 GiB not 8 GB (but hardly anyone does). Therefore: 8 GiB of memory is 8 * 1,073,741,824 not 8 * 1,000,000,000. File sizes may be measured using either method; many operating systems show file sizes using power-of-two units incorrectly labelled with SI abbreviations. A 175 KB file is really 175 KiB, which is 175 * 1024 = 179,200 bytes not 175,000 bytes! Disk drive manufacturers always use SI power-of-ten units, but since operating systems often use binary units you may find that your 2 TB (power-of-ten) disk drive displays as only 1.8 TiB (power-of-two) when you plug it in. Confusingly, your computer will incorrectly display the disk size using the historic SI abbreviation 1.8 TB instead of the correct binary 1.8 TiB. Everything else uses standard SI prefixes. A 32-bit word can address 4 GiB of memory because 2**32 = 2**2 * 2**30 = 4 Gibi [because 2**30 -> Gibi] A 2 terabyte disk drive (or file) may appear to be only 1.8 Ti bytes when displayed by an operating system that is using the larger binary prefixes instead of the SI (power of ten) prefixes, because 2 TB = 2 * 10**12 = 2,000,000,000,000 but 1.8189894 TiB = 1.8189894 * 2**40 = approximately 2,000,000,000,000 Writing Numbers in Different Bases (Radix) Writing numbers that represent bit patterns in computer memory is simpler if we use number bases that are powers of two, typically base 2 (binary), base 8 (octal), or base 16 (hexadecimal). In typeset text, subscripts are used to indicate a number in a different base. It looks something like this, only the subscript is in a smaller font and appears on the same line as the number: 1011010 1011010 1011010 1011010 2 8 10 16 The above are numbers in base 2, base 8, base 10, and base 16. In text files (what you are reading now), subscripts aren't possible, so we write the base of the number in parentheses after the number: 1011010(2) is a base 2 (binary) number 1011010(8) is a base 8 (octal) number 1011010(10) is a base 10 (decimal) number 1011010(16) is a base 16 (hexadecimal) number We can write: 25(10) = 31(8) which means decimal 25 equals octal 31. For bases larger than 10, we don't have enough number symbols to represent the values of each digit. For base 10 we need symbols that represent values from zero to 9, and we have the digits 0 through 9 to do that. For hexadecimal we need symbols that represent values from zero to 15, so we use letters for the values above 9: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15 The hexadecimal number CA(16) means "C times 16**1 plus A times 16**0" or "12 times 16 plus 10 times 1" which has decimal value 202(10). Floating-point numbers, e.g. 123.456, may also be in any base, e.g. 1.1(2) [binary], 1.1(8) [octal], 1.1(10) [decimal], 1.1(16) [hexadecimal]. The term "decimal point" is, strictly speaking, usable only for base-10 (decimal) floating-point numbers. The general term for "decimal point" is "radix point" when the numbers are not decimal numbers. Decimal, Binary, and fractions in other Bases Fractions are negative powers of the base: 10.0000 decimal is 1 * 10**1 or 10 decimal 1.0000 decimal is 1 * 10**0 or 1 decimal 0.1000 decimal is 1 * 10**-1 or 1/10 or 0.1 decimal 0.0100 decimal is 1 * 10**-2 or 1/100 or 0.01 decimal 0.0010 decimal is 1 * 10**-3 or 1/1000 or 0.001 decimal 0.0001 decimal is 1 * 10**-4 or 1/10000 or 0.0001 decimal ...etc... 10.0000 binary is 1 * 2**1 or 2 decimal 1.0000 binary is 1 * 2**0 or 1 decimal 0.1000 binary is 1 * 2**-1 or 1/2 or 0.5 decimal 0.0100 binary is 1 * 2**-2 or 1/4 or 0.25 decimal 0.0010 binary is 1 * 2**-3 or 1/8 or 0.125 decimal 0.0001 binary is 1 * 2**-4 or 1/16 or 0.0625 decimal ...etc... 10.0000 hexadecimal is 1 * 16**1 or 16 decimal 1.0000 hexadecimal is 1 * 16**0 or 1 decimal 0.1000 hexadecimal is 1 * 16**-1 or 1/16 or 0.0625 decimal 0.0100 hexadecimal is 1 * 16**-2 or 1/256 ... 0.0010 hexadecimal is 1 * 16**-3 or 1/4096 ... ...etc... Note that 16**3 = (2**4)**3 = 2**12 = 2**2 * 2**10 = 4 * 1024 = 4096 Normalized scientific notation E-Notation: 123.456E7 means 123.456 times 10**7 (10 to the power 7) 123.456e7 means the same (lower-case E) A normalized number has only *one* non-zero digit on the *left* of the radix point, e.g. 123.456e7 normalized is 1.23455e9 and 0.0123e5 normalized is 1.23e3. (Zero is an exception: 0.0e0.) In *binary* floating point, 1101.111(2) normalized is 1.101111 times 2**3 (2 to the power 3). 0.01010(2) normalized is 1.010 times 2**-2 (2 to the power -2, or 1/(2**2)). The digit to the left of the radix point is always a 1 in normalized binary, except for the zero case. Every floating-point number has a unique normalized representation. Expressing binary using hexadecimal constants in computer programs You may write a hexadecimal integer constant F5EA in a computer program using the 0x syntax "0xF5EA", e.g. int x = 0xf5ea; You may write a hexadecimal floating-point constant "1.F5EA times 2 to the power 5" using the 0x and "p" exponent notation "0x1.F5EAP5" e.g. double x = 0x1.f5eap5; or float x = 0x1.f5eap5f; Constants may use either upper-case or lower-case hexadecimal letters: 0x1.f5eap5 == 0X1.F5EAP5 and 0xdeadbeef == 0xDEADBEEF -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/