Skip to main content

How binary complements give difference? #Insight #OpenLearn

Source: unsplash.com

The Binary subtraction of two numbers, as you know consists of adding one number to complement of the other. The complement can be a 2s or 1s complement.

Consider the subtraction with 2s complement. After adding with 2s complement of the other number, there are two cases:- 

  • If carry occurs, ignore it, and the answer is the difference required.
  • If carry does not occur, find the 2s complement of the result to get the magnitude of difference, and add a negative sign to answer...

Now, we just go a little deep and try to make a sense of why these rules are framed...

 

Let A and B the two binary numbers of n bits, and we need to find A - B, which we do by finding 2s complementing of B and adding it to A

We can write the expression as:- 

A + 2s complement of B

= A + (2n - B) 

= A - B + 2n 

Based on A is greater or lesser than B, we have two cases :-

Case I: If A > B

A - B + 2n 

= (A - B) + 2n

(required value) + 2n

Thus A - B + 2n will be nothing but just the difference plus 2n. But, 2n is an n+1 bit number, which starts with 1 (MSB) and has n zeros. 

Ex: (23)10 = (1000)2, (24)10 = (10000)2, etc.

We can see that A and B are of only n-bits, and that means A-B will also be of n-bits. This value upon adding to the 2n value, we will get an n+1 bit binary with 1 as MSB and the difference filling the next zeros.

Ex: if n = 3, and A - B = 101

we can write that (2n)10 = (23)10 = (8)10 = (1000)2

then, A - B + 2n = 101 + 1000 = 1101

* similarly, for the same n, if A - B = 110

we can write that A - B + 2n = 110 + 1000 = 1110

* for n = 4 and A - B = 0100

then A - B + 2n = 0100 + 10000 = 10100

So yeah, if A > B, then for n-bit A and B, we are directly have the A - B value with an extra 1..

Case - II: Suppose consider that the A < B; here A - B gives a negative value. But B - A gives a positive number right..?

so it is like 

A - B + 2n 

= (A - B) + 2n

= - (B - A) + 2n

= 2n - (B - A)

= 2n - (some value)

If you observe, (2n - any value) is nothing but the 2s complement...

So, what we got is just the 2s complement of the difference.. wait, it's actually 2s complement of negative of difference (we actually got 2s complement of B - A, but we have to find A - B)

Another thing is, see that we are subtracting a smaller value (the difference) of an n-bit binary from a bigger value, i.e., 2n (an n+1-bit value) which can never produce a carry...

Observations:

In case I, where A > B, we observed a carry, which concludes that the difference is positive; the difference is simple, just neglect that extra carry and note down the remaining part...

In case II we did not get a carry, suggesting that the difference is negative; now to get the value, we need to do a 2s complement once again on the result.. (as it is in 2s complement form because of the operation we performed)


That's why we got such rules of checking carry and deciding whether a further 2s complement is required or not, and yeah, a similar analysis can be made when we use the 1s complement addition...

Thanks for reading...

Hope you understood the post, and you need not hesitate to inform us if there's any kind of error in the topic discussed...

If you also knew or came across any exciting concepts, make sure you share with us and other readers through the comment section, and help us reaching 'The Eternal Thing'...



Comments

Popular posts from this blog

25 and Disappointed: Can’t we choose simple life?

 There’s something strange about the way we live now. It’s not enough to do your job. You have to grow. Climb. Upskill. Network. Earn more. Spend more. Repeat. People areound you - even the ones who seem tired - still say, “Keep pushing.” The ones ahead keep reaching higher. The ones behind are told to catch up. And if you stand still for even a moment, they say you’re wasting time. But no one explains why. Why is a peace a problem? Why is being satisfied with what you have seen as a failure? This idea of “simple life” - it used to be normal. Work. Come home. Cook. Be with the people you love. Rest. Now it’s rare. Or romanticized. Or quietly looked down on. We’re told that success means constant movement. That we need to hustle, or we’ll be left behind.  But behind what, really? We’ve normalized the rat race, without asking who started it. And the truth is - only a few can actuallly win it. Most people burn out trying.  Others fake their way through it, smiling through st...

Bye-bye strict timetables...! - The Bloggers League 2022

     Author: Rama Subrahmanyam (ramasubrahmanyam.m@gmail.com) When we are planning for multi-tasking, we divide our time and allocate it for each task. That is a great thing, as it gives a chance for incremental growth, eventually getting compounded...      Scheduling makes us punctual; sometimes forces us to be punctual if someone is counting on us, say an examination, office meeting, so on...      Besides strict things, some activities should happen at planned times, like sports, gym, etc. But, we still have things, that can be done in our free time - say reading books, solving puzzles, or learning something of our passion. Should we also maintain the timetable in those cases...? Well, may not be that necessary... We get used to a task at the same time if we have a strict timetable; In a way, can also affect our peace... Suppose due to urgent work, we missed a task      -  We may develop a bad feeling,    ...

25 and Disappointed: Survival should not mean Struggling

There are days I wish I could just leave my job. But I don’t do it. Because I need to pay my rent. I need to eat. I don’t have any other income. I am afraid that I am not alone in this. I see people who are smart and talented - doing things they don’t love. They settle. Not out of choice, but out of fear. The fear of being homeless, being hungry. They live in a world that demands payment of bills more than anything. That brings a simple, maybe naive thought: What if food and shelter were guaranteed for everyone? What if one need not be working just to survive? Is it wild to wish for these? Are these not the basic human rights… If the basic needs were covered, what could we become? Artists, teachers, thinkers, healers - they are made out of passion. People can volunteer more, care for the elderly, innovate better, or take time and get a chance to do things slower… The working class would not have to burn themselves to keep the lights on. The poor wouldn’t have to beg.  What if peopl...