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

25 and Disappointed: The tragedy of leaving home

It’s strange, isn’t it? We live in a world with high-speed internet, 5G, instant messages, Zoom calls and flight tickets that can take us across the country in a few hours. The world has never been more connected. And yet… we’re all so far apart. We are becoming disabled in this tech enabled world. People move away from home more than ever. To study. To work. To “build a future.” Yes, it’s needed sometimes. For learning. For exposure.  But often… it seems we are allowing it quietly more than required. Maybe it’s just the system forcing us to. All the jobs are in cities. All the money, infrastructure and “opportunities”. Hence we leave. We leave behind warm meals, evening walks with parents, familiar lanes and families that actually feel like festivals. We leave people who raised us - and see them only on phone calls or during a short visit squeezed between deadlines. For what? For a cubicle in a crowded city, A rented flat that never feels like home, Weekends that vanish in chores,...

Top 5 take aways from "Linux 101 Hacks" - The Bloggers League 2022

  Author: Rama Subrahmanyam Hello, how are you doing..?         We all know that Windows is a super cute-looking OS, but software people will connect to Linux; It offers much flexibility with file management, and plus, it's open-source too..!         So, the book - Linux 101 Hacks, is a nice intro for beginners, by Ramesh Natarajan . Having said that, we look at the top 5 hacks from the book. It is a free e-book, and you can download it here . 1. 'alias' for most used commands:-     There will be commands that are used repeatedly, for which we can have a shortcut using an alias. For example, for cd commands, we can have            alias cd1="cd .."           alias cd2="cd ../.."           alias cd3="cd ../../.."           alias cd4="cd ../../../.."           alias cd5="cd ../../.....