Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Number Systems & Operations

Part II

4 2555

Arithmetic Operations with Signed Numbers


Because the 2s complement form for representing signed numbers is the most widely used in computer systems. Well limit to 2s complement arithmetic on:

Addition Subtraction Multiplication Division

Addition
4 cases that can occur when 2 signed numbers are added:

Both numbers positive Positive number with magnitude larger than negative number Negative number with magnitude larger than positive number Both numbers negative

Addition
Both numbers positive:

ex:

00000111! !

! ! ! +00000100! ! +4 ! ! ! 00001011! ! 11

The sum is positive and is therefore in true (uncomplemented) binary.

Addition
Positive number with magnitude larger than negative number:

ex:

00001111! !

15

! ! ! +11111010! ! + -6 ! ! 1
Discard carry

00001001! !

The nal carry bit is discarded. The sum is positive and is therefore in true (uncomplemented) binary.

Addition
Negative number with magnitude larger than positive number:

ex: ! ! ! ! !

00010000! ! +11101000! ! ! 11111000! !

16 + -24 -8

The sum is negative and therefore in 2s complement form.

Addition
Both numbers negative:

ex: ! ! ! ! !
Discard carry

11111011!!

-5

+11110111!! + -9 1 11110010! ! -14

The nal carry bit is discarded. The sum is negative and therefore in 2s complement form.

Addition
Remark:

The negative numbers are stored in 2s complement form so, as you can see, the addition process is very simple: Add the two numbers and discard any final carry bit.

Addition
Overow condition:

When two numbers are added and the number of bits required to represent the sum exceeds the number of bits in the two numbers, an overflow results as indicated by an incorrect sign bit. An overow can occur only when both numbers are + or -.

ex: 01111101 125 +00111010 + 58 10110111 183

Magnitude incorrect Sign incorrect

Addition
Numbers are added two at a time:

Computer add strings of numbers two numbers at a time.


add the signed numbers: 01000100, 00011011, 00001110, and 00010010 ! ! ! ! ! ! ! + + + 68!! 27!! 95!! 14!! 109!! 18!! 127!! 01000100 + 00011011! Add 1st two numbers 01011111! 1st sum + 00001110! Add 3rd number 01101101! 2nd sum + 00010010! Add 4th number 01111111! Final sum

ex: ! ! ! ! ! ! ! ! ! ! ! ! ! !

Subtraction
Subtraction is a special case of addition.

Subtraction is addition with the sign of the subtrahend changed. The result of a subtraction is called the difference.

The sign of a positive or negative binary is changed by taking its 2s complement.

Subtraction
Since subtraction is simply an addition with the sign of the subtrahend changed, the process is stated as follows:

To subtract two signed numbers, take the 2s complement of the subtrahend and add. Discard any nal carry.

Subtraction
ex: Perform each of the following subtraction of the signed numbers:
(a) 00001000 00000011!! (b) 00001100 11110111 (d) 10001000 - 11100010 12! + 9! 21 -120 + 30 -90

! (c) 11100111 00010011!!


! (a) 00001000! ! ! +11111101!+ 100000101! 8! -3! 5! -25! ! ! ! ! ! !

(b) 00001100! +00001001! 00010101! (d) 10001000 +00011110 10100110

! (c) 11100111! ! !

+11101101!+ -19! 111010100! -44!

Hexadecimal and Octal Numbers

Hexadecimal Numbers
We will call it for short as hex. It has 16 characters. Digits 0-9 and letters A-F. It used primarily as a compact way of displaying or writing binary numbers since it is very easy to convert between bin and hex.

Hex is widely used in computer and microprocessor applications.

Hexadecimal Numbers
Decimal
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Binary
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Hexadecimal
0 1 2 3 4 5 6 7 8 9 A B C D E F

Hexadecimal Numbers
If you see h mixing in numbers (in the context of computer systems), please note that its most likely that the numbers are hexadecimal numbers. (Be careful. h is not one of A-F using in hex).

For example 16h = 000101102 0Dh = 000011012

Hexadecimal Numbers
Bin-to-Hex Conversion

Simply break the binary number into 4-bit groups, starting at the right-most bit and replace each 4-bit group with the equivalent hex symbol.
(b) 111111000101101001 00111111000101101001

(a) 1100101001010111! ! 1100101001010111

!!

! ! !

7
!

!!
! !

= CA5716!

= 3F16916

Hexadecimal Numbers
Hex-to-Bin Conversion

Reverse the process (of bin-to-hex) and replace each hex symbol with the appropriate four bits.

ex: Determine the binary numbers for the following hex numbers:

(a) 10A4h! 1 0 A 4!

(b) CF8Eh!! C F 8 E!

(c) 9742h 9 7 4 2

0001000010100100! 1100111110001110

1001011101000010

Hexadecimal Numbers
Hex-to-Dec Conversion

2 methods: Hex-to-Bin rst and then Bin-to-Dec. Multiply the decimal values of each hex digits by its weight and then take the sum of these products.

Hexadecimal Numbers
Hex-to-Dec Conversion

Hex-to-Bin rst and then Bin-to-Dec

ex: Convert the following hex numbers to decimal: (a) 1Ch 1Ch = 00011100 = 16+8+4 = 2810 (b) A85h A85h = 101010000101 = 2048+512+128+4+1 = 269310

Hexadecimal Numbers
Hex-to-Dec Conversion

Multiply the decimal values of each hex digits by its weight and then take the sum of these products.

ex: Convert the following hex numbers to decimal: (a) E5h E5h = (Ex16)+(5x1) = (14x16)+5 = 224+5 = 22910 (b) B2F8h B2F8h = (Bx4096)+(2x256)+(Fx16)+(8x1) = (11x4096)+(2x256)+(15x16)+(8x1) ! ! = 45,056+512+240+8 = 45,81610

Hexadecimal Numbers
Dec-to-Hex conversion

Repeated division of a dec number by 16


650/16 = 40.625 0.625x16 = 10 = A

ex: Convert the dec number 650 to hex


LSD

40/16 = 2.5

0.5x16

=8 =8
Hence 65010 = 28Ah

2/16 = 0.125

0.125x16 = 2 = 2

MSD

Stop when whole number quotient is ZERO.

Octal Numbers
Like the hex, the oct provides a convenient way to express binary numbers and codes. (btw, its not as commonly used as hex). 8 digits: 0-7

0,1,2,3,4,5,6,7,10,11,12,13,14,15,16,17,20,

Operations we learn about hex so far work the same way on oct just mark this:

Hex = 4 binary bits Oct = 3 binary bits

Now, lets crack the following exercises

Octal Numbers
Bin-to-Oct Conversion (a) 110101! ! (b) 101111001 ! (c) 100110011010 ! (d) 11010000100 Oct-to-Bin Conversion (a) 138 (b) 258 (c) 1408 (d) 75268 Oct-to-Dec Conversion

(a) 23748
Dec-to-Oct Conversion (a) 35910

You might also like