How Computers Represent Information Yong Choi School of Business CSU, Bakersfield Digital Storage Humans: decimal (or base 10) number system Computer: binary (base 2) number system On (“high” or 1) Off (“low” or 0) Each digit in a binary number is known as a bit and can have only one of two values, 0 or 1. One bit can store a number from 0 to 1 Two bits can store from 0 to 3 n bits can store numbers from 0 to 2n Binary Numbers Programmers often need to read numbers in the computer because the number of bits in calculation affects the accuracy and size limitations of numbers manipulated by the computer. So, it is useful to understand how the binary number system is used within the computer. In Java, programmer can declare a variable to be short (16 bits) and or long (64 bits) integer depending on the anticipated size of the number being used and required accuracy in calculations. Advantages of Binary Numbers Simple Unambiguous and clear signals Only two O and 1 Analog signal vs. Digital signal Flawless copies can be made Anything (English, Spanish, Chinese) can be represented with patterns of bits Hexadecimal Numbers Important!! Because it is commonly used as a shorthand notation for binary numbers. Each hexadecimal number exactly represent 4 binary bits. Most computers store and manipulate instructions and data using word sizes that are multiple of 4 bits. Colors in VB can be specified as a six-digit hexadecimal number. http://www.mrs.umn.edu/committees/wwwac/tool box/color.html Review of Decimal Numbers Decimal numbers are based on powers of 10. Mathematically, the number 537 can be calculated.. 5 • 102 3 • 101 7 • 100 5 • 100 3 • 10 7•1 500 30 7 Similarly 9238 is 9 • 103 + 2 • 102 + 3 • 101 + 8 • 100 Converting Binary to Decimal Easy, just multiply digit by power of 2 Binary numbers are based on power of 2. Just like a decimal number is represented So, 101 binary is 1 • 22 + 0 • 21 + 1 • 20 = 5 or 1•4 + 0•2 + 1•1=5 Binary to Decimal Example 7 6 5 4 3 2 1 0 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 What is 10011100 in decimal? 1 0 0 1 1 1 0 0 128 + 0 + 0 + 16 + 8 + 4 + 0 + 0 = 156 27 *1 + 26 *0 …………….. Converting Decimal to Binary A little more work than binary to decimal Find largest power-of-two smaller than decimal number Make the appropriate binary digit a ‘1’ Subtract the power of 2 from decimal Do the same thing again Some examples 3 = 2 + 1 = 11 (that’s 1•21 + 1•20) 5 = 4 + 1 = 101 (that’s 1•22 + 0•21 + 1•20) Decimal to Binary Example Convert 28 decimal to binary 28/2 = 12 ……. 0 12/2 = 10 ……. 0 10/2 = 5 ……… 0 5/2 = 4 ……….. 1 4/2 = 2 ……….. 0 2/2 = 1 ……….. 0 Answer: 1001000 Converting Hex to Binary Hexadecimal table Dec Bin Hex Dec Bin Hex 0 0000 0 8 1000 8 1 0001 1 9 1001 9 2 0010 2 10 1010 a 3 0011 3 11 1011 b 4 0100 4 12 1100 c 5 0101 5 13 1101 d 6 0110 6 14 1110 e 7 0111 7 15 1111 f Hex to Binary Example Hex to Binary – just convert digits 2ac 0010 1010 1100 2ac = 001010101100 No magic!! - remember each hex digit = 4 bits Binary to Hex Just convert groups of 4 bits 101001101111011 0101 0011 0111 1011 5 3 7 b 101001101111011 = 537b Dec Hex Hex to Decimal Just multiply each hex digit by decimal value, and add the results. 2ac 2 • 256 163 4096 + 10 • 16 + 12 • 1 = 684 162 256 161 16 160 1 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 a 11 b 12 c 13 d 14 e 15 f Decimal to Hex 1. 2. 3. 4. Analogous to decimal binary. Find largest power-of-16 smaller than decimal number Divide by power-of-16. The integer result is hex digit. The remainder is new decimal number. Do the same thing again Dec Hex Decimal to Hex 0 0 1 1 2 2 3 3 4 4 5 5 6 6 2a_ 7 7 8 8 2ac 9 9 10 a 11 b 12 c 13 d 14 e 684 2__ 684/256 = 2 684/256 = 172 172/16 = 10 = a 172/16 = 12 = c 163 4096 162 256 161 16 160 1 Exercise Questions Convert decimal value 110 to Convert binary value 1100111 to Binary Hexadecimal Decimal Hexadecimal Convert hexadecimal value 6CAD to Decimal Binary