How Integers Are Stored in Computer Memory? | Signed Integers and 2’s Complement

How Integers Are Stored in Computer Memory? | Signed Integers and 2’s Complement | Simplified

To understand how integers are stored in computer memory, it is important to know about signed integers and 2’s Complement.

Introduction

Computers operate using binary — a language of 0s and 1s — to represent and process numbers. But not all numbers are the same. Some numbers are only positive (like whole numbers), some are both positive and negative (like integers), and others include fractions or decimals (like real numbers).

To handle signed integers — positive and negative whole numbers — computers use a system called two’s complement. This method allows for efficient arithmetic operations and consistent storage of negative values in binary form.

Signed integers and 2’s complement are fundamental concepts for anyone studying computer science or working with low-level programming and data representation.

What Are Signed Integers?

Signed integers are whole numbers that can either be positive or negative. For instance, -3, 0, and 7.

In computing, signed integers are crucial for representing real-world values like:

  • Temperatures below zero
  • Bank account overdrafts
  • Negative offsets in graphics

Bit Sizes and Sign Bit

Computing systems use a fixed number of bits (preferably) to store values. For 8-bit signed integers, the leftmost bit is reserved as the sign bit. It indicates whether a number is positive or negative.

  • Sign Bit: 0 for positive, 1 for negative.
  • The remaining bits store the magnitude of the number.

Signed Integers Value Range

When using n–bit, and reserving 1 bit for the sign, leaves you with (n – 1) bits to represent the magnitude (value range).

But in two’s complement, negative values are encoded in such a way that you can represent one more negative number than positive.

So, the range for an n–bit signed integer is:

-2^{n-1} \ \text{to} \ 2^{n-1} - 1

\text{minimum value to maximum value}

Hence, the minimum and maximum values look like this:

SizeBitsMinimum ValueMaximum Value
1 byte8–2⁷ = –1282⁷–1 = 127
2 byte16–2¹⁵ = –32,7682¹⁵–1 = 32,767
4 byte32–2³¹ = –2,147,483,6482³¹–1 = 2,147,483,647

What is 2’s Complement?

Two’s complement is the standard binary system used to represent negative numbers in computing.

It simplifies arithmetic operations and eliminates the need for separate circuits to handle subtraction or negative values.

How to Convert to 2’s Complement (–x)?

1. Write the positive binary value of x using a fixed bit size.

2. Invert all bits (0 → 1, 1 → 0).

3. Add 1 to the inverted result.

Example

Convert ‘–5’ in 8 bits.

Step 1

+5 in binary = 00000101

Step 2

Invert bits      = 11111010

Step 3

Add 1            = 11111011

Hence, ‘5’ is stored as 11111011 in 8-bit two’s complement.

Why Use 2’s Complement?

Two’s complement presents its pros and cons.

Advantages
  • Simplified hardware: Works naturally with existing adders.
  • Uniform arithmetic: No special rules for subtraction.
  • Single zero: Only one representation for 0.
  • Easy negation: Invert bits and add 1.
Disadvantages & Limitations
  • Fixed range: Limited by bit size.
  • Overflow risk: If the result exceeds the range.
  • No Fractions: Only integers can be directly represented.

Now, let’s explain the process of converting a decimal integer to its binary representation and vice versa. It includes examples of both positive and negative integers.

Decimal to Binary Conversion

To convert a decimal number to binary:

1. Divide the number by 2 repeatedly.

2. Note down the remainders.

3. Record the remainders from bottom to top.

For negative numbers, do these additional steps (use of 2’s Complement).

4. Invert the bits (0 → 1, 1 → 0)

5. Add 1

This will give the binary equivalent of the signed integer.

Decimal to Binary (Positive Numbers)

Convert 47 to Binary.

47 ÷ 2 = 23 remainder 1

23 ÷ 2 = 11 remainder 1

11 ÷ 2 = 5 remainder 1

5 ÷ 2 = 2 remainder 1

2 ÷ 2 = 1 remainder 0

1 ÷ 2 = 0 remainder 1

Hence, the binary of 47 is:

47 (decimal) = 101111 (binary)

After padding with zeroes for an 8-bit binary.

47 (decimal) = 00101111 (8-bit binary)

Decimal to Binary (Negative Numbers)

Convert –47 to 8-bit Two’s Complement.

As seen earlier, the binary of 47 is 00101111. So, diving directly into the 2’s complement:

Step 1

+47 in binary = 00101111

