Hey There!

Hope you find these contents helpful. If you have any questions hit us up with an email.

We are looking to get 20,000 downloads for our app Prefies. Prefies lets you share, discover and find great products. If each one of you download & share Prefies, we will have reached our goal in 1 minute. Help us meet our goal. Prefies is now in the app stores.

You Rock!

– Teahlab Team

Java Circuit Simulation Workaround ▾

Due to recent changes by Oracle, java applets have become difficult to run in the browser. To mitigate the troubles, Oracle has provided the following websites to help users troubleshoot: http://java.com/en/download/help/enable_browser.xml and https://www.java.com/en/download/mac_download.jsp

Even after following the above instructions, loading applets may still show warning concerning “unsigned application” and “unknown publisher”. For Teahlab in particular, these warnings are due to the fact that we have opted not to pay a third party such as Verisign to sign our applets. Any warning that comes up when you try to run our applets should emphasize that our applets will always run with “limited access”, which is Oracle’s way of letting you know that teahlab doesn’t do anything on your computer except running the circuits you see: in other words, our applets are safe to run.


The Teahlab Team

by Isai Damier

Interactive 4 bit adder-subtractor digital logic boolean circuit.


To be able to perform arithmetic, you must first be familiar with numbers. Therefore, although we give a few helping examples, this article is not about binary numerals.

The main interactive circuit at the top of this page is an arithmetic circuit capable of performing both addition and subtraction on any two 4-bit binary numbers. The circuit has a Mode switch that allows you to choose between adding (M=0) and subtracting (M=1). To understand why this circuit works, let’s review binary addition and binary subtraction. We use 4-bit numbers in the examples because the main interactive circuit is a 4-bit adder–subtractor.

Binary addition is certainly easier than decimal addition. You just add 0s and 1s. For example to add the numbers five (0101) and six (0110) together, we just add the respective bits:

Decimal numerals Binary numerals
  11   1011

For binary subtraction, we use 2’s complement to keep things simple. For instance, to perform the operation six (0110) minus five (0101), we first obtain the 2’s complement of five and then add it to six:

Step one: Getting the 2’s complement of 5

  1. Flip every bit in five to get 1010.
  2. Add one to 1010 to get: 1010 + 1 = 1011.
Step two: Adding the 2’s complement of 5 to 6:
  1. Decimal numerals Binary numerals
       1   10001.

We show the carry bit in green because normally it does not count towards the result.


Now that we have reviewed our binary addition and subtraction skills, let’s look at what the circuit is doing. If you fix your gaze on the full–adders while you play around with the input switches, you will see that no matter what operation you think you are doing, the full–adder’s job never changes: the full–adder adds up the signals that appear at its inputs. This is surely not a strange behavior. In reality, whether you are doing binary addition or binary subtraction, the last thing you always do is add the numbers (e.g. “Step two” above tells you to add).

So how does the circuit know whether you want to add or subtract? The XOR gates and the Mode switch, of course! The circuit assumes that your subtraction will always be in the form A — B. So because one input of each XOR gate is attached to the Mode switch while the other input of each XOR gate is attached to a B bit, when the Mode switch is equal to 1, the output of the XOR is the complement of B. This is because B xor symbol: a cirle with a plus sign inside 0 = B and B xor symbol: a cirle with a plus sign inside 1 = B’. In addition to going into the XOR gates, the Mode signal also goes into the carry input of the least significant full–adder (C0), which basically adds a 1 to B’ when the Mode switch is ON. Hence, when the Mode switch is equal to 1, the XOR gates and the Mode switch take the 2’s complement of B and pass it to the full–adders.

To recap, here is a brief summary of how the circuit works: When M = 0, the full–adders see A + B because B xor symbol: a cirle with a plus sign inside 0 = B. However, when M = 1, the full–adders see A + B’ + 1 because B xor symbol: a cirle with a plus sign inside 1 = B’ and C0 = M. And, yes, (B’ + 1) is the 2’s complement of B.

Although our presentation focuses on a 4–bit adder–subtractor, the circuit can be extended to any arbitrary size by simply incorporating more XOR gates and full–adders.

Using the Adder–Subtractor

The following four tables show you how to use the adder-subtractor to perform different type of arithmetic operations. At the time of publication no similar breakdown exists in the literature. Hence, we advice that you pay attention to the distinctions.

Operation Note calculation
Unsigned addition Ignore the V-bit


Operation Note calculation
Signed addition
  • The addends A & B and the sum S are all 2’s complements.
  • Ignore C & V


Operation Note calculation
Subtraction without overflow (V=0)
  • The minuend A, the subtrahend B, and the difference S are all 2’s complements.
  • Ignore C


Operation Note calculation
Subtraction with overflow (V=1) The minuend A, the subtrahend B, and the difference S are all 2’s complements.


2’s Complement Table

Binary 2’s complement Binary 2’s complement
01111 +15 10000 -16
01110 +14 10001 -15
01101 +13 10010 -14
01100 +12 10011 -13
01011 +11 10100 -12
01010 +10 10101 -11
01001 +9 10110 -10
01000 +8 10111 -9
00111 +7 11000 -8
00110 +6 11001 -7
00101 +5 11010 -6
00100 +4 11011 -5
00011 +3 11100 -4
00010 +2 11101 -3
00001 +1 11110 -2
00000 +0 11111 -1