Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 2130 Intro Computer Systems Thompson Rivers University

Similar presentations


Presentation on theme: "COMP 2130 Intro Computer Systems Thompson Rivers University"— Presentation transcript:

1 COMP 2130 Intro Computer Systems Thompson Rivers University
Data Representation Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

2 Course Contents Introduction to computer systems: B&O 1
Data representations: B&O 2.1 – 2.4 Introduction to C programming: K&R 1 – 4 C: advanced topics: K&R 5.1 – 5.10, 6 – 7 Introduction to IA32 (Intel Architecture 32): B&O 3.1 – 3.8, 3.13 Compiling, linking, loading, and executing: B&O 7 (except 7.12) Dynamic memory management – Heap: B&O – 9.9.2, – 9.9.5, 9.11 Code optimization: B&O 5.1 – 5.6, 5.13 Memory hierarchy, locality, caching: B&O 5.12, 6.1 – 6.3, – 6.4.2, 6.5, – 6.6.3, 6.7 Virtual memory (if time permits): B&O 9.4 – 9.5 Not the replacement of IPv6; Inter-transition mechanism; But if IPv6 would fail TRU-COMP2130 Data Representation

3 Unit Learning Objectives
Convert a decimal number to binary, Octal, Hexadecimal. Convert a hexadecimal number to binary number, or Octal and vice versa Compute binary addition. Compute binary subtraction using 2’s complement. Determine the 2’s complement representation of a signed integer. Understand the overflow of unsigned integers and signed integers. Understand the 2’s complement representation for negative integers. Subtract a binary number by using the 2’s complement addition. Multiply two binary numbers. Distinguish little endian byte order and big endian byte order Use of left shift and right shift. Binary division TRU-COMP2130 Data Representation

4 1. Information Storage memory, address Example
Memory space is partitioned into more manageable units to store the different program objects, i.e., instructions, program data, and control information. The actual machine level program simply treats each program object as a block of bytes, and the program itself as a sequence of bytes. Example int number = 28; 4 bytes, i.e., 32 bits, will be allocated to the variable number. The decimal number 28 will be stored in the 32 bits? How? Binary number, not the decimal, will be stored in the 32 bits. Do programmers have to convert 28 to it’s binary number? TRU-COMP2130 Data Representation

5 d2*B2+d1*B1+d0*B0+d-1*B-1+d-2*B-2
The Decimal System Base (also called radix) = 10 10 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } Digit Position Integer & fraction Digit Weight Weight = (Base) Position Magnitude Sum of “Digit x Weight” Formal Notation 1 -1 2 -2 5 1 2 7 4 10 1 0.1 100 0.01 500 10 2 0.7 0.04 d2*B2+d1*B1+d0*B0+d-1*B-1+d-2*B-2 (512.74)10 TRU-COMP2130 Number Systems

6 The Binary System In computer systems, the most basic memory unit is a bit that contains 0 and 1. The data unit of 8 bits is referred as a byte that is the basic memory unit used in main memories and hard disks. All data are represented by using binary numbers. Data types such as text, voice, image and video have no meaning in the data representation. 8 bits are usually used to express English alphabets. A collection of n bits has 2n possible states. Is it true? E.g., How many different numbers can you express using 2 bits? How many different numbers can you express using 4 bits? How many different numbers can you express using 8 bits? How many different numbers can you express using 32 bits? TRU-COMP2130 Number Systems

7 Binary Number System 1 1 1 =(5.25)10 (101.01)2 1 0 1 1 1 1 0 0 0 1 0 1
Base = 2 2 digits { 0, 1 }, called binary digits or “bits” Weights Weight = (Base) Position Magnitude Sum of “Bit x Weight” Formal Notation Groups of bits bits = Nibble 8 bits = Byte Kilobyte (KB) = 1,024 Bytes Megabyte (MB) = 1,024 Kilobytes Gigabyte (GB) =1,024 Megabytes Terabyte (TB) = 1,024 Gigabytes Petabyte (PB) = 1,024 Terabytes Exabyte (EB) = 1,024 Petabytes 1 -1 2 -2 1/2 4 1/4 1 1 1 1 *22+0 *21+1 *20+0 *2-1+1 *2-2 =(5.25)10 (101.01)2 TRU-COMP2130 Number Systems

