Data Representation
Bits Binary Digits 0 or 1 Everything stored in a computer is stored as bits. Bits can mean different things depending on how the software or hardware interpret the bits Bits are usually grouped into chunks of eight bits A chuck of eight bits is called a byte
Digitization Convertor 01100110 Computing Device 0011010 Convertor Input such as sound, light, pressing a key Output such as sound, light, moving a robot arm
Bits Consider the follow byte ( 8 bits) 01011010 What could it represent? The integer 90 The letter Z (ASCII) Red part of color deep reddish brown http://www.perbang.dk/rgb/5a0000/ The opcode of an instruction The sequence of boolean values false true false true true false true false
Common Questions How many bits are needed to represent a set of values? How many bits do I need to represent the values in a set with N elements? Ceiling(log 2 N) If N is 8 then 3 bits are required If N is 11 then 4 bits are required
Common Questions How many values can be represented with a given number of bits? How many values can be represented with N bits? 2 N If N is 5 the 32 values can be represented If N is 7 then 128 values can be represented
Maximum Patterns for a Given Number of Bits Number of Bits Maximum Number of Pattern 1 2 2 4 3 8 4 16 8 256 N 2 N
Counting In Binary Binary Decimal 0 0 1 1 10 2 11 3 100 4 101 5 110 6 111 7 1000 8 1001 9 1010 10 1011 11
Character Code Mapping between numbers and characters ASCII Unicode
ASCII Codes Letter Symbol, Decimal, Binary A, 65, 01000001 B, 66, 01000010 C, 67, 01000011 D, 68, 01000100 E, 69, 01000101 F, 70, 01000110 G, 71, 01000111 H, 72, 01001000 I, 73, 01001001 Symbol, Decimal, Binary a, 97, 01100001 b, 98, 01100010 c, 99, 01100011 d, 100, 01100100 e, 101, 01100101 f, 102, 01100110 g,103, 01100111 h,104, 01101000 i, 105, 01101001
ASCII Codes Digits and Other Symbol, Decimal, Binary 0, 48, 00110000 1, 49, 00110001 2, 50, 00110010 3, 51, 00110011 4, 52, 00110100 5, 53, 00110101 6, 54, 00110110 7, 55, 00110111 8, 56, 00111000 9, 57, 00111001 Symbol, Decimal, Binary Line Feed, 10, 00001010 Space, 32, 00100000
ASCII Character Code
ASCII Character Code
Example Problem Convert the word Computer into a sequence of numbers representing the ASCII characters codes of the letters.
Example Problem Answer Computer 67 111 109 112 117 116 101 114
Example Problem Convert the following sequence of numbers to text using the ASCII character code. To make the sequence easier to read a space is used to separate numbers. 67 111 100 101 33
Example Problem Answer 67 111 100 101 33 Code!
Practice Problems Convert the sequence of letters in your name (first last) into a sequence of numbers using the ASCII character code. The code for a space is 32 Convert the following sequence of numbers to text using the ASCII character code. To make the sequence easier to read a space is used to separate numbers. 87 104 97 116 32 102 117 110 33
Place-Value Notation Consider the base 10 number 7438 This number (sequence of digits) represents 7*10 3 + 4*10 2 + 3*10 1 + 8*10 0 7000+400+30+8 = 7438 Since the number is a base 10 number the digit in position i is multiplied by 10 i Positions are numbered from right to left beginning at 0 This works for any base A base n number must have n different digits
Place-Valued Notation Most common bases used in computer science Base 2 (binary) Base 8 (octal) Base 10 (decimal) Base 16 (Hexadecimal)
Base 2 Two Digits 0 and 1 Consider the base 2 number 100111 This number represents 1*2 5 + 0*2 4 + 0*2 3 + 1*2 2 + 1*2 1 + 1*2 0 Since there are only two digits it is common to write the above expression using only the powers of 2 that are multiplied by 1 2 5 + 2 2 + 2 1 + 2 0 This represents to base 10 number 32 + 4 + 2 + 1 = 39 To indicate the base of a number a subscript is sometimes used at the end of the number 100111 2
Powers of 2 Formula Value 2 0 1 2 1 2 2 2 4 2 3 8 2 4 16 2 5 32 2 6 64 2 7 128 2 8 256 2 9 512 2 10 1024
Base 8 Eight digits 0, 1, 2, 3, 4, 5, 6, 7 Consider the base 8 number 5072 The number represents 5 * 8 3 + 0 * 8 2 + 7*8 1 + 2*8 0 The is equivalent to the base 10 number 5 * 512 + 0 * 64 + 7 * 8 + 2 * 1 = 2618
Powers of 8 Formula Value 8 0 1 8 1 8 8 2 64 8 3 512 8 4 4096 8 5 32768 8 6 262144 8 7 2097152
Base 16 Sixteen digits Consider the base 16 number B74D This number represents B*16 3 + 7*16 2 + 4*16 1 + D*16 0 11*16 3 + 7*16 2 + 4*16 1 + 13*16 0 This is equivalent to the base 10 number 11*4096 + 7*256 + 4*16 + 13*1 = 46925
Powers of 16 Formula Value 16 0 1 16 1 16 16 2 256 16 3 4096 16 4 65536 16 5 1048576
Base 10 Base 2 Base 8 Base 16 0 0000 0 0 1 0001 1 1 2 0010 2 2 3 0011 3 3 4 0100 4 4 5 0101 5 5 6 0110 6 6 7 0111 7 7 8 1000 10 8 9 1001 11 9 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F
Conversion Problems Convert between bases For example given the base 2 number 11001001 find the equivalent base 10 and base 16 number Base 10 201 Base 16 C9
Conversion Problems Base 2 -> Base 10 Base 2 -> Base 16 Base 10 -> Base 2 Base 10 -> Base 16 Base 16 -> Base 2 Base 16 -> Base 10
Convert Base 2 to Base 16 last4bits(x) last4bits(11001101) 1101 bitsexcludinglast4(x) bitsexcludinglast4(11001101) 1100
Convert base 2 to base 16 concatenate(a, B) concatenate(6, 547) 6547 base16digit(x) base16digit(1100) C
Algorithm to Convert Base 2 to Base 16 Let X be the base 2 number we want to convert to base 16 Give B the value of the empty string Do the following Give Y the value of last4bits(x) Give Z the value base16digit(y) Give B the value concatenate(z, B) Given X the value of bitsexcludinglast4(x) Until X is empty B is the base 16 representation of the original X
Algorithm to convert base 10 representation to binary representation Let X be the base 10 representation to convert Give B the value of the empty string Do the following Give Y the value of the integer quotient of X / 2 Give Z the value of the remainder of X / 2 Give B the value of concatenate(z, B) Give X the value of Y Until X is 0 B is now the binary representation of the original value of X
Example: convert 14 10 to binary X Y Z B 14 7 0 7 0 3 1 3 10 1 1 1 110 0 1 0 1110
Practice Problems Convert the following base 10 numbers to base 2 27 43 80 How can you check your answer?
How Many Dots? 10101 2 25 8 21 10 15 16