Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Representation Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.

Similar presentations


Presentation on theme: "Data Representation Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University."— Presentation transcript:

1 Data Representation Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

2 TRU-COMP2130 Data Representation2 Course Objectives The better knowledge of computer systems, the better programing. Computer SystemC Programming Language Computer architecture CPU (Central Processing Unit) IA32 assembly language Introduction to C language Compiling, linking, loading, executing Physical main memory MMU (Memory Management Unit) Virtual memory space Memory hierarchy Cache Dynamic memory management Better coding – locality Reliable and efficient programming for power programmers (to avoid strange errors, to optimize codes, to avoid security holes, …)

3 TRU-COMP2130 Data Representation3 Course Contents Introduction to computer systems: B&O 1 Introduction to C programming: K&R 1 – 4 Data representations: B&O 2.1 – 2.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.1 – 9.9.2, 9.9.4 – 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.1 – 6.4.2, 6.5, 6.6.2 – 6.6.3, 6.7 Virtual memory (if time permits): B&O 9.4 – 9.5

4 TRU-COMP2130 Data Representation4 Unit Learning Objectives Convert a decimal number to binary number. Convert a decimal number to hexadecimal number. Convert a binary number to decimal number. Convert a binary number to hexadecimal number. Convert a hexadecimal number to binary number. Distinguish little endian byte order and big endian byte order. 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. Trace and fix faulty code.

5 TRU-COMP2130 Data Representation5 Add two binary numbers. Compute the 1’s complement of a binary number. Compute the 2’s complement of a binary number. Understand the 2’s complement representation for negative integers. Subtract a binary number by using the 2’s complement addition. Multiply two binary numbers. Use of left shift and right shift. Binary division

6 TRU-COMP2130 Data Representation6 Unit Contents Information Storage Integer Representations Integer Arithmetic Floating Point

7 TRU-COMP2130 Data Representation7 1. Information Storage Virtual memory, address, and virtual address space Virtual address space is a conceptual image presented to the machine-level program. 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, 00000000 00000000 00000000 00011100 will be stored in the 32 bits. Do programmers have to convert 28 to it’s binary number?

8 The Decimal System Uses 10 digits 0, 1, 2,..., 9. Decimal expansion: 83 = 8×10 + 3 4728 = 4×10 3 + 7×10 2 + 2×10 1 + 8×10 0 84037 = ??? 43.087 = ??? Do you know addition, subtraction, multiplication and division? 1234 + 435.78 1234 – 435.78 1234 × 435.78 1234 / 435.78 TRU-COMP1380 Number Systems8

9 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 2 n 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-COMP1380 Number Systems9

10 How can we store integers (i.e., positive numbers only) in a computer? E.g., A decimal number 329? Is it okay to store 3 characters ‘3’, ‘2’, and ‘9’ for 329? How are characters stored? 329 10 = ??? 2 TRU-COMP1380 Number Systems10

11 Uses two digits 0 and 1. How to expand binary numbers? 0 2 = 0×2 0 = 0 10 1 2 = 1×2 0 = 1 10 10 2 = 1×2 1 + 0×2 0 = 2 10 11 2 = 1×2 1 + 1×2 0 = 3 10 100 2 = 1×2 2 + 0×2 1 + 0×2 0 = 4 10 101 2 = 1×2 2 + 0×2 1 + 1×2 0 = 5 10 110 2 = 1×2 2 + 1×2 1 + 0×2 0 = 6 10 111 2 = 1×2 2 + 1×2 1 + 1×2 0 = 7 10 1000 2 = 1×2 3 + 0×2 2 + 0×2 1 + 0×2 0 = 8 10 1001 2 = 1×2 3 + 0×2 2 + 0×2 1 + 1×2 0 = 9 10... TRU-COMP1380 Number Systems11

12 Powers of 2 1 2 = 1×2 0 = 1 10 10 2 = 1×2 1 + 0×2 0 = 2 10 100 2 = 1×2 2 + 0×2 1 + 0×2 0 = 4 10 1000 2 = 1×2 3 + 0×2 2 + 0×2 1 + 0×2 0 = 8 10 1 0000 2 = 16 10 10 0000 2 = 32 10 100 0000 2 = 64 10 TRU-COMP1380 Number Systems12

13 1000 0000 2 = ??? 10 1 0000 0000 2 = ??? 10 10 0000 0000 2 = ??? 10 100 0000 0000 2 = ??? 10 1000 0000 0000 2 = ??? 10 1 0000 0000 0000 2 = ??? 10 Can you memorize the above powers of 2? Converting to decimals 1101 2 = ??? 10 1011 0010 2 = ??? 10 1011.0010 2 = ??? 10 TRU-COMP1380 Number Systems13

