Two’s Complement 1.As an action: (Assume the starting value is 1011) 1.Flip the bits from the starting value => Add one to get the answer => 0101
Signed Integer MSB indicates sign: 0 is positive; 1is negative 1XXX XXXX 0XXX XXXX Two’s-Complement A – B = A + ( -B ) 1 – 1 = 1 + ( -1 ) Starting Value (+1 in decimal) Step 1: Reverse the bits Step 2: Add 1 to the value from step Sum:Two’s-complement representation (-1 in decimal) ( -1 )(1)
Two’s Complement/Signed Binary to Decimal 1.As representation of a number 1.If MSB, the left most bit, is a 0, you can convert it to decimals as if it were an unsigned binary integer. (The number represented is simply the bits’ value as a base 2 number.) This is positive or zero => 16 (decimal) 0101 => 5 (decimal) 0000 => 0 (deciml) (0000=>1111=> =>0000=>0; NOT 10000; the extra 1 does not fit in the available four bits)
Two’s Complement/Signed Binary to Decimal 2.As representation of a number If MSB, the left most bit, is a 1, the number represented is negative. To find its magnitude, we take the two’s complement of the bits (flip them and add 1) and interpret the result as a base 2 number. Because the original integer is negative, we add “-”, negative sign, to its decimal value. Starting value1110 Step 1: Reverse the bits0001 Step 2: Add 1 to the value from Step 1 1 Step 3: Create the two’s complement0010 Step 4: Convert to decimal 2 Step 5: Since the original value is negative, report its negation. -2
Signed Integer MSB indicates sign: 0 is positive; 1is negative 1XXX XXXX 0XXX XXXX Two’s-Complement A – B = A + ( -B ) 1 – 1 = 1 + ( -1 ) Starting Value (+1 in decimal) Step 1: Reverse the bits Step 2: Add 1 to the value from step Sum:Two’s-complement representation (-1 in decimal) ( -1 )(1)
Two’s-complement of Hexadecimal Hexadecimal starting value 6 A 3 D Binary Bits Step1: Reverse the bits Step2: Add 1 to the value from Step Sum: Two’s-complement Two’s-complement of Hexadecimal 9 5 C 3 Hexadecimal starting value 9 5 C 3 Binary Bits Step1: Reverse the bits Step2: Add 1 to the value from Step Sum: Two’s-complement Two’s-complement of Hexadecimal 6 A 3 D
Signed Integer +10(decimal): Reverse the bits: Add 1: (decimal): =10+(-10) (10) (-10) (1) (0)
Signed Decimal to Binary 1.Convert the absolute value of the decimal to binary For example, 43 and -43 in decimal Represent 43 in binary Absolute value of the decimal integer | 43| = 43 Binary: If the decimal integer is negative, (Assume the starting value is -43) Absolute value of the decimal integer | -43| = 43 Binary: Create two’s complement => => +1 =>
Signed Decimal to Hexadecimal 1.Convert the absolute value of the decimal to hexadecimal 2.Represent +32 in binary (Assume the starting value is +32.) Absolute value of the decimal integer | +32 | = 32 Binary: Hexadecimal: If the decimal integer is negative, Create two’s complement => => +1 => (E0) Reverse the bits, Add 1, Represent the resulting binary in hex, E0
Signed Hexadecimal to Decimal 1.If the hexadecimal is positive, retain the integer as is. 2.Represent +20(Hexdecimal) in binary (Assume the starting value is +20 in hex.) Binary: Decimal: 32 3.If the decimal integer is negative, -20 in hexadecimal 4.Binary of -32 (decimal): (see previous slide) 5.Create two’s complement => => +1 => (E0) Reverse the bits, => Add 1, => Decimal: 32 Attach a minus sign to the beginning of the decimal integer, -32.
Ranges of Integer Values TypeMinimum and Maximum unsigned short0 to signed short–32768 to unsigned long0 to signed long– to