EEE342 Digital Electronics Ian McCrumRoom 5B18, Lecture 2: Codes & Arithmetic
Binary Arithmetic - Addition So which is ‘0’ and carry ‘1’ If you end up adding three ones then you get 11, which is a ‘1’ and a carry ‘1’ Remember to carry when you get a 2, not a ten = =
Binary arithmetic - Subtraction 3 So can’t do so borrow ‘1’ 1 E.G 1010 = = I.e, read this as 1 from 0 won’t go so 1 from 2 is 1 and carry 1, then we have 1 from 1 which is 0 then 1 from 0, can’t go so borrow 1. 1 from 2 is 0 and carry 1, last subtraction is 1 from 1, which is = 5 10
Binary Multiplication *0*1*0 *
Long Binary multiplication As for decimal, you add a zero (equivalent to shifting left one digit position, but since you only multiply by ‘1’ or by zero it becomes an addition problem 10100= = * 101= = = =
Binary Division ÷0÷1÷0 ÷
Binary Long Division Beware the Internet! There are actually two ways of doing “division” described on the net The conventional way A simple way that gives the wrong answer BUT it is consistent. It is used in cryptography to protect binary data - a process known as adding a CRC. This is a strange thing, we will not deal with it here. In calculating CRC and other cryptographic things we throw the answer away – it is only the remainder that is used! (not on course…) COM181 Computer Hardware:IMcC7
For decimal long division we subtract the divisor or a multiple of it, for binary this becomes simpler, we subtract the divisor or all zeroes, / = divide by is 10 rem Binary Long Division
Two’s complement There is NO subtraction in the two’s complement system You “take the two’s complement” of a number to change its sign You then add it (to subtract it!) i.e two numbers 5 and is actually 5 + (-3) so to subtract 3 from 5, take the two’s complement of 3 and add it to 5. 9
in a 4 bit two’s complement system The “weights” are (-8) (+4) (+2) and (+1) Hence 5 is 0101 and 3 is 0011 (-3) is (-8)+(4)+(1) = 1101 Adding 0101= =(-3) = +2 in two’s complement we ignore bits beyond the most significant bit. COM181 Computer Hardware:IMcC10
Alternative way to take the two’s complement E.g to convert 3 to -3. Write 3 in binary, at the correct length (4 bits in our example here, could be bigger) 0011 NOW INVERT THE BITS 1100 THIS GIVES THIS +1 NOW ADD ONE AS BEFORE. (inverting the bits is called taking the one’s complement) 11