There are three different ways to represent signed integer (article). a: Signed bit, b: 1’s Complement, and c: 2’s Complement. Let’s try to understand how these methods have derived and why 2’s complement is preferred over others.
As we know that data are stored in bits. How can we store signed integer in the memory? To solve this problem, first we will develop a naïve solution and then will iterate it till we have the best solution for our problem.
a) Signed bit
When trying to store a signed integer, it seems obvious to reserve the left most bit for sign and use remaining bits to actually store the values. For example: in 4-bit system, first bit from left will be reserved for sign (0 represent positive whereas 1 represent negative) and other 3 bits will be used to store the values. Similarly in 8-bit system, first bit from left will be used for sign and remaining 7 will be used for values.
Sr. No.
| Binary Representation
| Decimal Value
|
A
| 0000
| +0
|
B
| 0001
| +1
|
C
| 0010
| +2
|
D
| 0011
| +3
|
E
| 0100
| +4
|
F
| 0101
| +5
|
G
| 0110
| +6
|
H
| 0111
| +7
|
I
| 1000
| -0
|
J
| 1001
| -1
|
K
| 1010
| -2
|
L
| 1011
| -3
|
M
| 1100
| -4
|
N
| 1101
| -5
|
O
| 1110
| -6
|
P
| 1111
| -7
|
By using this approach, we are successfully able to represent signed integer. But when we analysis it more closely, we could observe following drawbacks:
1) Two representations of zero:
In 4-bit system, we should be able to store 16 (24) values, but +1 to +7 and -1 to -7 are only 14 values. Where are remaining two values? When we observe the table carefully, we will find out that those two values converge to 0. Thus, we have two representations of zero, that means, one representation for +0 and another for -0.
But are two representations of 0 a big concern? So what? Instead of 16 unique values, we are only able to store 15 values. We can afford to reduce the range by 1, isn’t it? To the software developer, it might not concern but for a circuit designer it might be very frustrating to first check if value is +0 and then to check if it -0.
2) Signed extension doesn’t work for negative numbers:
Size of the data is increasing rapidly. Some time we need to extend the bit system so that we can increase the range of data that can be stored. In 2014, Gangnam Style video overflowed YouTube view limit and it forced YouTube to upgrade view count from 32-bits to 64-bits signed integer. Similarly, 32-bit Unix clock will overflow on 19 Jan 2038 because it records time in seconds in a 32-bit signed integer.
So, it is equally important that our representation system should be extendable easily which is not possible with this representation system.
Decimal
| 4-bit
| 5-bit
| 6-bit
|
+2
| 0010
| 00010
| 000010
|
+7
| 0111
| 00111
| 000111
|
-2
| 1010
| 10010 (! = 11010)
| 100010 (! = 111010)
|
-7
| 1111
| 10111 (! = 11111)
| 100111 (! = 111111)
|
3) Binary addition doesn’t work:
Let's try to add two binary numbers:
| Binary
| Decimal
|
| Binary
| Decimal
|
| Binary
| Decimal
|
Number-1
| 0010
| +2
|
| 0111
| +7
|
| 1101
| -5
|
Number-2
| 1010
| -2
|
| 1010
| -2
|
| 0011
| +3
|
Binary addition
| 1100
| -4
|
| 0001
| +1
|
| 0000
| +0
|
Decimal addition
|
| +0
|
|
| +5
|
|
| -2
|
Why is a simple binary addition not working here? The reason is that the sign bit (left most) is not an ordinary bit and not part of actual number. Imagine the situation where one has to design the hardware circuitry to ignore the sign bit to perform addition and then append the sign bit.
So, this was a naïve way to represent signed integer. The main problem with this approach is that we have mapped negative numbers down up. If we change our mapping system to top down them some of above issue will be resolved.
b) 1’s Complement
If we remap our negative numbers from top-down, then we will be getting following binary table:
S. No.
| Binary Representation
| Decimal Value
|
1's complement
| Signed bit
|
A
| 0000
| +0
| +0
|
B
| 0001
| +1
| +1
|
C
| 0010
| +2
| +2
|
D
| 0011
| +3
| +3
|
E
| 0100
| +4
| +4
|
F
| 0101
| +5
| +5
|
G
| 0110
| +6
| +6
|
H
| 0111
| +7
| +7
|
I
| 1000
| -7
| -0
|
J
| 1001
| -6
| -1
|
K
| 1010
| -5
| -2
|
L
| 1011
| -4
| -3
|
M
| 1100
| -3
| -4
|
N
| 1101
| -2
| -5
|
O
| 1110
| -1
| -6
|
P
| 1111
| -0
| -7
|
How to get binary representation of an integer in 1’s complement method?
- Positive numbers are represented similar to signed integer method
- Negative numbers are represented by inverting every bit of corresponding positive number (inverting can easily be done by using NOT gate during hardware design)
Let's analyze this closely to see if we have achieved some improvement.
1) Two representations of zero:
In this approach also we have two representations of zero.
2) Signed extension doesn’t work for negative numbers:
Signed extension works perfectly for negative numbers.
Decimal
| 4-bit
| 5-bit
| 6-bit
|
+2
| 0010
| 00010
| 000010
|
+7
| 0111
| 00111
| 000111
|
-2
| 1101
| 11101
| 111101
|
-7
| 1000
| 11000
| 111000
|
3) Binary addition works with modified rules:
| Binary
| Decimal
|
| Binary
| Decimal
|
| Binary
| Decimal
|
Number-1
| 0010
| +2
|
| 0111
| +7
|
| 1010
| -5
|
Number-2
| 1101
| -2
|
| 1101
| -2
|
| 0011
| +3
|
Binary addition
| 1111
| -0
|
| 0100
| +4
|
| 1101
| -2
|
Decimal addition
|
| +0
|
|
| +5
|
|
| -2
|
The answer is not always correct, but it is very close to the right answer. We can make it work if we follow the rule that if you have generated carry forward on your left most bit, then don't throw it away instead bring it back and add it to the right most bit.
| Binary
| Decimal
|
| Binary
| Decimal
|
| Binary
| Decimal
|
Number-1
| 0111
| +7
|
| 1110
| -1
|
| 0111
| +7
|
Number-2
| 1101
| -2
|
| 1001
| -6
|
| 1011
| -4
|
Binary addition
| (1) 0100
| +4
|
| (1) 0111
| +7
|
| (1) 0010
| +2
|
Adding carry forward back
| 0101
| +5
|
| 1000
| -7
|
| 0011
| +3
|
Definitely 1's complement method is better than signed bit. Our major concerns are resolved but remain issue (having two representations of zero) and our hack in binary addition give clues to improve 1's complement method. Let's rephrase those sentences to make it easier.
- We have an extra representation of zero which is unnecessary
- While addition two binary numbers, if we have a carry forward in left most bit, then we have to add +1 to the result i.e., the right answer can be found by traversing down to next row in the binary table.
Both of them direct us that an extra representation of zero is the root cause of issue. So, let's remove this extra zero and shift all negative values to the next row (-7 will move from I -> J, -6 will move from J -> K and so on...)
c) 2’s Complement
When we remove -0 from the 1's complement table and shift all negative values one row below, then we will get following table which is called 2's complement:
S. No.
| Binary Representation
| Decimal Value
|
2's complement
| 1's complement
| Signed bit
|
A
| 0000
| +0
| | +0
| +0
|
B
| 0001
| +1
| | +1
| +1
|
C
| 0010
| +2
| | +2
| +2
|
D
| 0011
| +3
| | +3
| +3
|
E
| 0100
| +4
| | +4
| +4
|
F
| 0101
| +5
| | +5
| +5
|
G
| 0110
| +6
| | +6
| +6
|
H
| 0111
| +7
| | +7
| +7
|
I
| 1000
| -8
| | -7
| -0
|
J
| 1001
| -7
| = inverse of 7 + 1-bit
| -6
| -1
|
K
| 1010
| -6
| = inverse of 6 + 1-bit
| -5
| -2
|
L
| 1011
| -5
| = inverse of 5 + 1-bit
| -4
| -3
|
M
| 1100
| -4
| = inverse of 4 + 1-bit
| -3
| -4
|
N
| 1101
| -3
| = inverse of 3 + 1-bit
| -2
| -5
|
O
| 1110
| -2
| = inverse of 2 + 1-bit
| -1
| -6
|
P
| 1111
| -1
| = inverse of 1 + 1-bit
| -0
| -7
|
How to get binary representation of an integer in 2’s complement method?
- Positive numbers are represented similar to signed integer method
- Negative numbers are represented by inverting every bit of corresponding positive number then adding 1 bit to it
1) One representation of zero:
Now we have only one representation of zero and it allows us to store total 16 unique values (+0 to +7 and -1 to -8).
2) Signed extension works for negative numbers:
Signed extension works perfectly for negative numbers.
Decimal
| 4-bit
| 5-bit
| 6-bit
|
+2
| 0010
| 00010
| 000010
|
+7
| 0111
| 00111
| 000111
|
-2
| 1110
| 11110
| 111110
|
-7
| 1001
| 11001
| 111001
|
3) Binary addition:
| Binary
| Decimal
|
| Binary
| Decimal
|
| Binary
| Decimal
|
| Binary
| Decimal
|
Number-1
| 0010
| +2
|
| 0111
| +7
|
| 1011
| -5
|
| 1111
| -1
|
Number-2
| 1110
| -2
|
| 1110
| -2
|
| 0011
| +3
|
| 1010
| -6
|
Answer
| 0000
| +0
|
| 0101
| +5
|
| 1110
| -2
|
| 1001
| -7
|
4) First bit is a signed bit:
2's complement has this nice property that first bit is a sign bit because all positive starts with 0 whereas all negative with 1.
5) Memory overflow check:
While doing addition, we made sure that our answer is within the range but while designing hardware, memory overflow needs to be detected. It will be very bad idea for hardware designers to check magnitude to catch overflow. 2's complement method provides a very simple way to detect memory overflow. If carry in to signed bit is not equal to carry out of signed bit, then it is case of memory overflow i.e., if carry in to signed bit is 0 but carry out is 1 or if carry in 1 but carry out is 0, then it is case of memory overflow.
| Binary
| Decimal
|
| Binary
| Decimal
|
| Binary
| Decimal
|
| Binary
| Decimal
|
Number-1
| 1011
| -5
|
| 0010
| 2
|
| 0111
| +7
|
| 1011
| -5
|
Number-2
| 1100
| -4
|
| 0110
| 6
|
| 1110
| -2
|
| 0011
| 3
|
Addition
| (1) 0111
|
|
| (0)1000
|
|
| (1)0101
|
|
| (0)1110
|
|
carry in to sign bit
| 0
| overflow
|
| 1
| overflow
|
| 1
| no
|
| 0
| no
|
carry out to sign bit
| 1
|
| 0
|
| 1
|
| 0
|
Similar Reads
Digital Electronics and Logic Design Tutorials Digital Electronics and Logic Design are key concepts in both electronics and computer science. Digital systems are at the core of everything from basic devices like calculators to advanced computing systems. Digital systems use binary numbers (0s and 1s) to represent and process information.Logic g
4 min read
Number Systems
Boolean Algebra and Logic Gates
Logic Gates - Definition, Types, UsesLogic Gates are the fundamental building blocks in digital electronics. There are basically seven main types of logic gates that are used to perform various logical operations in digital systems. By combining different logic gates, complex operations are performed, and circuits like flip-flops, coun
10 min read
Basic Conversion of Logic GatesIn the Digital System, logic gates are the basic building blocks. Â In these logic gates, we can find the gates having more than one input, but will have only one output. The connection between the input and the output of a gate is based on some logic. Based on this logic, different gates are develop
6 min read
Realization of Logic Gate Using Universal gatesIn Boolean Algebra, the NAND and NOR gates are called universal gates because any digital circuit can be implemented by using any one of these two i.e. any logic gate can be created using NAND or NOR gates only.Implementation of AND Gate using Universal GatesImplementation using NAND GatesThe AND ga
6 min read
Canonical and Standard FormCanonical Form - In Boolean algebra, the Boolean function can be expressed as Canonical Disjunctive Normal Form known as minterm and some are expressed as Canonical Conjunctive Normal Form known as maxterm. In Minterm, we look for the functions where the output results in "1" while in Maxterm we loo
6 min read
Types of Integrated CircuitsIn this article, we will go through the Types of Integrated Circuits, we will start our article with the introductions of the ICs, then we will go through different types of ICs one by one, At last, we will conclude our article will their applications, advantages, disadvantages and some FAQs. Table
7 min read
Minimization Techniques
Minimization of Boolean FunctionsBoolean functions are used to represent logical expressions in terms of sum of minterms or product of maxterms. Number of these literals (minterms or maxterms) increases as the complexity of the digital circuit increases. This can lead to large and inefficient circuits. By minimizing Boolean functio
4 min read
Introduction of K-Map (Karnaugh Map)In many digital circuits and practical problems, we need to find expressions with minimum variables. We can minimize Boolean expressions of 3, 4 variables very easily using K-map without using any Boolean algebra theorems. It is a tool which is used in digital logic to simplify boolean expression. I
5 min read
5 variable K-Map in Digital LogicPrerequisite - Implicant in K-Map Karnaugh Map or K-Map is an alternative way to write a truth table and is used for the simplification of Boolean Expressions. So far we are familiar with 3 variable K-Map & 4 variable K-Map. Now, let us discuss the 5-variable K-Map in detail. Any Boolean Express
5 min read
Various Implicants in K-MapAn implicant can be defined as a product/minterm term in Sum of Products (SOP) or sum/maxterm term in Product of Sums (POS) of a Boolean function. For example, consider a Boolean function, F = AB + ABC + BC. Implicants are AB, ABC, and BC. There are various implicant in K-Map listed below :Prime Imp
5 min read
Don't Care (X) Conditions in K-MapsOne of the most important concepts in simplifying output expressions using Karnaugh Maps (K-Maps) is the 'Don't Care' condition. The 'Don't Care' conditions allow us to treat certain cells in a K-Map as either 0, 1, or to ignore them altogether, which can help in forming larger and more efficient gr
4 min read
Quine McCluskey MethodThe Quine McCluskey method also called the tabulation method is a very useful and convenient method for simplification of the Boolean functions for a large number of variables (greater than 4). This method is useful over K-map when the number of variables is larger for which K-map formation is diffi
8 min read
Two Level Implementation of Logic GatesThe term "two-level logic" refers to a logic design that uses no more than two logic gates between input and output. This does not mean that the entire design will only have two logic gates, but it does mean that the single path from input to output will only have two logic gates.In two-level logic,
9 min read
Combinational Circuits
Half Adder in Digital LogicA half adder is a combinational logic circuit that performs binary addition of two single-bit inputs, A and B, producing two outputs: SUM and CARRY. The SUM output which is the least significant bit (LSB) is obtained using an XOR gate while the CARRY output which is the most significant bit (MSB) is
3 min read
Full Adder in Digital LogicFull Adder is a combinational circuit that adds three inputs and produces two outputs. The first two inputs are A and B and the third input is an input carry as C-IN. The output carry is designated as C-OUT and the normal output is designated as S which is SUM. The C-OUT is also known as the majorit
5 min read
Half Subtractor in Digital LogicA half subtractor is a digital logic circuit that performs the binary subtraction of two single-bit binary numbers. It has two inputs, A and B, and two outputs, Difference and Borrow. The Difference output represents the result of subtracting B from A, while the Borrow output indicates whether a bor
4 min read
Full Subtractor in Digital LogicA Full Subtractor is a combinational circuit used to perform binary subtraction. It has three inputs:A (Minuend)B (Subtrahend)B-IN (Borrow-in from the previous stage)It produces two outputs:Difference (D): The result of the subtraction.Borrow-out (B-OUT): Indicates if a borrow is needed for the next
3 min read
Parallel Adder and Parallel SubtractorAn adder adds two binary numbers one bit at a time using carry from each step. A subtractor subtracts one binary number from another using borrow when needed. A parallel adder adds all bits at once, making addition faster. Similarly, a parallel subtractor subtracts all bits at the same time for quic
5 min read
Sequential Binary MultiplierIn this article, we are going to learn how a sequential binary multiplier works with examples. So for that, we also need to learn a few concepts related to the sequential circuit, binary multipliers, etc. Finally solving the examples using a sequential binary multiplier method.Sequential CircuitA se
12 min read
Multiplexers in Digital LogicIn this article we will go through the multiplexer, we will first define what is a multiplexer then we will go through its types which are 2x1 and 4x1, then we will go through the Implementation of the 2x1 mux and higher mux with lower order mux, at last we will conclude our article with some applic
10 min read
Event Demultiplexer in Node.jsNode.js is designed to handle multiple tasks efficiently using asynchronous, non-blocking I/O operations. But how does it manage multiple operations without slowing down or blocking execution? The answer lies in the Event Demultiplexer.The Event Demultiplexer is a key component of Node.js's event-dr
3 min read
Binary Decoder in Digital LogicA binary decoder is a digital circuit used to convert binary-coded inputs into a unique set of outputs. It does the opposite of what an encoder does. A decoder takes a binary value (such as 0010) and activates exactly one output line corresponding to that value while all other output lines remain in
5 min read
Encoder in Digital LogicAn encoder is a digital circuit that converts a set of binary inputs into a unique binary code. The binary code represents the position of the input and is used to identify the specific input that is active. Encoders are commonly used in digital systems to convert a parallel set of inputs into a ser
7 min read
Code Converters - Binary to/from Gray CodeIn this article, we will go through Code Converters - Binary to/from Gray Code, we will start our article by defining Code converters, Binary code and Gray code, and then we will go through the conversion of binary code to gray code and vice versa.Table Of ContentCode ConvertersBinary CodeGray CodeC
5 min read
Magnitude Comparator in Digital LogicA magnitude digital Comparator is a combinational circuit that compares two digital or binary numbers in order to find out whether one binary number is equal, less than, or greater than the other binary number. We logically design a circuit for which we will have two inputs one for A and the other f
7 min read
Sequential Circuits
Introduction of Sequential CircuitsSequential circuits are digital circuits that store and use the previous state information to determine their next state. Unlike combinational circuits, which only depend on the current input values to produce outputs, sequential circuits depend on both the current inputs and the previous state stor
7 min read
Difference between Combinational and Sequential CircuitIn digital electronics, circuits are classified into two primary categories: The combinational circuits and the sequential circuits. Where the outputs depend on the current inputs are called combination circuit, combinational circuits are simple and effective for functions like addition, subtraction
4 min read
Latches in Digital LogicLatch is a digital circuit which converts its output according to its inputs instantly. To implement latches, we use different logic gates. In this article, we will see the definition of latches, latch types like SR, gated SR, D, gated D, JK and T with its truth table and diagrams and advantages and
7 min read
Flip-Flop types, their Conversion and ApplicationsIn this article, we will go through the Flip-Flop types, their Conversion and their Applications, First, we will go through the definition of the flip-flop with its types in brief, and then we will go through the conversion of the flip-flop with its applications, At last, we will conclude our articl
7 min read
Conversion of Flip-Flop
Register, Counter, and Memory Unit
Counters in Digital LogicA Counter is a device which stores (and sometimes displays) the number of times a particular event or process has occurred, often in relationship to a clock signal. Counters are used in digital electronics for counting purpose, they can count specific event happening in the circuit. For example, in
4 min read
Ripple Counter in Digital LogicCounters play a crucial role in digital logic circuits, enabling tasks such as clock frequency division and sequencing. This article explores the concept of ripple counters, a type of asynchronous counter, their operation, advantages, and disadvantages in digital logic design. What is a Counter?Coun
5 min read
Ring Counter in Digital LogicA ring counter is a typical application of the Shift register. The ring counter is almost the same as the shift counter. The only change is that the output of the last flip-flop is connected to the input of the first flip-flop in the case of the ring counter but in the case of the shift register it
7 min read
General Purpose RegistersA register is a collection of flip-flops. Single bit digital data is stored using flip-flops. By combining many flip-flops, the storage capacity can be extended to accommodate a huge number of bits. We must utilize an n-bit register with n flip flops if we wish to store an n-bit word.General Purpose
8 min read
Shift Registers in Digital LogicPre-Requisite: Flip-FlopsFlip flops can be used to store a single bit of binary data (1 or 0). However, in order to store multiple bits of data, we need multiple flip-flops. N flip flops are to be connected in order to store n bits of data. A Register is a device that is used to store such informati
8 min read
Computer MemoryMemory is the electronic storage space where a computer keeps the instructions and data it needs to access quickly. It's the place where information is stored for immediate use. Memory is an important component of a computer, as without it, the system wouldnât operate correctly. The computerâs opera
9 min read
Random Access Memory (RAM)Random Access Memory (RAM) is a type of computer memory that stores data temporarily. When you turn off your computer, the data in RAM disappears, unlike the data on your hard drive, which stays saved. RAM helps your computer run programs and process information faster. This is similar to how the br
11 min read
Read Only Memory (ROM)Memory plays a crucial role in how devices operate, and one of the most important types is Read-Only Memory (ROM). Unlike RAM (Random Access Memory), which loses its data when the power is turned off, ROM is designed to store essential information permanently.Here, weâll explore what ROM is, how it
8 min read
LMNs and GATE PYQs
Practice Questions - Digital Logic & Design