8 Octal Number System 5 1 2 7 4 =(330.9375)10 (512.74)8 Base = 8 Weights
8 digits { 0, 1, 2, 3, 4, 5, 6, 7 } Weights Weight = (Base) Position Magnitude Sum of “Digit x Weight” Formal Notation 1 -1 2 -2 8 1/8 64 1/64 5 1 2 7 4 5 *82+1 *81+2 *80+7 *8-1+4 *8-2 =( )10 (512.74)8

9 Hexadecimal Number System
Base = 16 16 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F } Weights Weight = (Base) Position Magnitude Sum of “Digit x Weight” Formal Notation 1 -1 2 -2 16 1/16 256 1/256 1 E 5 7 A 1 * *161+5 *160+7 * *16-2 =( )10 (1E5.7A)16

10 The Power of 2 n 2n 20=1 1 21=2 2 22=4 3 23=8 4 24=16 5 25=32 6 26=64 7 27=128 n 2n 8 28=256 9 29=512 10 210=1024 11 211=2048 12 212=4096 20 220=1M 30 230=1G 40 240=1T Kilo Mega Giga Tera

11 Conversion Chart Uses two digits 0 and 1. How to expand binary numbers? 02 = 0×20 = 12 = 1×20 = 102 = 1×21 + 0×20 = 112 = 1×21 + 1×20 = 1002 = 1×22 + 0×21 + 0×20 = 1012 = 1×22 + 0×21 + 1×20 = 1102 = 1×22 + 1×21 + 0×20 = 1112 = 1×22 + 1×21 + 1×20 = 10002 = 1×23 + 0×22 + 0×21 + 0×20 = 810 10012 = 1×23 + 0×22 + 0×21 + 1×20 = 910 ... TRU-COMP2130 Number Systems

12 Powers of 2 12 = 1×20 = 102 = 1×21 + 0×20 = 1002 = 1×22 + 0×21 + 0×20 = 10002 = 1×23 + 0×22 + 0×21 + 0×20 = 810 = = = TRU-COMP2130 Number Systems

13 Number Base Conversions
Evaluate Magnitude Octal (Base 8) Evaluate Magnitude Decimal (Base 10) Binary (Base 2) Hexadecimal (Base 16) Evaluate Magnitude

14 Decimal to Binary Conversion
Divide the number by the ‘Base’ (=2) Take the remainder (either 0 or 1) as a coefficient Take the quotient and repeat the division Example: (13)10 Quotient Remainder Coefficient 13 / 2 = a0 = 1 6 / 2 = a1 = 0 3 / 2 = a2 = 1 1 / 2 = a3 = 1 Answer: (13)10 = (a3 a2 a1 a0)2 = (1101)2 MSB LSB

15 Decimal to Binary Conversion
Multiply the number by the ‘Base’ (=2) Take the integer (either 0 or 1) as a coefficient Take the resultant fraction and repeat the division Example: (0.625)10 Integer Fraction Coefficient 0.625 * 2 = a-1 = 1 0.25 * 2 = a-2 = 0 0.5 * 2 = a-3 = 1 Answer: (0.625)10 = (0.a-1 a-2 a-3)2 = (0.101)2 MSB LSB

16 Decimal to Octal Conversion
Example: (175)10 Quotient Remainder Coefficient 175 / 8 = a0 = 7 21 / 8 = a1 = 5 2 / 8 = a2 = 2 Answer: (175)10 = (a2 a1 a0)8 = (257)8 Example: (0.3125)10 Integer Fraction Coefficient 0.3125 * 8 = a-1 = 2 0.5 * 8 = a-2 = 4 Answer: (0.3125)10 = (0.a-1 a-2 a-3)8 = (0.24)8

17 Binary − Octal Conversion
0 0 0 1 0 0 1 2 0 1 0 3 0 1 1 4 1 0 0 5 1 0 1 6 1 1 0 7 1 1 1 8 = 23 Each group of 3 bits represents an octal digit Example: Assume Zeros ( )2 ( )8 Works both ways (Binary to Octal & Octal to Binary)

