100 likes | 233 Views
Review Two’s complement. Negate a Binary Number in 2C. Negation means to find the corresponding negative number with the same absolute value (in decimal the Negation of 10 is –10)
E N D
Negate a Binary Number in 2C • Negation means to find the corresponding negative number with the same absolute value (in decimal the Negation of 10 is –10) • The method is the same whether you negate a positive to a negative or vice versa and doing it twice should give you the same result: -(-a) = a • Method: • Invert the binary number (“flip all bits”) Inverse (01001101) = 10110010 • Add to the result binary 1 (append 0’s on the left) 10110010 + 1=10110010+00000001=10110011
Convert Decimal to 2C • Decimal to Binary (word length k = 8): • If positive: Fraction number into a sum of powers of 2 bin(13)=8+4+1=23+22+20 = 00001101 • If Negative: Fraction the negated number and negate binary number (invert and add 1) bin(-13) = Negate(bin(13)) = Negate(00001101) = Invert(00001101)+00000001=11110010+00000001 = 11110011
Convert 2C to Decimal • Binary to Decimal (word length k=8): • If most significant bit (left most) is 0 the number is positive: Sum the powers of 2 Dec(00101101)=0*27 +0*26+1*25 +0*24 +1*23 +1*22 +0*21 +1*20 =1*128+0*64+1*32+0*16+1*8+1*4+0*2+1*0 =173 • If most significant bit (left most) is 1 the number is negative, that means you can not just add the powers of 2 • Find the decimal value of the negated binary number and negate it back in decimal (putting a – in front) Dec(10101101) = - (Dec(Negate(10101101)) = - (Dec(Invert(10101101)+0000001)) = - (Dec(01010010+0000001)) = - (Dec(01010011)) - now it is a positive number and we can sum the powers of 2! = -(64+16+2+1) = - 83
Operations on 2C • Addition: bitwise including carry as in decimal • Negation: Invert and add 1 • Subtraction: Addition of Negated operand: A – B =A + (– B) • Multiplication by 2: Shift left 00101101 * 2 = 01011010 • Division by 2: Shift right 00101101 ÷ 2 = 00010110
Issues: Binary Numbers • Signed Extension: • Sometimes we are lazy and don’t write out all bits. In 2C you can only add 0’s to the left if the number is positive, otherwise you make a negative number positive: -3 = 1101 for k=4 = 11111101 for k=8 • For negative number you have to add 1’sto the left! • Overflow: • If the result of an addition/subtraction is bigger than the biggest or smaller than the smallest possible number of word length k • Can never happen if you add a positive and a negative number
Hexadecimal Numbers • Same principle as decimal and binary but with a base of 16 • Number has a little x to the side: xF5 • Motivation: Word size is either 32 or 64, that are very long binary numbers, in Hex you can save space: • Conversion from Hex to binary is simple in blocks of 4(this works because 16=24): x5B2F = 0101 1011 0010 1111 To decimal (if positive): = 5*163 + 11*162 + 2*161 + 15*160 = 20480 + 2816 + 32 + 15 = 23343
LOGICAL OPERATORS AND: is only 1 if all inputs are 1 OR: is 1 if any of the inputs is 1 XOR: is 1 if either input is 1 NOT: is the inverse (“flip the bit”)
Logic on Words and Multiple Input 00101101 AND 11101010 00101000 00101101 10011000 AND 11101010 00001000 00101101 OR 11101010 11101111 00101101 10011000 OR 11101010 11111111
Text Data: ASCII Code • ASCII is a 7 bit encoding scheme for all keyboard symbols and some additional ones • Digits: x30 to x39 • Uppercase: x41 to x5A • Lowercase: x61 to x7A • Conversion: Table • Question: How can you mathematically convert a lowercase letter into uppercase?