#DATA $NAME "An Introduction to Number Bases" / $AUTHOR "Light Ray" / $VERSION 001 / $DATE 950709 / $TYPE { TEXT ASCII CRLF } #DATA ******************************************************** An Introduction To NUMBER BASES by Light Ray ******************************************************** PREREQUISITE: A Brain --------------------------------- Notes --------------------------------- This is taken from the "lab notes" from Explorer Post 340, the oldest Boy Scout Explorer Post in the world, as far as I know. Explorer Post 340 is wonderful. We meet at the Western Digital Building in the El Toro "Y", between the I-5 and 405 ("San Diego") freeways every Wednesday night from about 7:15pm to around 9:30pm. A knowledge of alternate number bases (namely binary, hexidecimal, and sometimes octal) is necessary for advanced programming, digital logic design, impressing your friends, winning the lottery, and having a successful and fullfilling life.. Well, almost all of those. You should memorize the 16 possible binary nibbles and their corresponding values in decimal and hexidecimal, though you will do this automatically if you continue to use them frequently. You'll probably be saying, "Yeah, I am 010010 years old and I got my car in the year 07C9, A.D." by the time you finish reading this... BTW, this is the first file in a series. --------------------------------- Introduction --------------------------------- In base X, there are X symbols. People currently use base ten just about everywhere, supposedly because people have ten fingers. Well, most people do, except for those of us who take interest in machining or pyrotechnics. Bases other than the ever present base-10 have more or fewer symbols making them more applicable for non-human applications, those that do not have ten fingers. --------------------------------- A Summary Of Base 10 --------------------------------- Base 10 (decimal) uses 10 symbols. These are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. This is why it is called base 10. When writing a number in a specific base, the base is written in subscript after the number. Since I can't do subscript in ASCII text, I am the base in perenthesis: 13(base 10) To count in base 10, we start at 0 and add one until we get to 9. At this point, it is necessary to create an addittional column to the left, containing a one. Every time the first column reaches 9, it is set to zero and the column to the left is incremented. When 99 is reached a third column containing a one is created and the two nines are set to zero, etc. In base 10, every column is 10 times greater than the column to the right: thousands hundreds tens ones 10^3 10^2 10^1 10^0 [The carrot symbol (^) denotes "to the power of"] The value of the number 1234 in base 10 is computed by multiplying each digit by the value of its place and taking the sum of the individual products. 1234(base 10) = (1 * 10^3) + (2 * 10^2) + (3 * 10^1) + (4 * 10^0) 1234(base 10) = (1 * 1000) + (2 * 100) + (3 * 10) + (4 * 1) 1234(base 10) = 1000 + 200 + 30 + 4 1234(base 10) = 1234(base 10) [An asterik symbol (*) denotes "multiplied by"] The range of positive values that can be represented by n digits in base x is 0 to (x^n - 1). For example, the range of positive values that may be expressed by a ten digit number in base four is 0 to 9999. x = 10 n = 4 range = 0 to (x^n - 1) range = 0 to (10^4 - 1) range = 0 to (10000 - 1) range = 0 to 9999 --------------------------------- A Summary Of Base 2 --------------------------------- Base Two, otherwise known as Binary, uses TWO symbols. These two symbols are ZERO (0) and ONE (1). In binary, the symbols 2,3,4 etc do not exist. Binary is often used in digital electronics and logic because a digital signal has two states, on (1) and off (0). On is usually five volts and off is usually ground or zero volts. To count in base two, use the same system as base ten. However, you only have two symbols. Start at Zero. To find the next number, increment the first (right-most) digit. If this digit is already a ONE, then change it to a ZERO and increment the value to the left. Base 2 Base 10 (binary) (decimal) 0000 00 0001 01 0010 02 0011 03 0100 04 0101 05 0110 06 0111 07 1000 08 1001 09 1010 10 1011 11 1100 12 1101 13 1110 14 1111 15 In base two, every column has two times greater value than the column immediately to the right. eights fours twos ones 2^3 2^2 2^1 2^0 The value of the number 1101 in base 2 is computed by multiplying each digit by the value of its column and summing the individual products. 1101(base 2) = (1 * 2^3) + (1 * 2^2) + (0 * 2^1) + (1 * 2^0) 1101(base 2) = (1 * 8) + (1 * 4) + (0 * 2) + (1 * 1) 1101(base 2) = (8) + (4) + (0) + (1) 1101(base 2) = 13(base 10) The range of positive values that can be expressed by n digits in base two is zero through (2^n - 1). For example, the numbers 0 through (2^4 - 1) or 15 may be expressed in four digits of binary. A BIT is a single Binary digIT. A bit is either zero or one. A NIBBLE is four bits. A BYTE is two nibbles or eight bits. A byte has a value of zero to (2^8 - 1) or 255. A WORD is NORMALLY two bytes. A word can express 0 to 65535. A LONG WORD is NORMALLY two words. An 80386 processor transfers digital data a word at a time. A 486 processor transfers data a long word at a time. --------------------------------- A Summary Of Base 16 --------------------------------- Base 16 is known as hexidecimal. Hexidecimal uses sixteen symbols, which are: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F Keep in mind that: A(base 16) = 10(base 10) B(base 16) = 11(base 10) etc. The place values in hexidecimal are: 4096s 256s 16s ones 16^3 16^2 16^1 16^0 Sample Conversion: 1A3F(base 16) = (1 * 16^3) + (A * 16^2) + (3 * 16^1) + (F * 16^0) 1A3F(base 16) = (1 * 4906) + (10 * 256) + (3 * 16) + (15 * 1) 1A3F(base 16) = 4906 + 2560 + 48 + 15 1A3F(base 16) = 6719(base 10) --------------------------------- Converting from Decimal to Binary --------------------------------- The best way to illustrate how to convert a decimal number to binary (or any other base for that matter) is to show an example. Say that we want to convert 197(decimal) to binary. First, write out the place values for the target base. Start with a place value that is greater than the number you wish to convert. 256 128 064 032 016 008 004 002 001 2^8 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0 The object is to come up with 197 by taking the sum of selected place values. Those place values that are selected will be "1" and those that aren't will be set to "0". For example, 197 is 128 + 64 + 4 + 1 So we write 1's under the 128, 64, 4, and 1 columns, and 0's under the other columns: 256 128 064 032 016 008 004 002 001 0 1 1 0 0 0 1 0 1 The answer is 11000101(base 2). There is an easier way, but I don't remember it right now. (= --------------------------------- Converting from Hexidecimal to Binary and back. --------------------------------- The range that may be expressed by one binary nibble is 0 to 15. Incidentally, one Hexidecimal digit has the same range, 0 to 15. Handy conversion chart: Base 2 Base 10 Base 16 (binary) (decimal) ("hex") 0000 00 0 0001 01 1 0010 02 2 0011 03 3 0100 04 4 0101 05 5 0110 06 6 0111 07 7 1000 08 8 1001 09 9 1010 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F To convert 0100100101110110(base 2) to hexidecimal, first split it up into nibbles: 0100 1001 0111 0110 Then, convert each nibble to hex. Use the chart if necessary. binary: 0100 1001 0111 0110 hex: 4 9 7 6 Thus, 0100100101110110(base 2) = 4976(base 16) Since the base 16 version is so much shorter, we work with hex very often when programming and using digital logic. --------------------------------- END --------------------------------- This was typed and editted by Light Ray at the Digital Forest BBS, which may be reached at +1 (714) 586-6142. It is located in Mission Viejo, California, United States of America. This document is based on Explorer Post 340 "lab notes" by "rlh" dated 5/10/95. Please send any comments, suggestions, compaints, or addittions to me at dr261@cleveland.freenet.edu, even if all you say is "I read your file." I may also be reached at 1:103/925, 66:714/10, or 50:100/505. Light Ray