14 Converting Decimal to Binary QuotientRemainder 21 / 2 101 10 / 250 5 / 221 2 / 210 1 / 201 => 21 10 = 1 0101 2 271 10 = ??? 2 6071 10 = ??? 2 TRU-COMP1380 Number Systems14

15 Another similar idea 271 10 = ??? 2 256 271 = 256 + 15 = 1 0000 0000 2 + 15 8 15 = 8 + 7 = 1000 2 + 7 => 271 = 1 0000 0000 2 + 15 = 1 0000 0000 2 + 1000 2 + 7 = 1 0000 0000 2 + 1000 2 + 111 2 = 1 0000 1111 2 1271 10 = ??? 2 TRU-COMP1380 Number Systems15

16 Hexadecimal Number System 0 10 = 0000 2 = 0 16 = 0x0 1 10 = 0001 2 = 1 16 = 0x1 2 10 = 0010 2 = 2 16 = 0x2 3 10 = 0011 2 = 3 16 = 0x3 4 10 = 0100 2 = 4 16 = 0x4 5 10 = 0101 2 = 5 16 = 0x5 6 10 = 0110 2 = 6 16 = 0x6 7 10 = 0111 2 = 7 16 = 0x7 8 10 = 1000 2 = 8 16 = 0x8 9 10 = 1001 2 = 9 16 = 0x9 10 10 = 1010 2 = A 16 = 0xA 11 10 = 1011 2 = B 16 = 0xB 12 10 = 1100 2 = C 16 = 0xC 13 10 = 1101 2 = D 16 = 0xD 14 10 = 1110 2 = E 16 = 0xE 15 10 = 1111 2 = F 16 = 0xF 148 16 = ??? 10 148 16 = ??? 2 23c9d6ef = ??? 10 23c9d6ef = ??? 2 TRU-COMP1380 Number Systems16 4 bits can be used for a hexadecimal number, 0,..., F. Please memorize it!

17 Converting Decimal to Hexadecimal QuotientRemainder 328 / 16 = 20 × 16 + 8 20 / 16 = 1 × 16 + 4 1 / 16 = 0 × 16 + 1 => 328 10 = 148 16 = ??? 2 148 16 = (1 × 16 2 + 4 × 16 1 + 8 × 16 0 ) 10 192 10 = ??? 16 TRU-COMP1380 Number Systems17

18 Converting Binary to Hexadecimal 4DA9 16 = ??? 2 100110110101001 2 = ??? 16 = 100 1101 1010 1001 = 4DA9 10 1110 2 = 0x??? = ??? 10 0100 1110 1011 1001 0100 2 = 0x??? = ??? 10 TRU-COMP1380 Number Systems18

19 TRU-COMP2130 Data Representation19 Format specifiers in printf() for hexadecimal and decimal numbers? for (i = 0; i < num; i++) printf(“%d = 0x%x\n”, data[i], data[i]); But there is no printf format specifier to print an integer in the binary form. Write a function to make a string of 0’s and 1’s for a given integer.

20 TRU-COMP2130 Data Representation20 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?

21 TRU-COMP2130 Data Representation21 Data Sizes printf(“%lu\n”, sizeof(long)); //lu: long unsigned // integer C data type32-bit64-bit char 11 short 22 int 44 long 48 88 char* 48 float 44 double 88

22 TRU-COMP2130 Data Representation22 Addressing and Byte Ordering A variable x of type int Address of x : 0x100 This means the 4 bytes of x would be stored in memory locations 0x100, 0x101, 0x102, and 0x103. How to interpret the bytes in memory locations 0x100, 0x101, 0x102, and 0x103? Let’s assume x has 0x1234567. 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 0x100 address 0x01 0x23 0x45 0x67 value Big endian byte order – machines from IBM and Sun Microsystems 0x103 0x102 0x101 0x100 0x67 0x45 0x23 0x01

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

24 TRU-COMP2130 Data Representation24 Representing Strings A string is encoded by an array of characters terminated by the null (having 0) character ‘\0’; The ASCII character set Unicode – Some libraries are available for C.

25 TRU-COMP2130 Data Representation25 Representing Code Different machine types use different and incompatible instructions and encodings. Even identical processors running different OSes have differences in their coding conventions and hence are no binary compatible.

26 TRU-COMP2130 Data Representation26 Comparison and Logical Operations Comparison operators ??? Logical operators ???

