Menu Close

Why Are Integers Stored in Memory in Two’s Complement Form ?

Each data type in a computer is stored in binary form because that is the only language that a computer can understand. Other languages such as decimal are interpretive, and the machine must translate them into binary to truly understand them.

How are positive integers stored in a computer? For example, the number 4 is binary 100, so how do we store 100 in 4 bytes of memory:

integer storage
integer storage

The C language standard does not mandate that binary be stored in the form of negative numbers, but rather it is generally stored in two’s complement form.

In N-bit memory, the two’s complement of integer X is 2^N – X. For example:

  • In 8-bit memory, the two’s complement of 1 is 2^8 – 1, or 1111 1111 in binary.
  • In 8-bit memory, the two’s complement of 8 is 2^8 – 8, or 1111 1000 in binary.

Here is the representation of the two’s complement and ones’ complement of decimal 1 and 21, and the binary representation of -1 and -21:

  • The two’s complement and ones’ complement of decimal 1 are both the binary number 1.
  • The binary representation of decimal 21 in original form is 10101, in two’s complement form is 01011, and in ones’ complement form is 10100.
  • The two’s complement and ones’ complement of decimal -1 are both 1111 1111 in binary.
  • The two’s complement of decimal -21 is 1110 1011 in binary, and its ones’ complement is 1110 1010.

Please note that for signed integers, the highest bit is the sign bit. If this bit is 0, it represents a positive number, and if it is 1, it represents a negative number. In two’s complement representation, the sign bit of a negative number is always 1.

 

1 = 0000 0001
~1 = 1111 1110 (1's complement)
~1 + 1 = 1111 1111 (2's complemen)
-1 = 1111 1111 (Stored in Memory)

21 = 0001 0101
~21 = 1110 1010 (1's complement)
~21 + 1 = 1110 1011 (2's complement)
-21 = 1110 1011 (Stored in Memory)

Two’s complement has several advantages over one’s complement. For example, it eliminates the concept of “negative zero,” which can be confusing. If two’s complement is used, addition, multiplication, and subtraction work the same way for all signed integers.

Integers are stored in memory in two’s complement form.

The Sign-Magnitude, one’s complement , and two’s complement of a positive integer are the same.

Integers are stored in computers in two’s complement form for a few reasons.

Firstly, two’s complement is a simple and efficient way of representing both positive and negative numbers in binary form. This is because it only requires a single binary representation for each value, which makes the hardware design of the computer simpler and more efficient. In contrast, if the computer used a separate representation for positive and negative numbers, it would require additional hardware to distinguish between them, which would be more complex and costly.

Secondly, two’s complement simplifies arithmetic operations on integers. In particular, it allows the computer to use the same set of instructions and logic for both addition and subtraction of integers, without requiring any special treatment for negative numbers. This is because, in two’s complement form, the operation of adding a negative number is equivalent to subtracting its absolute value, and vice versa. This means that the same circuitry can be used for both addition and subtraction, which again simplifies the hardware design and makes the computer more efficient.

Finally, two’s complement allows for easy detection of overflow and underflow conditions, which can occur when adding or subtracting integers. This is because an overflow or underflow occurs when the result of an operation cannot be represented using the same number of bits as the operands. In two’s complement form, this can be detected simply by examining the sign bit of the result. If the sign bit changes from positive to negative or vice versa, then an overflow or underflow has occurred.

Overall, two’s complement is a simple and efficient way of representing integers in a computer, which simplifies the hardware design, allows for efficient arithmetic operations, and facilitates detection of overflow and underflow conditions.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Related Posts

Leave a Reply