Skip to main content

DESIGNING A RELAY COMPUTER: The Negative

When we hear about negative numbers the symbol " - " comes to mind since the use of the symbol seems to make any number negative.
As human beings it is pretty easy to think about negative numbers and deal with the " - " symbol. But for a computer we need a binary notation for this too since there is no way of representing " - ", it either has to be a 0 or 1. In this post I will try and show the various ways one could write negative numbers in binary and whether or not they are suitable for doing mathematics.
To test if the representation method is appropriate we can try doing a simple arithmetic operation such as subtraction. If the result generated after the operation matches the correct result, we can be sure that the system of representing the numbers works.

Sign Magnitude:

To solve the problem of handling negative numbers we can device a simple solution of using an extra bit on the left of the number and call it the "Sign Bit". The rest of the bits on the right will denote the magnitude or the actual number.
Using this method we get the following table:
In this method however, we notice a problem. As highlighted in the table, there are two notations of the number zero +0 and -0.
Now, let's try doing some simple arithmetic operation with it.












Suppose we had to do the following operation:

7 - 3 = ?

to do that we could try and use this negative number notation, turning the problem into:

7 + (-3) = ?

Now, adding the two numbers in binary we get:
As you can see the answer is incorrect since it should be 0100 (+4). Hence, this is not an appropriate method to represent negative numbers.

1's Complement:

In this method the positive numbers are written as it is while having an extra zero digit on the left. this extra digit is considered the sign bit.
To represent negative numbers the same positive numbers are inverted i.e. turn all 1's to 0's and vice versa. By this method the following table can be made:

In this method however, we notice the same problem. As highlighted in the table, there are two notations of the number zero +0 and -0.
But let us ignore this fact for now and try doing some simple arithmetic with it.












Let's try the previous operation again:

7 + (-3) = ?

Now, adding the two numbers in binary we get:
In this case the generate carry is added to the number again giving us the correct answer 0100 (+4). So even though this system still has two representations of zero its still allows operations to function correctly.


2's Complement:

This method of representing negative numbers is similar to 1's complement. The only difference being - after inverting the bits, i.e. turning 1's to 0's and vice versa, 1 is added to the number and any carry generated is discarded. Applying 2's complement to the numbers gives us the following table:
The first thing one might notice is that there is only one representation for zero i.e. 2's complement of 0000 is 0000.














Now, let's try doing the previous operation again using 2's complement:

7 + (-3) = ?

In this case the carry generated is discarded. As can be seen from this example the 2's complement method of writing negative numbers gives us the correct answer 0100 (+4).

So, the 2's complement method not only eliminates the extra zero but also gives us the correct result.
This concept of bit inversion and adding 1 can actually be used to modify the adding machine from the previous post to a subtracting machine as well (give it a try maybe??).

Got any queries??? 
E-Mail me at: shashwath.sundar@gmail.com

Comments

Popular posts from this blog

DESIGNING A RELAY COMPUTER: Relays

In the previous posts I highlighted the various concepts that are fundamental to designing a computer. Now I think its time to mention the ways of practically applying these concepts to design a computer. The first thing I should highlight is that I will not be using transistors, I'll be using relays. This is due to the fact that its easier to understand how a relay works than to understand how a transistor works. Also, this seems to be the easiest way to understand how computers actually work since we are using mechanical switches instead of solid state switches (transistors). What is a Relay?? Relays are basically electrically controlled switches. Inside a relay is an electromagnet. when the electromagnet is powered, it attracts a metal strip that in turn operates a switch. Most widely available relays are SPDT relays or Single Pole Double Throw relays. In these relays the switch has 3 terminals- Common terminal , NO i.e. normally open terminal and NC i.e. normally ...

DESIGNING A RELAY COMPUTER: The Adding Machine

In a CPU there is circuitry dedicated to do all kinds of arithmetic operations and logical operations on binary numbers. Adders are circuits that reside in this ALU section of the CPU and they, as the name suggests, add numbers. So, how exactly does a circuit add binary numbers?? As I had previously mentioned in my post talking about number systems, certain properties of numbers remain the same regardless of the base of the number system. So, adding two binary numbers is similar to adding two decimal numbers. Lets first look at how we add decimal numbers. Suppose we were to add two 3 digit numbers: To add these two numbers first we add the numbers in the units place. Upon adding the two numbers we get either a single digit answer or a double digit answer. If the answer is a single digit it is considered the units digit of the final sum and we add the digits of the next place value. If the answer is a two digit number the units place of the answer is considered as ...

Making a URISC Processor: The Idea

So a while back I saw this video on Youtube by "Gary Explains" talking about a "One Instruction Set Computer". For those who do not know, a computer processor does a fixed set of things or follows a fixed set of instructions. Usually modern processors have hundreds of instructions. In the OISC or URISC (Ultimate Reduced Instruction Set Computer) processor the idea is to have one universal instruction that the processor executes that allows the programmer to write any program. The first time I heard of this idea I fell off my chair with excitement (Literally). My mind was really blown by the fact that you could write any program with just one instruction. So after going through what "Gary" on Youtube had to offer... I sat down and tried to come up with a design of this hypothetical processor that executes only one instruction. In the video Gary talked about a processor that executed an instruction called 'SUBLEQ' which simply means, SUB tract and th...