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

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 ../../../../.." *Put these in your .bashrc to save permanently. 2. 'ctrl-r' to search:-     Pressing the up arrow till you reach that one previous

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,      - This can lead to frustration, anger, regret, grief...       uff... Enough d

How can we become narrow-minded people without realising it?

In science, whenever we observe a novel event, we start sharpening our brains to decode it. We make a couple more observations related to that event, and try to get a reason for it. It was also the same with the previous generation of scientists. They observed various new phenomena and they gathered as much information as possible to get the accurate reason behind them. But there was a problem with the human mind. In the past days, when there was limited information at hand and more time was required for getting new information, scientists had to make the best possible theories (i.e., explanations) with whatever data was available. It was like a competition, where the scientist with the best theory would win. This "winner" scientist, should be able to explain the data from future experiments with the theory. Consider that, a new experiment happened later, but the results were not explained by the theory of our scientist, then the theory was to be changed, and again the compet