18 Binary − Hexadecimal Conversion
1 2 3 4 5 6 7 8 9 A B C D E F 16 = 24 Each group of 4 bits represents a hexadecimal digit Example: Assume Zeros ( )2 ( )16 Works both ways (Binary to Hex & Hex to Binary)

19 Octal − Hexadecimal Conversion
Convert to Binary as an intermediate step Example: ( )8 Assume Zeros Assume Zeros ( )2 ( )16 Works both ways (Octal to Hex & Hex to Octal)

20 Decimal, Binary, Octal and Hexadecimal
00 0000 01 0001 1 02 0010 2 03 0011 3 04 0100 4 05 0101 5 06 0110 6 07 0111 7 08 1000 10 8 09 1001 11 9 1010 12 A 1011 13 B 1100 14 C 1101 15 D 1110 16 E 1111 17 F

21 Decimal Addition 1 1 Carry 5 5 + 5 5 1 1 = Ten ≥ Base
= Ten ≥ Base  Subtract a Base

22 Binary Addition Column Addition 1 1 1 1 1 1 1 1 1 1 1 = 61 = 23 + 1 1 1 1 1 1 1 = 84 ≥ (2)10

23 Binary Subtraction 1 2 = (10)2 2 2 2 1 1 1 1 − 1 1 1 1 1 1 1 1 = 77
Borrow a “Base” when needed 1 2 = (10)2 2 2 2 1 1 1 1 = 77 = 23 1 1 1 1 1 1 1 1 = 54

24 Binary Multiplication
Bit by bit 1 1 1 1 x 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

25 Hexadecimal Number System
1010 = = A16 = 0xA 1110 = = B16 = 0xB 1210 = = C16 = 0xC 1310 = = D16 = 0xD 1410 = = E16 = 0xE 1510 = = F16 = 0xF 14816 = ??? = ???2 23c9d6ef = ???10 23c9d6ef = ???2 4 bits can be used for a hexadecimal number, 0, ..., F. Please memorize it! TRU-COMP2130 Number Systems

26 Binary to/from Hexadecimal conversion
4DA916 = ???2 = ???16 = = 4DA9 = 0x??? = ???10 = 0x??? = ???10 TRU-COMP2130 Number Systems

27 Words A word size – the nominal size of integer and pointer data. A pointer variable contains an address in the virtual address space. We will discuss pointer variable in the next unit. The word size determines the maximum size of the virtual address space. 32bit operating systems? 64bit operating systems? TRU-COMP2130 Data Representation

28 Addressing and Byte Ordering
A variable x of type int (allocates 4 bytes) If the address of x: 0x100 (means it starts storing from 0x100) This means the 4 bytes of x would be stored in memory locations 0x100, 0x101, 0x102, and 0x103. Let’s assume x has the value 0x , which needs to be stored in four bytes. There are two conventions to store the values in the 4 consecutive byte memory locations. 0x01, 0x23, 0x45, and 0x67, or 0x67, 0x45, 0x23, and 0x01, depending on CPU architecture. Little endian byte order – Intel-compatible machines 0x103 0x102 0x101 0x address 0x01 0x23 0x45 0x67 value Big endian byte order – machines from IBM and Sun Microsystems 0x103 0x102 0x101 0x100 0x67 0x45 0x23 0x01 TRU-COMP2130 Data Representation

29 Why are the byte orderings important?
The byte orderings are totally invisible for most application programmers. Why are the byte orderings important? Think of data exchange between two machines through a network. Assembly programming When type casting is used TRU-COMP2130 Data Representation

30 2. Integer Representations
C Java Size char, unsigned char byte 1B short, unsigned short short 2Bs char in Java uses 2Bs. int, unsigned int int 4Bs long, unsigned long long 8Bs // there is no unsigned in Java float float 4Bs double double 8Bs TRU-COMP2130 Data Representation

31 Unsigned Encodings unsigned char All 8 bits are used. No sign bit.
The smallest number is 0 The maximum number is 0xff. unsigned short 16 bits The smallest number is ___ The maximum number is ____ unsigned int 32 bits The smallest number is ___ unsigned long 64 bits The smallest number is ____ The maximum number is _____ TRU-COMP2130 Data Representations

