170 likes | 395 Views
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)
E N D
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 • Only two O and 1 • Unambiguous and clear signals • 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/toolbox/color.html
Review of Decimal Numbers • Decimal numbers are based on powers of 10. Mathematically, the number 537 can be calculated.. • 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 What is 10011100 in decimal? 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
0010 1010 1100 Hex to Binary Example • Hex to Binary – just convert digits 2ac 2ac = 001010101100 No magic!! - remember each hex digit = 4 bits
5 3 7 b Binary to Hex • Just convert groups of 4 bits 101001101111011 0101 0011 0111 1011 101001101111011 = 537b
Hex to Decimal • Just multiply each hex digit by decimal value, and add the results. 2ac 2 • 256 + 10 • 16 + 12 • 1 = 684
Decimal to Hex 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
Decimal to Hex 684 2__ 684/256 = 2 684/256 = 172 2a_ 172/16 = 10 = a 172/16 = 12 = c 2ac
Exercise Questions • Convert decimal value 110 to • Binary • Hexadecimal • Convert binary value 1100111 to • Decimal • Hexadecimal • Convert hexadecimal value 6CAD to • Decimal • Binary