27 TRU-COMP2130 Data Representation27 Bit-Level Operations ??? &, |, ~, ^, >>, << >>: Logical right shift and arithmetic right shift. Logical right shit for unsigned integers: filled with 0 Arithmetic right shit for signed integers: filled with the MSB Examples char x = -128; unsigned char y = 128; x = x >> 1; y = y >> 1; printf (“%d, %d\n”, x, y); We will discuss about this example later again. Some examples a ^ a = ???a ^ 0 = ??? x = 10; y = 20; y = x ^ y; x = x ^ y; y = x ^ y;

28 TRU-COMP2130 Data Representation28 Some more examples 11110000 & 11001100 = ??? 11110000 | 11001100 = ??? 11110000 ^ 11001100 = ??? 0x3B & 0x33 = ??? 0x3B | 0x33 = ??? 0x3B ^ 0x33 = ??? 0x3B >> 2 = ??? 0x33 << 2 = ??? Consult with programming assignments

29 TRU-COMP2130 Data Representation29 2. Integer Representations CJavaSize char, unsigned charbyte1B short, unsigned shortshort2Bs char in Java uses 2Bs. int, unsigned intint4Bs long, unsigned longlong8Bs // there is no unsigned in Java floatfloat4Bs doubledouble8Bs

30 Unsigned Encodings unsigned charAll 8 bits are used. No sign bit. The smallest number is 0 The maximum number is 0xff. unsigned short16 bits The smallest number is ??? The maximum number is ??? unsigned int32 bits The smallest number is ??? The maximum number is ??? unsigned long64 bits The smallest number is ??? The maximum number is ??? TRU-COMP1380 Data Representations30

31 Representation of Unsigned Integers 8-bit representation of unsigned char 25511111111 25411111110...... 12810000000 12701111111 12601111110...... 200000010 100000001 000000000 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 ??? TRU-COMP1380 Data Representations31 +1 overflow

32 TRU-COMP2130 Data Representation32 unsigned charx, y; x = 128; y = 128; printf(“x = %d, y = %d\n”, x, y); printf(“x + y = %d\n”, x + y); // int (not char) addition x = x + y; // 256 -> 100000000 -> truncation printf(“x = %d, y = %d\n”, x, y); X = 128; x = x >> 1; // logical right shift for unsigned printf(“x = %d\n”, x); The output is ??? 128, 1282560, 12864 16-bit, 32-bit, 64-bit representations have the same overflow problem. How to represent signed integers?

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., 10010100101111 + 1001 + 1011 + 1 1101111101 ??? 10111 + 111 ??? carry TRU-COMP1380 Data Representations33

34 Binary Subtraction How to subtract a binary number? Just like the subtraction of decimal numbers. E.g., 0112 02 02 1000 10 10 10010 10010 10010 -1 -1 -11 -11 -11 1 ?1 ?11 1111 Try: 101010 How to do? 1 -101 -10 TRU-COMP1380 Data Representations34

35 In the previous slide, 10010 – 11 = 1111 What if we add 00010010 + 11111100 1 00001110 + 1 00001111 Is there any relationship between 11 2 and 11100 2 ? The 1’s complement of 11 2 is ???Switching 0  1 This type of addition is called 1’s complement addition. Find the 8-bit one’s complements of the followings. 11011 ->00011011 -> 10 ->00000010 -> 101 ->00000101 -> TRU-COMP1380 Data Representations35

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

37 Another example 101010 - 101 ??? What if we use 1’s complement addition or 2’s complement addition instead as follow? Let’s use 8-bit representation. 00101010 00101010 + 11111010 + 11111011 1 00100100 1 00100101 + 1 00100101 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-COMP1380 Data Representations37

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

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? 00001001 How about -9? 10001001 is really okay? 00001001 (9) + 10001001 (-9) = 10010010 (-18) It is wrong! We need a different representation for negative integers. TRU-COMP1380 Data Representations39

40 How about -9? 10001001 is really okay? 00001001 (9) + 10001001 (-9) = 10010010 (-18) It is wrong! We need a different representation for negative integers. What is the 8-bit 1’s complement of 9? 11110110<- 8-bit 1’s complement of 9 00001001 + 11110110 <- 9 + 8-bit 1’s complement of 9 = 11111111<- Is it zero? (1’s complement of 0?) What is the 2’s complement of 9? 11110111<- 8-bit 2’s complement of 9 00001001 + 11110111 <- 9 + 8-bit 2’s complement of 9 = 1 00000000<- It looks like zero. 2’s complement representation is used for negative integers. TRU-COMP1380 Data Representations40