32 Representation of Unsigned Integers
8-bit representation of unsigned char The maximum number is ? The minimum number is ? What if we add the maximum number by 1 ??? What if we subtract the minimum number by 1 ??? +1 +1 +1 overflow TRU-COMP2130 Data Representations

33 Binary Addition We will discuss binary addition and binary subtraction, before we discuss the representation of signed integers. How to add two binary numbers? Let’s consider only unsigned integers (i.e., positive numbers only) for a while. Just like the addition of two decimal numbers. E.g., ??? 10111 ??? carry TRU-COMP2130 Data Representations

34 Binary Subtraction How to subtract a binary number?
Just like the subtraction of decimal numbers. E.g., 0112 ?1 ? Try: How to do? 1 TRU-COMP2130 Data Representations

35 In the previous slide, 10010 – 11 = 1111 What if we add 00010010
Is there any relationship between 112 and ? The 1’s complement of 112 is ??? Switching 0  1 This type of addition is called 1’s complement addition. Find the 8-bit one’s complements of the followings. > > 10 -> > 101 -> > TRU-COMP2130 Data Representations

36 In the previous slide, 10010 – 11 = 1111 What if we add 00010010
Is there any relationship between 11 and 11101? The 2’s complement of 11 is ??? 2’s complement ≡ 1’s complement + 1 -> = 11101 This type of addition is called 2’s complement addition. Find the 16-bit two’s complements of the followings. > > 10 101 TRU-COMP2130 Data Representations

37 Another example 101010 ??? What if we use 1’s complement addition or 2’s complement addition instead as follow? Let’s use 8-bit representation. What does this mean? A – B = A + (–B), where A and B are positive Is the 1’s complement or the 2’s complement of B sort of equal to –B? TRU-COMP2130 Data Representations

38 Let’s use 8-bit 2’s complement addition for 12 – 102.
Can we use 8-bit 1’s complement addition for 12 – 102 = –12 ? <- 8-bit 1’s complement of 10 <- Is this correct? (1’s complement of 1?) Let’s use 8-bit 2’s complement addition for 12 – 102. <- 2’s complement of 10 <- Correct? (2’s complement of 1?) 12 – 102 = 12 + (–102) = –12 How to represent negative binary numbers, i.e., signed integers? TRU-COMP2130 Data Representations

39 Representation of Negative Binaries
Representation of signed integers 8 or 16 or 32 bits are usually used for integers. Let’s use 8 bits for examples. The left most bit (called most significant bit) is used as sign. When the MSB is 0, non-negative integers. When the MSB is 1, negative integers. The other 7 bits are used for integers. How to represent positive integer 9? How about -9? is really okay? (9) (-9) = (-18) It is wrong! We need a different representation for negative integers. TRU-COMP2130 Data Representations

40 2’s complement representation is used for negative integers.
How about -9? is really okay? (9) (-9) = (-18) It is wrong! We need a different representation for negative integers. What is the 8-bit 1’s complement of 9? <- 8-bit 1’s complement of 9 < bit 1’s complement of 9 = <- Is it zero? (1’s complement of 0?) What is the 2’s complement of 9? <- 8-bit 2’s complement of 9 < bit 2’s complement of 9 = <- It looks like zero. 2’s complement representation is used for negative integers. TRU-COMP2130 Data Representations

41 12 – 102 = 12 + (–102) ??? What is the result in decimal? 00000001
<- 2’s complement of 10, i.e., -102 <- 2’s complement of 1, i.e., -1 (= 1 – 2) – 1012 = (–1012) ??? – 112 ??? 102 – 12 ??? -102 – 12 ??? Is the two’s complement of the two’s complement of an integer the same integer? What is x when the 8-bit 2’s complement of x is TRU-COMP2130 Data Representations

42 8-bit representation of signed char with 2’s complement
The maximum number is ? The minimum number is ? What if we add the maximum number by 1 ??? What if we subtract the minimum number by 1 ??? overflow +1 +1 -1 +1 overflow -1 -1 TRU-COMP2130 Data Representations

