%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Secrets of Binary Numbers by David Tiberio %% %% dtiberio@libserv1.ic.sunysb.edu %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% What is the best way to use numbers? Most people are used to using only the decimal system, with 10 digits. The theory behind it is that humans have ten fingers, so miraculously the decimal system became popular. Some suggested using base 12 instead, which has 2 more digits. the reason behind this is that 12 is easier to work with in some cases, since it is easily divisible by 0, 1, 2, 3, 4, 5, 6, 8, 9, and 10. Others use base 8 (octal) and 16 (hexadecimal) for programming, but the undisputed champion of all numbering system is binary, using only 0 and 1. Of course you could try using unary systems, with only one digit, but then each number would be even more of a pain to count. :) Now, let's investigate the magic of binary. Find an unsuspecting person and ask him or her how high he can count on his fingers. Tell him that he must show, with his fingers, a given number such that it is not mistakable. In other words, he cannot say that counting ten fingers will also be good for 20 or 30, etc. Most people should say that they cannot count higher than 10 on their fingers without repeating fingers. Respond by telling them that you can count to 1024! Here is an 8 bit binary number I saw on a license plate last Friday: 01101001. This number, in decimal, equals 105. Each bit position carries a different weight. For example, let's look at 105 in decimal. 1 0 5 = 105 (1 * 100) + (0 * 10) + (5 * 1) = 105 (1 * 10^2) + (0 * 10^1) + (5 * 10^0) = 105 Each digit in the number 105 carries a different value based upon the number 10, which is the base value. The first digit, 1, is multiplied by 10^2 (10 squared). The 0 is multiplied by 10^1 (just 10 will do), and the 5 is multiplied by 10^0 (anything to the power of 0 is 1). Now let's look at the binary equivalent. Each number will be generated in the same way, except using base 2 instead of base 10. 0 1 1 0 1 0 0 1 = 105 (0 * 2^7) + (1 * 2^6) + (1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (0 * 2^1) + (1 * 2^0) = 105 FIGURE 1: 10000000 2^7 = 128 00001000 2^3 = 8 01000000 2^6 = 64 00000100 2^2 = 4 00100000 2^5 = 32 00000010 2^1 = 2 00010000 2^4 = 16 00000001 2^0 = 1 So, substitute from the table and get: 0*128 + 1*64 + 1*32 + 0*16 + 1*8 + 0*4 + 0*2 + 1*1 = 105 Here are the decimal and binary numbers from 1 to 16: FIGURE 2: 1 0001 9 1001 2 0010 10 1010 3 0011 11 1011 4 0100 12 1100 5 0101 13 1101 6 0110 14 1110 7 0111 15 1111 8 1000 16 10000 Now here is why it works this way. In decimal, let's say you have 100. You know that the number before it is 99. And when you get up to 1000, you know the previous number is 999. In binary, it works a little more interestingly. Let's say you have the binary number 1000. this is 2^3, or 8 in decimal. Since binary only has the numbers 0 and 1, the number before 1000 is 0111, or 7 in decimal. On the above chart, decimal 16 is 10000 in binary. Subtract 1 and you get 15 decimal, or 1111 in binary. Now for more fun. Let's say we have 4 people, named: Mr. Ali, Mr. Fish, Mr. Luck and Mrs. West. We want to know who will be attending the party. Each person will either attend (TRUE) or not attend (FALSE). If TRUE, a 1 is used. If FALSE, a 0 is used. Each person is also ordered alphabetically. The data is stored in a binary number. Mr. Ali is the man of honor, so he is attending. TRUE Mr. Fish was busy backing up his disks with BRU, so he can't attend. FALSE Mr. Luck hasn't been to the offices in a while, so he is going. TRUE Mrs. West found a job working out of state, so she can't attend. FALSE Now in binary, we are storing the answers in the order of the person's name, alphabetically. So we have a TRUE, a FALSE, a TRUE, and a FALSE. Above, we defined TRUE as equal to 1, and FALSE as equal to 0. This makes the binary number 0101 (or F-T-F-T). NOTE! The numbers are stored in REVERSE ORDER! If we had just 1, it would be no problem. If we add another numeral, such as 0, it would be 01, so that 1 keeps the same place (2^0). Now, the question is, is Mr. Luck going? We have the data 0101, and we know Mr. Luck is the 3rd digit from the right. We would check the number 0101 against the value used ot define Mr. Luck. Since Mr. Luck is the third position, he takes 2^2 (2^1 is the second position, and 2^0 is the first position). Now, 2^2 = 4 in decimal, so that is what we assign to Mr. Luck. Now, let's convert 0101 to decimal. 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0 = 0 + 4 + 0 + 1 = 5 in decimal Okay, our data equals 5. We take 5 and use the AND operation to check it with 4. In C this might look like: if(DATA && MR_LUCK) or if(5 && 4) in decimal or if(0101 && 0100) in binary. Now let's look at the binary comparison: 0101 0100 ^ This is the position. Since 0101 AND 0100 both have that bit set to 1, Mr. Luck is attending. Now in decimal, we have 0101 = 5 AMD 0100 = 4, or 5 AND 4. The only way to make 5 is by adding the numbers (2^2) and (2^0), which is (2^2 = 4) = (2^0 = 1), or 4 + 1 = 5, so we have proven that 4 is included in the numbers of the binary places that add to 5 (see FIGURE 1). There is much much more to using binary, and it may be hard to understand or read at first, but hopefully now you will know how to count to 1024 with just ten fingers. To give you a hint, try calculating 2^10! I took this from the Amiga Online Reference Manual (with permission), to show you what some number conversions look like. Take a look at the patterns formed in the binary numbers and compare them with the decimal. Note the numbers denoted by an "x". Index 7 ASCII Conversion Table BINARY OCTAL DECIMAL CHAR HEXD x 00000000 000 000 NUL 00 00000001 001 001 SOH 01 x 00000010 002 002 STX 02 00000011 003 003 ETX 03 x 00000100 004 004 EOT 04 00000101 005 005 ENQ 05 00000110 006 006 ACK 06 00000111 007 007 BEL 07 x 00001000 010 008 BS 08 00001001 011 009 HT 09 00001010 012 010 LF 0A 00001011 013 011 VT 0B 00001100 014 012 FF 0C 00001101 015 013 CR 0D 00001110 016 014 SO 0E 00001111 017 015 SI 0F x 00010000 020 016 DLE 10 00010001 021 017 DC1 11 00010010 022 018 DC2 12 00010011 023 019 DC3 13 00010100 024 020 DC4 14 00010101 025 021 NAK 15 00010110 026 022 SYN 16 00010111 027 023 ETB 17 00011000 030 024 CAN 18 00011001 031 025 EM 19 00011010 032 026 SUB 1A 00011011 033 027 ESC 1B 00011100 034 028 FS 1C 00011101 035 029 GS 1D 00011110 036 030 RS 1E 00011111 037 031 US 1F - AMINET REFERENCES - Check comp.sys.amiga.programmer for more information on the binary system. bilo.lha game/think 6K*Binary Logic: Think game, requires Kick 2. As Edward Cayce might say, that is all for the present... dtiberio@libserv1.ic.sunysb.edu