41 1 2 – 10 2 = 1 2 + (–10 2 ) ??? What is the result in decimal? 00000001 + 11111110 <- 2’s complement of 10, i.e., -10 2 11111111 <- 2’s complement of 1, i.e., -1 (= 1 – 2) 101010 2 – 101 2 = 101010 2 + (–101 2 ) ??? 10010 2 – 11 2 ??? 10 2 – 1 2 ??? -10 2 – 1 2 ??? 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 111111111111001110000001 TRU-COMP1380 Data Representations41

42 8-bit representation of signed char with 2’s complement 12701111111 12601111110...... 200000010 100000001 000000000 -111111111 -211111110 -311111101...... -12710000001 -12810000000 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 ??? TRU-COMP1380 Data Representations42 +1 overflow

43 16-bit representation signed short with 2’s complement...01111111 11111111...01111111 11111110... 300000000 00000011 200000000 00000010 100000000 00000001 000000000 00000000 -111111111 11111111 -211111111 11111110 -311111111 11111101......10000000 00000001...10000000 00000000 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-COMP1380 Data Representations43 +1 overflow +1

44 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 char8-bits short,unsigned short16-bits int,unsigned int32-bits long,unsigned long64-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-COMP1380 Data Representations44

45 TRU-COMP2130 Data Representation45 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. 12701111111 12601111110...... 200000010 100000001 000000000 -111111111 -211111110 -311111101 2’s complement of -3 is 00000011, i.e., 3....... -12710000001 -12810000000 2’s complement of -128 is 10000000 again. For any -127 < x < 127, x – x = 0. But (-128) – (-128) = ???

46 TRU-COMP2130 Data Representation46 charx, y; x = 128;// 128 -> 10000000 This is -128. y = 128; printf(“x = %d, y = %d\n”, x, y); printf(“x – y = %d\n”, x - y); x = x – y; printf(“x = %d, y = %d\n”, x, y); x = 128; x = x >> 1; // arithmetic shift for signed printf(“x = %d\n”, x); The output is ??? -128, -12800, -128-64 -128 – (-128) = -128 + (-(-128)) = -128 + (-128) = 10000000 + 10000000 = 00000000 = 0

47 TRU-COMP2130 Data Representation47 chara = 127, b = 127, c; unsigned chard; c = a + b; d = a + b; printf(“a=%d, b=%d, c=%d, d=%d\n”, a, b, c, d); The output is ??? a=127, b=127, c=-2, d=254 01111111 + 01111111 = 11111110 ??? We have to be very careful with overflow for integer values.

48 Advice on Signed vs. Unsigned Practice Problem 2.25 float sum_elements (float a[], unsigned int length) { inti; floatresult = 0; for (i = 0; i <= length-1; i++) result += a[i]; return result; } When run with length equal to 0, this code should return 0.0. Instead it encounters a memory error. Why??? unsigned int lengthlength-1 How to fix this code? TRU-COMP2130 Data Representations48

49 short x = -5; unsigned short y = 128; printf(“%x, %x\n”, x, y); printf(“%x, %x, %x, %x\n”, x >3, y >3); The output is ??? fffffffb, 80ffffffd8, ffffffff, 400, 10 Singed integers: Arithmetic right shift: Left part is filled with the most significant bit of the original value. Unsigned integers: Logical right shift: Left part is filled with 0. TRU-COMP2130 Data Representations49

50 Practice Problem 2.26 size_t strlen(const char* s);// defined in string.h int strlonger(char s[], char t[]) { return strlen(s) – strlen(t) > 0; } Note that size_t is defined in stdio.h to be unsigned int. For what cases will this function produce an incorrect result? 0 – 1 > 0->1 (true value) Explain how this incorrect result comes about. Unsigned int of 0 – 1 is 0xffffffff that is greater than 0. Show how to fix the code so that it will work reliably. TRU-COMP2130 Data Representations50

51 TRU-COMP2130 Data Representation51 Conversions Between Signed and Unsigned shortv = -12345; unsigned shortuv = (unsigned short)v; printf(“v=%x, v=%d, uv=%x, uv=%u\n”, v, v, uv, uv); The output is v=ffffcfc7, v=-12345, uv=cfc7, uv=53191 (12*16 3 +15*16 2 +12*16+7) What if we convert an unsigned integer to signed integer? shortv = -12345, w; unsigned shortuv = v; // implicit type casting w = uv; printf(“v = %d, uv = %u, w = %d\n”, v, uv, w); The output is v = -12345, uv = 53191, w = -12345