43 16-bit representation signed short with 2’s complement
The maximum number is ? What if we add the maximum number by 1 ??? The minimum number is ? What if we subtract the minimum number by 1 ??? overflow +1 +1 +1 -1 -1 -1 overflow TRU-COMP2130 Data Representations

44 In programming languages
Note that computers use the 8-bit representation, the 16-bit representation, the 32-bit representation and the 64-bit representation with 2’complement for negative integers. In programming languages char, unsigned char 8-bits short, unsigned short 16-bits int, unsigned int 32-bits long, unsigned long 64-bits When we use the 32-bit representation with 2’s complement, The maximum number is ? What if we add the maximum number by 1 ??? The minimum number is ? What if we subtract the minimum number by 1 ??? TRU-COMP2130 Data Representations

45 Now we know how to represent negative integers.
2’ complement addtion A + (–B) is computed for subtraction A – B. Let’s suppose B is negative. Then –B is really a positive integer? For example, let’s consider 1 byte signed integer. ’s complement of -3 is , i.e., 3. ’s complement of -128 is again. For any -127 < x < 127, x – x = 0. But (-128) – (-128) = ??? TRU-COMP2130 Data Representation

46 Binary Multiplication
Bit by bit 1 1 1 1 x 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 TRU-COMP2130 Data Representation

47 This is why division is an expensive operation.
Binary Division Binary division? 1111 <- quotient -101 1001 1000 111 10 <- remainder Try / 110 Dividing negative binary numbers: division without sign, and then put the sign. This is why division is an expensive operation. TRU-COMP2130 Number Systems

48 Binary division by a power of 2? 1001101 / 10 = 100110
/ 100 = 10011 / 1000 = 1001 What if we shift right by 1 bit? What if we shift right by 2 bits? What if we shift right by 3 bits? / 101 ??? Complicated implementation required TRU-COMP2130 Number Systems

49 2.4 Floating Point Fractional binary numbers
IEEE floating-point representation TRU-COMP2130 Data Representation

50 Fractions: Fixed-Point
How can we represent fractions? Use “binary point” to separate positive from negative powers of two -- like “decimal point.” 2’s comp addition and subtraction still work. (Assuming binary points are aligned) (40.625) (-1.25) (2’s complement) (39.375) 2-1 = 0.5 2-2 = 0.25 2-3 = 0.125 No new operations -- same as integer arithmetic. TRU-COMP2130 Number Systems

51 How to deal with big numbers
Just for your curiosity: How to convert decimal fractions to binary? = ???2 0.625 * 2 = > 1 (0.625 = x y z ) 0.25 * 2 = 0.5 -> 0 (1.25 = x + y z => x = 1) 0.5 * 2 = 1.0 -> 1 (0.25 = y z ) Therefore 0.101 0.710 = ???2 0.7 * 2 = 1.4 -> 1 0.4 * 2 = 0.8 -> 0 0.8 * 2 = 1.6 -> 1 0.6 * 2 = 1.2 -> 1 0.2 * 2 = 0.4 -> 0 ... Therefore How to deal with big numbers and small numbers? TRU-COMP2130 Number Systems

52 Binary Logic AND OR NOT z = x • y = x y z = x + y Truth table x y z 1
1 x y z 1 x z 1 z = x • y = x y z = x + y TRU-COMP2130 Introduction

53 ASCII Character Codes American Standard Code for Information Interchange (Refer to Table 1.7) A popular code used to represent information sent as character-based data. It uses 7-bits to represent: 94 Graphic printing characters. 34 Non-printing characters. Some non-printing characters are used for text format (e.g. BS = Backspace, CR = carriage return). Other non-printing characters are used for record marking and flow control (e.g. STX and ETX start and end text areas).

54 ASCII Properties ASCII has some interesting properties:
Digits 0 to 9 span Hexadecimal values 3016 to 3916 Upper case A-Z span 4116 to 5A16 Lower case a-z span 6116 to 7A16 Lower to upper case translation (and vice versa) occurs by flipping bit 6.


Download ppt "COMP 2130 Intro Computer Systems Thompson Rivers University"

Similar presentations


Ads by Google