0% found this document useful (0 votes)
57 views

Binary Two's Complement

Uploaded by

snadmin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
57 views

Binary Two's Complement

Uploaded by

snadmin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
How can negative numbers be represented using only binary 0's and 1's so that a computer can “read” them accurately? The concept is this: Consider the binary numbers from 0000 to 1111 (i.e., 0 to 15 in base ten). 9001 -9111 will represent the positive numbers 1 +7 respectfully and, 1001~>1111 will represent the negative numbers ~7 —> ~4, respectfully In a computer, numbers are stored in registers where there is reserved a designated number of bits for the storage of numbers in binary form. Registers come in different sizes. This handout will assume a register of size 8 for each example Itis easy to change a negative integer in base ten into binary form using the method of two’s complement. First make sure you choose a register that is large enough to accommodate all of the bits needed to represent the number. Step 1: Write the absolute value of the given number in binary form. Prefix this number with 0 indicate that itis positive. Step 2: Take the complement of each bit by changing zeroes to ones and ones to zero. Step 3: Add 1 to your result. This is the two’s complement representation of the negative integer. EXAMPLE: find the two’s complement of -17 Step 1: 1749 = 0001 0001, Step 2: Take the complement: 1110 1110 Step 3: Add 1: 1110 1110 +1 = 1110 1111. Thus the two's complement for -17 is 1110 1111). It begins on the left with a 1, therefore we know itis negative. To translate a number in binary back to base ten, the steps Now you try some: greene Fineithene s combieter a Step 1: Subtract 1: -. 11101111 -1= 1110 1110 ieieieis Step 2: Take the complement of the complement: 0001 0001 ae Step 3: Change from base 2 back to base 10 “.16+1= 17 crete Step 4: Rewrite this as a negative integer: ~17 This suggests a new way to subtract in binary due to the fact that subtraction is defined in the following manner: X-Y=X+(¥) EXAMPLE 1: Subtract 17 from 23, as a computer would, using binary code. Given a register of size 6, 23 -17 = 23 + (-17) becomes 0001 0111 + 1110 1111 = 10000 0110. (Verify both the binary form of 23 and the addition.) Since this result has 9 bits, which is too large for the register chosen, the leftmost bit is truncated, resulting in the binary representation of the positive (it starts with a 0) integer 00000110. When this is changed to a decimal number, note that 4 + 2 = 6 which is the answer expected. Note that a register of size eight can only represent decimal integers between —2" and +2) and, in general, a register of size n will be able to represent decimal integers between —2""" and zim EXAMPLE 2: Subtract 29 from 23, as a computer would, using binary code. Again we use a register of size 8, so that 23 ~ 29 = 23 + (-29) becomes 0001 0111 + 1110 0011 = 1111 1010. (Verify both the binary form of -29 and the addition.) Note that no truncation of the leftmost bit is necessary here. The result is the negative (it starts with a 1) integer 1111 1010. This needs to be “translated” to change it back toa decimal (see the steps on how to do this in the box above). Hence, going backwards, 1111 1010 ~ 1 = 1111 1001. The complement of which is 0000 0110 which is 6 in decimal, Negating this we get -6 as expected. Now you try some: Subtract each, as a computer out, using binary code using registers of size 8. a) 26-15 b) -31-6 ) 144-156 d) Make up your own exercises as needed. ANSWERS. 11 = 11110101, -43 = 1101 01012 123 = 100001012 26 ~ 15 = 26 + (-15) = 0001 1010 + 1111 0001 = 10000 1021, and truncating the leftmost 1 to remain within a register of 8, the answer is 0000 10112 31-6 = (-31) + (-6) = 1110 0001 + 1111 1010 = 11101 1011, and truncating the leftmost 1 to remain within a register of 8, the answer is 1101 10112 144 156 = 144 + (-156) = 1001 0000 + 0110 0100 = 1111 0100, which remains register of 8 bits (so nothing gets truncated), thus the answer is 1111 01002. the 4 ee ee eat 2° Waa your 4 some (ean) Mot toa ee ti 1 8) -ab-ws Gane i Ti nero Sea eee bevel eae bee 7 cere dete te ME Bee” an co Go aw wer ee Hi Gia commen teem, p) —H3,0 = 1011 le Het ea RB Oe se priate yaad Viro6001 ary germane tee etmek ge OL as en 0 8100 ety Lie mms wn Tore ie 2 Mit Bip; Be E Torrerr ‘Csi rgnew ei! Tors, Saas rd Meeeeons tay C) 11349 = 10000101 LY Witter totter ty Wage UMOIY fete HE gd a C) 1885 =e Gass ong 10000100, fnke om Thee "PoMans HL pmatieon yall at orl tts Suledt of fet tarp of on Brvid teyabin. PO. get Neer date tne odd get usar You tM Some! iat 4 ‘ noise Bee 18) adh ood atte " “ seals tliat oF mae ac He eocenity 1 ty 0004 setos wei asise i) LIL opts te ethesh Ego me Viteloo 1) osee itt « +o ened ater Leooe tet moms GE Pa UROt most Ld aes Aenea, tele ae Beer joios + 1111 90812 = 0800 Lol Gee hares cates ee ee eae E disieg 4t easton \oe1 0000, 4 ol oe todas (II st00,,

You might also like