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

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

Making a URISC Processor: Micro-Instruction Encoding

 In the last post I described the micro-instructions that are to be executed to execute the SUBNEG instruction. Given below is the table I had posted previously: I realized later that it still might not be entirely clear as to how this was working so I have made a GIF of the same to better explain how the "Subtraction" instruction is executed. Pretty cool huh?.... no??? okay 😢 In The GIF it is assumed that the following data is stored in the RAM: Address    Byte #0000           #32  [B(l)] #0001         #00  [B(h)] #0002         #33  [A(l)] #0003       #00  [A(h)] #0004       #20  [C(l)] #0005       #01  [C(h)] #0032         #07 [Data] #0033       #09 [Data] The next thing we need to do is try to optimize this process. In the above GIF you may or may...