# [solved] How to subtract 54-21 in binary ?

A = 54 B = 21 in binary A= 11 0110 B= 1 0101 A-B = A+(-B) 1's Complement of B : 01010 Now adding A + 1's complement of B 11 0110 + 0 1010 ----------------------- 1 00 0000 here carry is 1 so we need to add it in LCB 00 0000 00 0000 + 1 -------------- 00 0001 but 00 0001 in decimal is 1 but 54-21 = 33 where i made mistake ?? please correct me..

1/19/2021 10:09:21 AM

Ratnapal Shende13 Answers

New AnswerBinary subtraction is performed at the hardware level by adding the two's complement of the subtrahend. Essentially there is no subtraction hardware only adders. Note it is the two's compliment that you should be using, not the 1's compliment. 54 = 0011 0110 21 = 0001 0101 Ones compliment of 21 is = 1110 1010 Two's compliment we add 1 to it = 1110 1011 So finally 0011 0110 + 1110 1011 ------------ 0010 0001 = 33 It looks awful in a proportional font, I know.

what would make the most sense to me is changing the whole layout of binary numbers. I would slap both numbers together, like so: 5 == 0101 + 1 == 0001 -------------------- 0102 (2*1) + (0*2) + (1*4) + (0*8) = 6 i then multiply each value by the number of occurences (either 0,1, or 2), then add all the values! ** in subtraction, just do the opposite 54 == 0011 0110 - 23 == 0001 0111 --------------------------------- 0010 0001 = 33 ***After some thought, it looks like you can subtract binary numbers by keeping the 1's that don't have a matching one on the other number.

To negate B and use addition you have to use two's complement. Subtraction in binary works the same as in decimal. If the subtracted digit is bigger than the digit to subtract from, you "borrow" 1 from the next digit, i.e. use carry

Benjamin Jürgens yes we can do that using borrow method but I want to learn 1's complement method.. A-B=A+(-B)

Ratnapal Shende you can't use one's complement here. Use two's complement instead (add 1 to one's complement). https://en.wikipedia.org/wiki/Two%27s_complement With 8bit signed integer: A = 54 = 0011 0110 B = 21 = 0001 0101 -B = -21 = 1110 1011 A + (-B) = 33 = 0010 0001

The way i came to see it, (and this is only tested for a larger number minus a smaller number) is you stack em, then bring down whatever isnt negated from the top. 0011 0110 | this 1 is brought down v 0001 0111 <--(this 1 is leftover) *all others cancel out to give you: 0010 0000 = 32 ***but since you're subtracting, subtract that extra 1 from the total: 32 - 1 = 31 same as 54 - 23 = 31 Benjamin looks like he has a way better way of doing it, but this way works

Ok I try another explanation of division in binary. Principles are exactly the same as with decimal numbers: start at the right and do one digit at a time. If the upper digit is smaller than the lower, you "borrow" 1 from the next digit (to the left) and have to subtract that extra 1 there and "carry" it to the next digit. It is called carry bit. In binary we have only 0 and 1 so I list all 4 cases for a single digit: 0 - 0 = 0, 1 - 1 = 0, 1 - 0 = 1, 0 - 1 = 1 and subtract 1 from the next digit (carry it over) Since we can have an extra - 1, we have additional cases (third number is the carry): 0 - 0 - 1 = 1 and - 1 for next digit, 1 - 1 - 1 = 1 and - 1 for next digit, 1 - 0 - 1 = 0, 0 - 1 - 1 = 0 and - 1 for next digit. So the example with the carry bits in the third line: 0011 0110 - 0001 0101 ____ __1 = 0010 0001

Another good explanation with example: https://en.wikipedia.org/wiki/Subtraction#In_computing

A = 54 B= 21 first convert both numbers in binary.........then take 2's complement of B. Result = A + B's 2's complement. #2's complement = 1's complement of B + 1

haha, mine makes more sense now, i just did the math wrong. Instead of adding 1 to make it 33, you should subtract 1 and make it 31, wich is your answer