Number Representation Tutorial One Number Representation CompSci 210 - Semester One 2017
Introductions Who am I: Callan Christophersen Contact: cchr158@aucklanduni.ac.nz Office hours: Monday 10am, Friday 11am. Where to find me: outside the games lab on the 4th floor. Take south side lifts to 4th floor, then turn left. CompSci 210 - Semester One 2017
Value of a Decimal Number Examine the decimal number “210” We reflexively understand the value to be, 200+10+0=210 This is because, 2⋅100+1⋅10+0⋅1=210 We use 100, 10, and 1 because we are told the number is a decimal number and decimal numbers use base 10 100= 10 2 ,10= 10 1 , 1= 10 0 Remember: “210” does not have a value until we calculate it CompSci 210 - Semester One 2017
Value of a Binary Number Now examine the binary number “101” Since we probably don’t reflexively understand the value, we must work backwards We use 4, 2, and 1 because we are told the number is a binary number and binary numbers use base 2 2 2 =4, 2 1 ,=2, 2 0 =1 In the formula, this becomes, 1⋅4+0⋅2+1⋅1=5 Note: We usually perform our calculations using decimal numbers CompSci 210 - Semester One 2017
Value of a 2’s Complement Number Now examine the 2’s complement number “10001101” 2’s complement numbers include a sign bit, and negative numbers are represented by their complementary bits This number is negative, because the leftmost bit is 1 The complement of the other bits is, 0001101→1110010+1=1110011 “1110011” represents a regular binary number, so, 2 6 =64, 2 5 =32,…, 2 0 =1 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 CompSci 210 - Semester One 2017
Decimal to Binary Conversion CompSci 210 - Semester One 2017
Convert 102 to binary Decimal Value: 102 102 / 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 CompSci 210 - Semester One 2017
Convert 102 to binary Decimal Value: 102 102 / 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 0 1 1 0 0 1 1 0 Answer: 01100110 CompSci 210 - Semester One 2017
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 10-15. 0 1 2 3 4 5 6 7 8 9 A B C D E F CompSci 210 - Semester One 2017
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 210 - Semester One 2017
Convert hex to binary 0101110100010100 Split the number into groups of four, starting on the right 0101 1101 0001 0100 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 210 - Semester One 2017
Ands & Ors Oring 2 bits E.g. 1 OR 0 = 1 A B A OR B 0 0 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 1 1 1 1 Anding 2 bits E.g. 1 and 0 = 0 A B A&B 0 0 0 0 1 0 1 0 0 1 1 1 Anding binary strings This is done bitwise 11001010111 and 00100101011 00000000011 ORing binary strings This is also done bitwise 11001010111 and 00100101011 11101111111 CompSci 210 - Semester One 2017
Binary multiplication – the silly way Lets use an example: 10 x 20 = 200 or 1010 x 10100 = 11001000 One way to calculate this is 10+10+10+10+…+10 = 200 Or you could use a better way… CompSci 210 - Semester One 2017
Binary multiplication – a better way Using the same example: 10 x 20 = 200 or 1010 x 10100 = 11001000 1010 10100 101000 1010000 10100000 101000000 000000000 000101000 011001000 CompSci 210 - Semester One 2017