Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compsci 210 Tutorial Two CompSci 210 - Semester Two 2016.

Similar presentations


Presentation on theme: "Compsci 210 Tutorial Two CompSci 210 - Semester Two 2016."— Presentation transcript:

1 Compsci 210 Tutorial Two CompSci Semester Two 2016

2 Tutorials Tutorials are not compulsory but are strongly recommended.
Tutorials try to enhance your understanding of content from lecture and apply the content based on examples and exercises. Tutorials only cover the content from the lecture. Please feel free to contact us if you have any problem. Tutorials materials can be found at Canvas:

3 Tutors https://canvas.auckland.ac.nz/courses/1193/assignments/syllabus
 Josh Hill          Room:   4th Floor Lunch Area Computer Science Building      Office hours: Monday & Wednesday  Ben Wang          Room:  Research Room, the corner of 4th Floor, Computer Science Building      Office hours: Tuesday & Thursday

4 Agenda Quick review last tutorial Binary-decimal conversion - easy
Conversion Between Hex and Binary – easy Float point – intermediate (just mention a little today)

5 value1 ∗ 𝑠𝑐𝑎𝑙𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑜𝑛1 * + value2 * 𝑠𝑐𝑎𝑙𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑜𝑛2 + …
decimal and binary value1 ∗ 𝑠𝑐𝑎𝑙𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑜𝑛1 * + value2 * 𝑠𝑐𝑎𝑙𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑜𝑛2 + … The decimal number “210” 2 ∗ ∗ ∗ = 210 the binary number “101” 1∗ ∗ ∗ 2 0 =5

6 Calculate negative number
Most Significant(MS) bit is sign bit Range of an n-bit number: - 2 𝑛−1 through 2 𝑛−1 – 1 Keep negative zero in your mind

7 Calculate negative number
the 2’s complement number “ ” This number is negative, because the leftmost bit is 1 The complement of the other bits is, → = “ ” represents a regular binary number, so, 1⋅64+1⋅32+1⋅16+0⋅8+0⋅4+1⋅2+1⋅1=115 Remember: The number is negative, so the final value is, −115

8 Decimal to Binary Conversion(1)

9 Decimal to Binary Conversion(2)
CompSci Semester One 2016

10 Exercise 1.2 Convert the following decimal numbers into 8-bit 2’s complement binary numbers 𝟏𝟎𝟐 64 33 −125 127

11 SOLUTION 1.2.A Decimal Value: / 2 = 51 R 0 51 / 2 = 25 R 1 25 / 2 = 12 R 1 12 / 2 = 6 R 0 6 / 2 = 3 R 0 3 / 2 = 1 R 1 1 / 2 = 0 R 1 0 / 2 = 0 R 0

12 SOLUTION 1.2.A Decimal Value: / 2 = 51 R 0 51 / 2 = 25 R 1 25 / 2 = 12 R 1 12 / 2 = 6 R 0 6 / 2 = 3 R 0 3 / 2 = 1 R 1 1 / 2 = 0 R 1 0 / 2 = 0 R 0 Answer:

13 SOLUTION 1.2.D Decimal Value: / 2 = 62 R 1 62 / 2 = 31 R 0 31 / 2 = 15 R 1 15 / 2 = 7 R 1 7 / 2 = 3 R 1 3 / 2 = 1 R 1 1 / 2 = 0 R 1 0 / 2 = 0 R 0

14 SOLUTION 1.2.D Decimal Value: / 2 = 62 R 1 62 / 2 = 31 R 0 31 / 2 = 15 R 1 15 / 2 = 7 R 1 7 / 2 = 3 R 1 3 / 2 = 1 R 1 1 / 2 = 0 R 1 0 / 2 = 0 R 0 Calculate 2’s complement: → Answer:

15 Exercise 1.2 Solutions 102 64 33 −125 127 Now we’ll solve them all using the process from the previous slides