Step 2

Invert: 11010000

Step 3

Add 1: 11010001

Hence, the signed integer 47 in binary is:

–47 = 11010001 (8-bit 2’s complement)

Note that the leftmost bit (also known as ‘most significant bit’ – MSB) is ‘1’, which represents a negative value.

Binary to Decimal Conversion

The conversion of a binary to decimal involves the following steps:

1. Multiply each bit by powers of 2 from right to left.

2. Add the results.

This technique is applicable if the sign bit is 0 (positive numbers). However, if the sign bit is 1, proceed as follows:

3. Invert the bits

4. Add 1

5. Convert the result to decimal (using steps 1 and 2)

6. Apply a minus sign

Binary to Decimal (Positive Numbers)

Convert (00101111)2 into decimal.

= 0×2⁷ + 0×2⁶ + 1×2⁵ + 0×2⁴ + 1×2³ + 1×2² + 1×2¹ + 1×2⁰

= 0 + 0 + 32 + 0 + 8 + 4 + 2 + 1

= 47

Hence, the decimal equivalent of the binary 00101111 is:

00101111 (binary) = 47 (decimal)

Binary to Decimal (Negative Numbers)

Convert a two’s complement binary 11010001 to decimal.

Since, sign bit (MSB) is 1, so, it is a negative number. Following steps 3–6:

Invert: 00101110

Add 1: 00101111

Converting 00101111 to decimal gives 47.

Apply Sign = –47

Hence, the binary 11010001 in decimal is:

00101111 (binary) = –47 (decimal)

Conclusion

Two’s complement is the standard method computers use to represent signed integers. It allows efficient storage and uniform arithmetic for both positive and negative numbers.

By understanding how two’s complement and sign bits work, you gain insight into how computers handle arithmetic, memory, and real-world values like temperatures or bank balances. It is a simple yet powerful system at the core of modern computing.

Frequently Asked Questions (FAQs)

Explain how a negative integer is represented in binary.

Negative integers are represented using two’s complement:

  • Write the positive number in binary.
  • Invert all bits.
  • Add 1 to the result.

The most significant bit (MSB) becomes 1 to indicate a negative number.

What is the benefit of using unsigned integers?

Unsigned integers can represent larger positive values using the same number of bits, since no bits are used for the sign.

How does the number of bits affect the range of integer values?

More bits allow a wider range of values. For instance:

  • Unsigned Integer: 0 to 2ⁿ − 1
  • Signed Integer: –2ⁿ⁻¹ to 2ⁿ⁻¹ − 1

Where n is the number of bits.

What is the primary difference between signed and unsigned integers?

  • Signed integers store both positive and negative values.
  • Unsigned integers store only non-negative values (zero and positive numbers).

How many bytes are used to store a typical integer? (a) 1 byte (b) 2 bytes (c) 4 bytes (d) 8 bytes

(c) 4 bytes

Though it can vary, 4 bytes (32 bits) is the common size for standard integers.

Describe in detail how integers are stored in computer memory.

Integers are stored in binary format using a fixed number of bits (e.g., 8, 16, and 32).

  • Unsigned integers use all bits for value.
  • Signed integers reserve the MSB as the sign bit, using two’s complement to represent negatives.

The value is split into bits and stored in memory addresses depending on endianness (byte order).

Explain the process of converting a decimal integer to its binary representation and vice versa.

Decimal to Binary (Positive):

  • Divide the number by 2 repeatedly.
  • Record remainders from bottom to top.

Decimal to Binary (Negative):

  • Convert a positive value to binary.
  • Invert all bits.
  • Add 1 (two’s complement).

Binary to Decimal:

  • For positive: Multiply bits by powers of 2 and sum.
  • For negative: Use two’s complement to convert to positive, then apply a minus sign.

What are the advantages and disadvantages of 2’s complement?

Advantages

  • Simplifies arithmetic (same circuits for addition and subtraction).
  • Only one representation for zero.
  • Easy negation (invert + add 1).

Disadvantages

  • Fixed size limits the range.
  • Overflow can occur silently.
  • Not intuitive for humans to read negative values.

What is 2’s complement in computer science?

Two’s complement is a binary method used to represent negative integers. It allows arithmetic to be performed the same way for both positive and negative numbers, simplifying hardware design.

What are signed integers?

Signed integers are whole numbers that can be positive, negative, or zero. They are stored in binary using systems like two’s complement to handle the sign.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.