DATA aka. “ The class where you learned to count in powers of 2. ” REPRESENTATION
321 ...in decimal (That is, “Base 10”) ↑ ↑ ↑ ← How did we get these? x100 x10 x1 ↑ ↑ ↑ ← Therefore, base “ 10 ” 10 2 10 1 10 0 ↑ ↑ ↑ ← Positions denote powers of 10 3x100 2x10 1x1 Symbols 0-9 denote value of position 2
101000001 One-Hundred Million and One? Actually… 321… in “binary” (that is, “base 2”) Why do we use binary? Computers use binary (bits) to store all information 3
5V 0V 1 0 1 0 0 0 1 0 0 4
1 0 1 0 0 0 0 0 1 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ x256 x128 x64 x32 x16 x8 x4 x2 x1 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ = 321 +256 +0 +64 +0 +0 +0 +0 +0 +1 Positions denote powers of 2. Symbols “0” and “1” denote position values. 5
PARTNER ACTIVITY Find decimal values of the binary numbers below. 16 8 4 2 1 x = 1 0 0 1 1 y = 0 1 0 0 1 What are the decimal values of: x-y x+y What is the binary representation of: x-y x+y 6
0x141 141 … in decimal? Actually… 321… in “ hexa decimal” (that is, “base 16”) Why do we use hexadecimal? Recall the binary for “ decimal 321 ” 101000001 7
101000001 1 0100 0001 0001 0100 0001 ↑ ↑ ↑ 1 4 1 8
141 ...in hexadecimal 0x (That is, “Base 16”) ↑ ↑ ↑ ← How did we get these? x256 x16 x1 ↑ ↑ ↑ ← Therefore, base “ 16 ” 16 2 16 1 16 0 ↑ ↑ ↑ ← Positions denote powers of 16 1x256 4x16 1x1 However, we need 16 symbols to = 256 + 64 + 1 denote possible values. 9 = 321
SYMBOLS TO COUNT WITH Hexadecimal Decimal Binary Binary (Base 2) 0 0 0000 0, 1 ▸ 1 1 0001 2 2 0010 Decimal (Base 10) 3 3 0011 0, 1, 2, 3, 4, 5, 6, 7, 8 , 9 ▸ 4 4 0100 5 5 0101 Hexadecimal (Base 16) 6 6 0110 0, 1, 2, 3, 4, 5, 6, 7, 8 , 9, 7 7 0111 ▸ 8 8 1000 A, B, C, D, E, F 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 10
HEXADECIMAL EXAMPLE Hexa- Decimal Binary 0 x 0 C D decimal 0 0 0000 1 1 0001 2 2 0010 ↑ ↑ ↑ 3 3 0011 4 4 0100 x256 x16 x1 5 5 0101 6 6 0110 ↑ ↑ ↑ 7 7 0111 8 8 1000 16 2 16 1 16 0 9 9 1001 ↑ ↑ ↑ A 10 1010 B 11 1011 C 12 1100 0x256 12x16 13x1 D 13 1101 = 0 + 192 + 13 E 14 1110 11 = 205 F 15 1111
PARTNER ACTIVITY Hexa- Decimal Binary 0 x 1 A F decimal 0 0 0000 1 1 0001 2 2 0010 ↑ ↑ ↑ 3 3 0011 4 4 0100 x256 x16 x1 5 5 0101 6 6 0110 ↑ ↑ ↑ 7 7 0111 8 8 1000 16 2 16 1 16 0 9 9 1001 ↑ ↑ ↑ A 10 1010 B 11 1011 C 12 1100 1x256 10x16 15x1 D 13 1101 = 256 + 160 + 15 E 14 1110 12 = 431 F 15 1111
CONVERTING BASES From “Base 10” to other bases Find largest power x of base less than number n ▸ Find largest base digit b where b*x < n ▸ Recursively repeat using n-(b*x) ▸ 13
EXAMPLE - DECIMAL TO HEXADECIMAL Convert 15213 10 to Base 16 (Hexadecimal) Powers of 16 = 65536 4096 256 16 1 Find the highest base less than 15213 = 4096 ▸ Largest b where b*4096 < 15213 = 3 ▹ 15213 – 3*4096 = 2925 ▹ Find the highest base less than 2925 = 256 ▸ Largest b where b*256 < 2925 = 11 or B ▹ 2925 – 11*256 = 109 ▹ Find the highest base less than 109 = 16 ▸ Largest b where b*16 < 109 = 6 ▹ 109 – 6*16 = 13 ▹ Find the highest base less than 13 = 1 ▸ b = 13 or D ▹ 15213 10 = 03B6D 16 ▸ 3B6D 16 = 3*163 + 11*162 + 6*161 + 13*160 ▹ 14 Written in C as 0x3b6d ▹
PARTNER ACTIVITY Convert the following to the specified bases: 10110111 2 to Base 10 ▸ 11011001 2 to Base 16 ▸ 0x2AE to Base 2 ▸ 0x13E to Base 10 ▸ 150 10 to Base 2 ▸ 301 10 to Base 16 ▸ Base 2 128 64 32 16 8 4 2 1 Base 16 65536 4096 256 16 1 268435456 16777216 1048576 15
0x333231 3,355,185 … in decimal? Actually… “321” in American Standard Code for Information Interchange (that is, “ASCII”) Humans encode characters in pairs of hexadecimal digits: Each pair of hex digits is 8 bits or 1 byte ▸ Bytes are the smallest unit of data for computers ▸ 16
ASCII TABLE 17
PARTNER ACTIVITY Convert the following hex code (encoded in ASCII), to readable, English text: Line 1: ▸ 54 68 65 72 65 20 61 72 65 20 31 30 20 74 79 70 65 73 20 6f 66 Line 2: ▸ 70 65 6f 70 6c 65 20 69 6e 20 74 68 69 73 20 77 6f 72 6c 64 2e Line 3: ▸ 54 68 6f 73 65 20 77 68 6f 20 63 61 6e 20 63 6f 75 6e 74 20 69 6e Line 4: ▸ 62 69 6e 61 72 79 2c 20 61 6e 64 20 74 68 6f 73 65 20 77 68 6f 20 63 61 6e 27 74 2e 18
PARTNER ACTIVITY Convert the following hex code (encoded in ASCII), to readable, English text: Line 1: ▸ There are 10 types of Line 2: ▸ 70 65 6f 70 6c 65 20 69 6e 20 74 68 69 73 20 77 6f 72 6c 64 2e Line 3: ▸ 54 68 6f 73 65 20 77 68 6f 20 63 61 6e 20 63 6f 75 6e 74 20 69 6e Line 4: ▸ 62 69 6e 61 72 79 2c 20 61 6e 64 20 74 68 6f 73 65 20 77 68 6f 20 63 61 6e 27 74 2e 19
PARTNER ACTIVITY Convert the following hex code (encoded in ASCII), to readable, English text: Line 1: ▸ There are 10 types of Line 2: ▸ people in this world. Line 3: ▸ 54 68 6f 73 65 20 77 68 6f 20 63 61 6e 20 63 6f 75 6e 74 20 69 6e Line 4: ▸ 62 69 6e 61 72 79 2c 20 61 6e 64 20 74 68 6f 73 65 20 77 68 6f 20 63 61 6e 27 74 2e 20
PARTNER ACTIVITY Convert the following hex code (encoded in ASCII), to readable, English text: Line 1: ▸ There are 10 types of Line 2: ▸ people in this world. Line 3: ▸ Those who can count in Line 4: ▸ 62 69 6e 61 72 79 2c 20 61 6e 64 20 74 68 6f 73 65 20 77 68 6f 20 63 61 6e 27 74 2e 21
PARTNER ACTIVITY Convert the following hex code (encoded in ASCII), to readable, English text: Line 1: ▸ There are 10 types of Line 2: ▸ people in this world. Line 3: ▸ Those who can count in Line 4: ▸ binary, and those who can’t. 22
DATA IN MEMORY Memory is organized as an array of bytes 0000 0001 0002 . . . FFFD FFFE FFFF . . . Addressable unit of memory is called a byte ▸ 1 Byte = 8 Bits ▸ An “address” is an index in the array ▸ Recall, a system provides private ▸ address spaces to each “process” Binary 0000 0000 2 to 1111 1111 2 Decimal 0 10 to 255 10 Hexadecimal 00 16 to FF 16 23
MACHINE WORDS Any given computer has a “word size” Nominal size of pointers (addresses) ▸ For IA32, word size was 32-bits (that is, 4 bytes) ▸ Limits addresses to 4 GB (2 32 bytes) ▹ With x64, word sizes are 64-bit (that is, 8 bytes) ▸ Potentially up to 18 PB (petabytes) of addressable memory ▹ That’s 18.4 x 10 15 bytes of memory! ▹ 24
WORDS ORGANIZATION Words stored over contiguous byte locations Address of word specifies the lowest address ▸ E.g. int x with address 0x4 is stored in bytes 0x4, 0x5, 0x6, 0x7. ▸ Addresses of successive words differ by 4 (32-bit) or 8 (64-bit) ▸ 25
BYTE ORDERING Which way should you store the 4 byte integer “x”? Assume that &x is 0x100 ▸ Assume that: ▸ int x = 0x01234567; ... 01 23 45 67 ... ... Data ... 0x100 0x101 0x102 0x103 ... ... Address Ordering 1 ... 67 45 23 01 ... ... Data ... 0x100 0x101 0x102 0x103 ... ... Address Ordering 2 26
BYTE ORDERING Which way should you store the 4 byte integer “x”? Assume that &x is 0x100 ▸ Assume that: ▸ int x = 0x01234567; ... 01 23 45 67 ... ... Data ... 0x100 0x101 0x102 0x103 ... ... Address Big Endian ... 67 45 23 01 ... ... Data ... 0x100 0x101 0x102 0x103 ... ... Address Little Endian 27
ENDIANNESS How are bytes in multi-byte words (short, int, long, any pointers) be ordered in memory? Sun, PowerPC Macs, Internet protocols are “Big Endian” ▸ Least significant byte has highest address ▹ 01 23 45 67 0x100 0x101 0x102 0x103 x86 (PC/Mac), ARM (Android/iOS) are “Little Endian” ▸ Least significant byte has lowest address ▹ 67 45 23 01 0x100 0x101 0x102 0x103 28
REPRESENTING POINTERS Recall a pointer is a variable containing a memory address of an object of a particular data type Contains a “reference” or address for data ▸ char * cp; /* Declares cp to be a pointer to a character */ int * ip; /* Declares ip to be a pointer to an integer */ On x86-64, how many bytes is cp? ▸ On x86-64, how many bytes is ip? ▸ 29
POINTERS IN MEMORY Given the following code on an x64 (little endian) system Contains a “reference” or address for data ▸ int main() { int B = -15213; int * P = &B; return 0; } Suppose: The address of B is 0x7fffffff8d8 ▸ The address of P is 0x7fffffff8d0 ▸ At the end of main, write the value of each byte of P in order as it appears in memory. 30
REPRESENTING STRINGS Strings in C Represented by array of characters ▸ Each character encoded in ASCII format ▸ Standard 7-bit encoding of character set ▹ Must be null-terminated ▸ Final character = 0 ▹ Compatibility Endian is not an issue ▸ Data are single byte quantities ▹ Text files generally platform independent ▸ Except for different conventions of line termination character(s)! ▹ 31
Recommend
More recommend