16 Exercise 1.2 Solutions 102 2 =51r0 0 64 2 =32r0 0 33 2 =16r1 1
Step 1: Divide by 2 and write down the remainder to the side

17 Exercise 1.2 Solutions 51 2 =25r1 10 32 2 =16r0 00 16 2 =8r0 01
Step 2: Divide again and write the new remainder to the left

18 Exercise 1.2 Solutions 25 2 =12r1 110 16 2 =8r0 000 8 2 =4r0 001
Step 3: Repeat CompSci Semester One 2016

19 Exercise 1.2 Solutions 12 2 =6r0 0110 8 2 =4r0 0000 4 2 =2r0 0001
Step 3: Repeat CompSci Semester One 2016

20 Exercise 1.2 Solutions 6 2 =3r0 00110 4 2 =2r0 00000 2 2 =1r0 00001
Step 3: Repeat CompSci Semester One 2016

21 Exercise 1.2 Solutions 3 2 =1r1 100110 2 2 =1r0 000000 1 2 =0r1 100001
Step 3: Repeat CompSci Semester One 2016

22 Exercise 1.2 Solutions 1 2 =0r1 1100110 1 2 =0r1 1000000
Step 3: Repeat CompSci Semester One 2016

23 Exercise 1.2 Solutions 0 2 =0r0 01100110 0 2 =0r0 01000000
Step 3: Repeat. When the quotient and remainder are both zero, stop. CompSci Semester One 2016

24 Exercise 1.2 Solutions 0 2 =0r0 01100110 0 2 =0r0 01000000
Step 4: Take the 2’s complement of any negative numbers CompSci Semester One 2016

25 Exercise 1.2 Solutions 102 = 01100110 64 = 01000000 33 = 00100001
102 = 64 = 33 = -125 = 127 = Done CompSci Semester One 2016

26 Conversion Between Hex and Binary
Converting between hexadecimal and binary is easy! Recall that hexadecimal uses the normal digits 0-9, plus the letters A-F to represent the values A B C D E F position is expensive for human being (read/write/check) But sometime we want to express these machine level number directly (eg. memory address, Mac address, color code) CompSci Semester One 2016

27 Conversion Between Hex and Binary
Every hexadecimal digit can be converted into a 4 digit binary number. Examples: 0 16 = (0000) 2 3 16 = (0011) 2 8 16 = (1000) 2 B 16 = (1011) 2 F 16 = (1111) 2 CompSci Semester One 2016

28 Exercise 1.3 Convert the following 2’s complement binary numbers to hexadecimal 010 CompSci Semester One 2016

29 Solution 1.3.c Split the number into groups of four, starting on the right Calculate the hexadecimal value for each group 5 D 1 4 Write the solution with the correct sign 0x5D14 (‘0x’ is often used to indicate a hex value) CompSci Semester One 2016

30 Exercise 1.3 Solutions 010 Now we’ll solve them all using the process from the previous slides CompSci Semester One 2016

31 Exercise 1.3 Solutions 010 Step 1: Split into groups of 4, starting from the right CompSci Semester One 2016

32 Exercise 1.3 Solutions 0010 Step 2: Pad the leftmost group with zeros if needed CompSci Semester One 2016

33 Exercise 1.3 Solutions 2 9 B 5 D 1 4 F F 5 1
Step 3: Convert each group to hex CompSci Semester One 2016

34 Exercise 1.3 Solutions 0x2 0x9B 0x5D14 0xFF51 Step 4: final answer
CompSci Semester One 2016

35 Exercise 1.4 Convert the following hexadecimal numbers to binary 0xD
0xFE7F CompSci Semester One 2016

36 Exercise 1.4 Solutions 0xD 0x6E 0x8001 0xFE7F
CompSci Semester One 2016

37 Exercise 1.4 Solutions D 6 E 8 0 0 1 F E 7 F
Step 1: Split up the hex digits CompSci Semester One 2016

