Krista's Coding Corner

27.10.2013

Calculating with using just logic operators

I think you understand how logic operators work but creating real calculations might seem a bit fussy (at least to me it did!).

First, we have to be able to calculate two bits together. One bit can be either 1 or 0, so the outcome from summing two bits can be from 0 to 2 (which is really 10 in binary what we use with bits). Output cable can only indicate 0 or 1 (power ON or power OFF). The thing is: how to indicate number that is something else than just 0 or 1? Like this number 2 (=really the 10).

This problem is solved with half adder:

(XOR gives signal only if inputs have different status like 0 and 1).

What an adder does is just logic. If we think about the calculation, let.s look at situation where we calculate 0+0, 1+0 and 1+1.

0+0:

1+0:

1+1:

The thing is how we interpret the result.

With 0+0 it is easy: no output → we get 0 (no signal, result 0).

1+0: only XOR-operator gives output → result is 1 (we have signal!).

1+1, the tricky part. Only AND gives us output → result is 2 (in binary: 10). Why? Because in binary 0,1,2... is 0,1,10... If you think about our normal decimal system we have one digit numbers (0,1,2,3,4,5,6,7,8,9), IF our number is bigger than 9 it won't fit into one digit, right?

We will have an overflow when number nine (9) grows up to ten (10). Ten just won't fit into space meant only for one digit. Same thing happens in binary when number 1 becomes 10 (=2 in decimal system).

When overflow happens (in this case Output_C gives signal), we know that our result is 10+something (if adding two one digit numbers) or 10 (if using binary and adding two one digits).

PS. Output_C comes from Output_carry and Output_S from Output_Sum.

blog comments powered by Disqus