# What is Two’s Complement? A brief Guide

## Introduction to Two’s Complements

Two’s complement is a technique used in computer science to represent signed integers (positive and negative numbers) using binary numbers. In this method, the most significant bit (MSB) represents the sign of the number, with 0 indicating a positive number and 1 indicating a negative number.

## Basic Concepts of Two’s Complements

Two’s complement is a mathematical technique used to represent signed integers in binary form. It provides a way of encoding negative numbers in the same system that is used to represent positive numbers.

To obtain the two’s complement of a binary number, We first invert all the bits of the number and then add 1 to the result. This gives us the two’s complement representation of a negative number. For example, if we take the two’s complement of the binary representation of 5, we get the decimal representation of -5 and vice versa.

In the two’s complement system, the leftmost (most significant) bit of a binary number represents its sign. Specifically, if the leftmost bit is 0, the number is positive, while if it is 1, the number is negative. This means that 2’s complement can represent both positive and negative numbers using the same number of bits.

Two’s complement is widely used in computer arithmetic and is particularly useful for performing arithmetic operations on binary numbers, including addition, subtraction, multiplication, and division.

## How to Convert Positive and Negative Integer into 2’s Complements?

To convert a positive integer to its two’s complement representation, we simply write its binary representation and add 1 to the least significant bit. For example, the decimal number 5 in binary is 101, invert all the digits like 101 = 010, and add 1, So its two’s complement representation is also 010 + 1 = 011.

To convert a negative integer to its two’s complement representation, we first write its absolute value (the positive version of the number) in binary. Next, we invert all the bits (switching all 0s to 1s and all 1s to 0s) and add 1 to the result. For instance, To convert the decimal number -5 to its 2s-complement representation:

Write the absolute value of the number in binary: 5 = 101

Invert all the bits: 010

Add 1: 010 + 1 = 011

Now take it as a binary number and invert the digits such as 011 = 100. After adding 1 to LSB it will be 101.

Hence, -5 in two’s complement is represented as 101.

The two’s complement representation is useful in performing addition and subtraction of signed integers using binary arithmetic.

A two’s complement calculator can be a helpful way to convert positive and negative integers into 2’s complements with steps.

## Examples of Two’s Complement

Representing Negative Numbers:

• Convert the absolute value of the number to binary.
• Invert all the bits of the binary number.
• Add 1 to the inverted binary number.
• Deal the result as binary and again revert all the digits and add 1 to the least significant bit.

Example: Represent the decimal value -7 in binary using 2’s complement.

Solution:

• Step-1: Convert the absolute value of 7 to binary, which is 0111.
• Step-2: Invert all the bits of the binary number, resulting in 1000.
• Step-3: Add 1 to this result, obtaining 1001.
• Step-4: Now again invert the bits, resulting in 0110
• Step-5: Add 1 to the LSB, obtaining 0111

Thus, the two’s complement representation of -7 in binary is 0111.

Adding Binary Numbers with Different Signs

To add binary numbers with different signs using two’s complement, follow these steps:

• Add the two numbers as if they were both positive, ignoring their signs.
• Discard the leftmost bit if the result is one bit too long.
• Determine the sign of the result based on its leftmost bit.

Example: Add the binary numbers 0110 and 1011

Solution:

• Step-1: Add the two numbers as if they were both positive, resulting in 10001.
• Step-2: Discard the leftmost bit, resulting in the binary number 0001.
• Step-3: Because the leftmost bit of the result is 0, the final result is a positive number, representing the decimal value 1.

Subtracting Binary Numbers with Different Signs

To subtract binary numbers with different signs using two’s complement, follow these steps:

• Take the two’s complement of the subtrahend (the number being subtracted).
• Add the two numbers as if they were both positive, ignoring their signs.
• Discard the leftmost bit if the result is one bit too long.
• Determine the sign of the result based on its leftmost bit.

Example: Subtract the binary number 0110 from the binary number 1011.

Solution:

• Step-1: Take the two’s complement of the subtrahend, which is 1010.
• Step-2: Add this result to the minuend (the number from which the subtrahend is being subtracted) as if they were both positive, resulting in 10001.
• Step-3: Discard the leftmost bit, resulting in the binary number 0001.
• Step-4: Because the leftmost bit of the result is 0, the final result is a positive number representing the decimal value 1.

Therefore, subtracting a negative number using two’s complement is equivalent to adding its absolute value.

Conclusion:

In conclusion, two’s complement is a useful mathematical technique for representing negative numbers and performing arithmetic operations with binary numbers. It allows computers to perform addition and subtraction operations using the same hardware circuits, simplifying the design of computer processors.