38 Exercise 1.4 Solutions 1101 Step 2: Convert the hex digits into 4 digit binary values CompSci Semester One 2016

39 Bits and bytes Bits = positions of binary What is byte? Why 8 bits?
byte (/ˈbaɪt/) is a unit of digital information that most commonly consists of 8 bits Why 8 bits? powers of 2 are magic How many keys are on a typewriter keyboard include shift? ASCII defined a 7-bit character set. And one extra bit that has been used for all sorts of things One byte is enough for most common characters.

40 text

41 ASCII Codes ASCII stands for American Standard Code For Information Interchange. Each key on the keyboard is identified by its unique ASCII code When you type a key on the keyboard, the corresponding eight-bit code is stored and made available to the computer Most keys are associated with more than one code, for example, h and H have two different codes Google search “ASCII Table” and look at the image results for quick reference CompSci Semester One 2016

42 Why float point is important
The basic idea: can we change the position without changing the value? scientific notation: => x 10 2 F ∙10 𝐸 (decimal) F ∙2 𝐸 (binary) F is the float point number

43 Significant figures

44 Float point Decimal float point
∙∙∙ (𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑝𝑜𝑖𝑛𝑡) − − −3 ∙∙∙ ∙∙∙ (𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑝𝑜𝑖𝑛𝑡) 𝟏 𝟏𝟎 + 𝟏 𝟏𝟎𝟎 + 𝟏 𝟏𝟎𝟎𝟎 Binary float point ∙∙∙ (𝑏𝑖𝑛𝑎𝑟𝑦 𝑝𝑜𝑖𝑛𝑡) − − −3 ∙∙∙ ∙∙∙ (𝑏𝑖𝑛𝑎𝑟𝑦 𝑝𝑜𝑖𝑛𝑡) 𝟏 𝟐 + 𝟏 𝟒 + 𝟏 𝟖

45 IEEE 754 Floating Point (32-bit)

46 IEEE 754 Floating Point (32-bit)
Sign Bit Fraction (significand) Exponent Exponent for range; Fraction for accuracy

47

48 IEEE 754 Floating Point (32-bit)
Exponent Unsigned, but biased by 127 Special cases override fraction value = 0 = infinity Example: Calculate binary value = =128+32=160 Apply bias 160−127=33

49 IEEE 754 Floating Point (32-bit)
Fraction 23-bits Represents the fractional part of a number between 1 and 2 (Implied 1 in front) Example: Prepend a binary point Add 1 CompSci Semester One 2016

50 IEEE 754 Floating Point (32-bit)
= -5.0

51 Exercise 1.5 Convert − to IEEE single-precision floating point Convert 𝐼𝐸𝐸𝐸 to decimal CompSci Semester One 2016

52 Exercise 1.5.a solution − Convert to binary (see next slide) = Move decimal point to compute exponent and fraction = ⋅ 2 6 Add bias to exponent; convert to binary 6+127= = Assemble components CompSci Semester One 2016

53 Converting fractional numbers
Decimal Value: / 2 = 56 R 0 56 / 2 = 28 R 0 28 / 2 = 14 R 0 14 / 2 = 7 R 0 7 / 2 = 3 R 1 3 / 2 = 1 R 1 1 / 2 = 0 R 1 0 / 2 = 0 R 0 Decimal Value: * 2 = * 2 = * 2 = * 2 = * 2 = = CompSci Semester One 2016

54 Exercise 1.5.B solution 𝐼𝐸𝐸𝐸 Calculate biased exponent = =128+4=132 Subtract bias from value 132−127=5 Place 1 in front of the fraction and multiply by the exponent value ⋅ 2 5 = convert to decimal = −1 =42.5 CompSci Semester One 2016


Download ppt "Compsci 210 Tutorial Two CompSci 210 - Semester Two 2016."

Similar presentations


Ads by Google