52 TRU-COMP2130 Data Representation52 Truncating Numbers intx = 53191; // 0xcfc7: 0...0 11001111 11000111 shortsx = (short)x; // -12345 inty = sx; // ??? printf(“x = %d, sx = %d, y = %d\n”, x, sx, y); chara = 128, b = 128; x = a + b; // int (not short) addition y = a – b; // int (not short) subtraction printf(“%d, %d, %d, %d\n”, a, b, x, y); The output is ??? 53191, -12345, -12345-128, -128, -256, 0

53 TRU-COMP2130 Data Representation53 3. Integer Arithmetic What we have discussed Unsigned addition Two’s complement addition Two’s complement negation Overflow problems

54 TRU-COMP2130 Data Representation54 Unsigned Multiplication E.g., 1001101 × 1 = ??? 1001101 × 10 = ??? 1001101 × 100 = ??? 1001101 × 101 = 1001101 × 10 × 10 + 1001101 × 0 + 1001101 × 1 = 100110100 + 1001101 = 110000001 Unsigned multiplication x  y mod 2 w -> truncation of the bits above w bits Two’s complement multiplication for signed integers U2T w (x  y mod 2 w ), where U2T means unsigned to two’s complement

55 TRU-COMP2130 Data Representation55 For example, w = 3, i.e., 3-bit representation, and 2’s complement for signed integers: Modexyx * y Unsigned 5 [101]3 [011]7 [111] Signed -3 [101]3 [011]-1 [111] Unsigned 4 [100]7 [111]? [???] Signed ? [100]? [111]? [???] Unsigned 3 [011] ? [???] Signed ? [011] ? [???]

56 TRU-COMP2130 Data Representation56 Multiplying by Constants What if we shift 1001 left by one bit? 1001 -> 10010 9 -> ?? What if we shift 1001 left by two bits? 1001 -> 100100 9 -> ?? Multiplication by a power of 2.

57 TRU-COMP2130 Data Representation57 Dividing by Powers of Two What if we shift 100100 right by one bit? 00100100 -> 00010010 ?? -> ? What if we shift 100100 right by two bits? 00100100 -> 00001001 ?? -> ? Division by a power of 2. Need to think logical right shift for unsigned integers as shown in the above example, and arithmetic right shift for signed integers.

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

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

60 TRU-COMP2130 Data Representation60 2.4 Floating Point Fractional binary numbers IEEE floating-point representation

61 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) TRU-COMP1380 Number Systems61 00101000.101 (40.625) +11111110.110 (-1.25) (2’s complement) 00100111.011 (39.375) 2 -1 = 0.5 2 -2 = 0.25 2 -3 = 0.125 No new operations -- same as integer arithmetic.

62 Just for your curiosity: How to convert decimal fractions to binary? 0.625 10 = ??? 2 0.625 * 2 = 1.25 -> 1(0.625 = x 2 -1 + y 2 -2 + z 2 -3 +...) 0.25 * 2 = 0.5 -> 0(1.25 = x + y 2 -1 + z 2 -2 +... => x = 1) 0.5 * 2 = 1.0 -> 1(0.25 = y 2 -1 + z 2 -2 +... ) Therefore 0.101 0.7 10 = ??? 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 0.4 * 2 = 0.8 -> 0 0.8 * 2 = 1.6 -> 1... Therefore 0.1011001... TRU-COMP1380 Number Systems62 How to deal with big numbers and small numbers?

63 Very Large and Very Small: Floating-Point Large values: 6.023 × 10 23 -- requires 79 bits Small values: 6.626 × 10 -34 -- requires > 110 bits Use equivalent of “scientific notation”: F × 2 E Need to represent F (fraction), E (exponent), and sign. 6.023 × 10 23 = 0.6023 × 10 24 6.626 × 10 -34 = 0.6626 × 10 -33 00101000.101 (40.625 10 ) = 0.101000101 × 2 6. Store 6 in the exponent and 101000101 in mantissa. (Try the multiplication) IEEE 754 Floating-Point Standard (32-bits): TRU-COMP1380 Number Systems63 SExponentFraction (mantissa) 1b 8 bits 23 bits

64 Floating Point in C Two different floating-point data types float double Type casting int -> floatThe value may be rounded. float -> doubleThe exact value can be preserved. double -> floatThe value may be rounded. float, double -> intThe value will be rounded toward zero. E.g., 1.999 -> 1; -1.999 -> -1 TRU-COMP1380 Number Systems64


Download ppt "Data Representation Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University."

Similar presentations


Ads by Google