Basics of Two’s Complement: Method and Examples - Electrical Diary -->

# Basics of Two’s Complement: Method and Examples

Let's understand the binary numbers system before moving to the Two's Complement. A binary number is the most common number system used in computers and digital devices. It contains only two digits, 0 and 1; each digit is a bit. The Positive integers are easily represented using simple binary notation but a representation of negative numbers is more challenging. To overcome this problem, technique two's complement was introduced.

In this article, we will talk about the concept of two's complement. We will learn how to determine the two's complement in a few steps. We will solve some examples of two's complement in the example section.

## What is Two's (2's) Complement?

The two's complement is the most common method used to represent signed integers in digital computers. In two's complement representation, the most significant bit (MSB) is the left-most bit in a binary number, which indicates the sign of a number, where 1 shows a negative number, and 0 represents a positive number.

The remaining bits represent the magnitude of the number, with the rightmost bit, known as the least significant bit (LSB), contributing the least to the overall value of the number.

Note: When the number of bits in the binary representation is odd, the LSB is the second-to-rightmost bit.

## How to determine Two's (2's) complement?

You can determine two's complement by the following steps:

• Begin with the binary representation of the number.
• Invert all the bits (change 1s to 0s and 0s to 1s) in the binary representation.
• Add 1 to the inverted binary representation.

For example, the number 7 in the binary form 0111. Change 1s to 0s and 0s to 1s. We get 1000. Now add 1 to it. (1001 + 1 = 1001). Hence, the two's complement of 7 is 1001.

## An alternative method of finding two's complement

We can also find the two's complement by another method. Just follow these steps:

• Begin with the rightmost bits, and move towards the left until you find the first 1.
• Flip (complement) each bit after 1. In other words, change all the 0s to 1s and all the 1s to 0s after the first 1.
• The bits to the right side of the first 1 remain unchanged.

Let's consider the binary number 1100. The first two digits of the right side are 0, so we leave that and move the next bit 1. We will take the complement of each number after 1. The bits to the right side of the first 1 remain unchanged. Hence, two's complement of the given binary number 1100 is 0100.

There are several advantages of two's complements in computer arithmetic. Some advantages are given below:

• Simplicity: Two's (2's) complement allows for the use of the same arithmetic operation for both positive and negative numbers. This simplifies the design of arithmetic circuits, making them easier to implement.
• Efficient representation: Two's complement gives an efficient way to represent signed integers. It minimizes the storage space required compared to other methods, allowing more efficient memory usage in computer systems.
• Compatibility: Most modern computer languages ​​use two's complement representation, ensuring compatibility across systems.

## Solved Examples of Two's Complement

Let's understand how to determine two's complement with the help of some examples.

Example 1

Determine the two's complements of the binary number 1011110 in an 8-bit.

solution:

Number in Binary form = 1011110

Selected Bits = 8

The binary number after completing 08 bits = 01011110

Change all the 0s to 1s and all the 1s to 0s of the binary number 01011110. ie 10100001

Now add 1 in a flipped binary number.

10100001 + 1 = 10100010

Hence, two's complements of the binary number 1011110 in an 8-bit is 10100010.

example 2.

Calculate the two's complements of the decimal number 21 in 8-bit .

solution:

The binary form of 21 = 10101

Selected Bits = 8

The binary number after completing 08 bits = 00010101

Change all the 0s to 1s and all the 1s to 0s of the binary number 00010101. ie 11101010

Now add 1 in a flipped binary number.

11101010 + 1 = 11101010

Hence, two's complements of the decimal number 21 in an 8-bit is 11101011.

Example 3

Find the two's complements of the Hexadecimal number 5A in 8-bit .

solution:

 hexadecimal number 0 1 2 3 4 5 6 7 8 9 A B C D E F binary form 0 1 10 eleven 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111

The binary form of 5A = 1011010

Selected Bits = 8

The binary number after completing 08 bits = 01011010

Change all the 0s to 1s and all the 1s to 0s of the binary number 01011010. ie 10100101

Now add 1 in a flipped binary number.

10100101 + 1 =  10100110

Hence, two's complements of the Hexadecimal number 5A in an 8-bit is 10100110.

You can also take assistance from a 2 complement calculator to find the two's complements of a binary, decimal, or hexadecimal number with steps.

## Conclusion

In this article, we have discussed the definition of two's complements in depth. We learned two different methods of determining the two's complement. We explored the advantages of the two's complements. In the example section, we solved many examples of 2's complement to learn it better. After understanding this article, you can determine the 2's complement of the binary, decimal, and hexadecimal numbers.