[solved] How to subtract 54-21 in binary ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

[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..

19th Jan 2021, 10:09 AM
Ratnapal Shende
Ratnapal Shende - avatar
12 Answers
+ 2
Slick Benjamin Jürgens sorry guys but I didn't understand yet... still confused!
19th Jan 2021, 3:49 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
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.
19th Jan 2021, 10:25 AM
Slick
Slick - avatar
+ 1
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
19th Jan 2021, 11:08 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Benjamin Jürgens yes we can do that using borrow method but I want to learn 1's complement method.. A-B=A+(-B)
19th Jan 2021, 12:01 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
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
19th Jan 2021, 12:15 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
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
19th Jan 2021, 3:56 PM
Slick
Slick - avatar
+ 1
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
19th Jan 2021, 6:45 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Another good explanation with example: https://en.wikipedia.org/wiki/Subtraction#In_computing
19th Jan 2021, 6:57 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
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
20th Jan 2021, 8:47 AM
Surkhab Khan
Surkhab Khan - avatar
0
Slick i don't get you 54-23 = 33 ?? how?
19th Jan 2021, 11:59 AM
Ratnapal Shende
Ratnapal Shende - avatar
0
Ratnapal Shende so true, I am wrong there. Try subtracting w/ Benjamin's example
19th Jan 2021, 12:01 PM
Slick
Slick - avatar
0
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
19th Jan 2021, 12:03 PM
Slick
Slick - avatar