Skip to content

Instantly share code, notes, and snippets.

@prateekrajgautam
Last active February 3, 2025 03:47
Show Gist options
  • Save prateekrajgautam/bc889187fa7edfe2d3fc548f4e3d1ddb to your computer and use it in GitHub Desktop.
Save prateekrajgautam/bc889187fa7edfe2d3fc548f4e3d1ddb to your computer and use it in GitHub Desktop.

Understanding Binary Numbers

Before diving into signed and unsigned representations, it's essential to grasp the basics of binary numbers. Binary is a base-2 numeral system, meaning it only uses two digits: 0 and 1. Each digit in a binary number is called a bit. In a 4-bit binary number, there are four positions, each representing a power of 2, starting from the right (which is (2^0)).

Example of a 4-bit binary number:

1 0 1 1

Calculating its decimal equivalent: [ 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^0 = 8 + 0 + 2 + 1 = 11 ] So, 1011 in binary is 11 in decimal.

Unsigned Binary Numbers

Definition: Unsigned binary numbers represent only non-negative integers. All bits are used to denote the magnitude of the number.

Range for 4-bit Unsigned Binary:

  • Minimum Value: 0000 which is 0 in decimal.
  • Maximum Value: 1111 which is (2^4 - 1 = 15) in decimal.

Representation:

0000 = 0
0001 = 1
0010 = 2
...
1111 = 15

Key Points:

  • No sign bit; all bits contribute to the magnitude.
  • Simple and straightforward for non-negative numbers.

Signed Binary Numbers

Signed binary numbers can represent both positive and negative integers. There are different methods to represent signed numbers, but the most common are:

  1. Sign-Magnitude Representation
  2. One's Complement Representation
  3. Two's Complement Representation

We'll explore each method using 4-bit binary numbers.

1. Sign-Magnitude Representation

Definition: In sign-magnitude, the most significant bit (MSB) represents the sign of the number (0 for positive, 1 for negative), and the remaining bits represent the magnitude.

Range for 4-bit Sign-Magnitude:

  • Minimum Value: 1111 which is -7 in decimal.
  • Maximum Value: 0111 which is +7 in decimal.

Representation:

0000 = +0
0001 = +1
0010 = +2
...
0111 = +7
1000 = -0
1001 = -1
1010 = -2
...
1111 = -7

Issues:

  • Two representations for zero (+0 and -0), which can complicate computations.
  • Arithmetic operations are more complex due to the separate handling of the sign bit.

2. One's Complement Representation

Definition: In one's complement, negative numbers are represented by inverting all the bits of the positive number (including the sign bit).

Range for 4-bit One's Complement:

  • Minimum Value: 1000 which is -7 in decimal.
  • Maximum Value: 0111 which is +7 in decimal.

Representation:

0000 = +0
0001 = +1
0010 = +2
...
0111 = +7
1000 = -7
1001 = -6
1010 = -5
...
1111 = -0

Issues:

  • Still has two representations for zero (+0 and -0).
  • End-around carry is needed for addition, making arithmetic operations slightly more complex.

3. Two's Complement Representation

Definition: Two's complement is the most common method for representing signed numbers. A negative number is represented by inverting all the bits of the positive number and then adding 1 to the least significant bit (LSB).

Range for 4-bit Two's Complement:

  • Minimum Value: 1000 which is -8 in decimal.
  • Maximum Value: 0111 which is +7 in decimal.

Representation:

0000 = 0
0001 = +1
0010 = +2
...
0111 = +7
1000 = -8
1001 = -7
1010 = -6
...
1111 = -1

Advantages:

  • Only one representation for zero.
  • Simplifies arithmetic operations; no need for special handling of the sign bit.
  • Wider range compared to sign-magnitude and one's complement.

Example Calculation: To find the two's complement of 5 (0101):

  1. Invert the bits: 01011010
  2. Add 1: 1010 + 1 = 1011 which represents -5.

Summary of Ranges

Representation Minimum Value Maximum Value
Unsigned 0 15
Sign-Magnitude -7 +7
One's Complement -7 +7
Two's Complement -8 +7

Practical Implications

  • Unsigned Binary: Ideal for scenarios where only non-negative numbers are needed, such as counting or indexing.
  • Signed Binary (Two's Complement): Preferred in most computing systems for its efficiency in handling both positive and negative numbers, especially in arithmetic operations.

Common Mistakes and Clarifications

  1. Confusing Sign-Magnitude with Two's Complement:

    • It's crucial to distinguish between these methods as they handle negative numbers differently.
    • Two's complement avoids the issue of dual zero representations and simplifies subtraction by turning it into addition.
  2. Misinterpreting the Range:

    • Always verify the range based on the number of bits and the representation method.
    • For example, in 4-bit two's complement, the range is from -8 to +7, not -7 to +7.
  3. Overflow in Arithmetic Operations:

    • When performing arithmetic with signed numbers, be cautious of overflow, which occurs when the result exceeds the representable range.
    • For instance, adding 7 (0111) and 1 (0001) in 4-bit two's complement results in 8, which is 1000 and represents -8, leading to an overflow.

Final Answer

In summary, understanding signed and unsigned binary numbers is fundamental in computer science and digital electronics. Here's a concise breakdown using 4-bit numbers:

  • Unsigned Binary:

    • Range: 0 to 15
    • Example: 1011 represents 11
  • Signed Binary:

    • Sign-Magnitude:
      • Range: -7 to +7
      • Example: 1011 represents -3
    • One's Complement:
      • Range: -7 to +7
      • Example: 1011 represents -4
    • Two's Complement:
      • Range: -8 to +7
      • Example: 1011 represents -5

Key Takeaways:

  • Unsigned is for non-negative numbers.
  • Signed methods allow representation of negative numbers, with Two's Complement being the most efficient and widely used due to its simplicity in arithmetic operations and single zero representation.

By mastering these concepts, you'll have a solid foundation for understanding how computers represent and manipulate numerical data.

Understanding Two's Complement Representation

Two's complement is a method used in computing to represent signed integers. It is widely used because it simplifies the design of arithmetic circuits and allows for easy addition and subtraction of both positive and negative numbers.

Steps to Represent -8 in 4-bit Two's Complement

Let's go through the process of representing -8 in 4-bit two's complement step by step.

Step 1: Determine the Binary Representation of the Positive Number

First, find the binary representation of the positive counterpart of the number you want to represent. In this case, the positive number is 8.

However, in 4-bit binary, the maximum positive number we can represent is 7 (0111). The number 8 requires 5 bits to be represented (10000). This indicates that 8 is outside the range of 4-bit two's complement representation.

Step 2: Identify the Range for 4-bit Two's Complement

For an n-bit two's complement system:

  • The range of representable numbers is from (-2^{(n-1)}) to (2^{(n-1)} - 1).

For 4 bits:

  • Minimum value: (-2^{(4-1)} = -8)
  • Maximum value: (2^{(4-1)} - 1 = 7)

So, the range is from -8 to 7.

Step 3: Representing -8 in 4-bit Two's Complement

Since -8 is the minimum value in 4-bit two's complement, its representation is straightforward.

  1. Start with the binary representation of the positive number:

    • 8 in binary is 1000 (but as noted, this is actually -8 in two's complement).
  2. Invert all the bits (find the one's complement):

    • Inverting 1000 gives 0111.
  3. Add 1 to the least significant bit (LSB) to get the two's complement:

    • 0111 + 1 = 1000.

So, 1000 represents -8 in 4-bit two's complement.

Verification

To verify that 1000 indeed represents -8, let's convert it back to decimal.

  1. Identify the sign bit:

    • The most significant bit (MSB) is 1, indicating a negative number.
  2. Find the two's complement to get the positive counterpart:

    • Invert the bits: 10000111.
    • Add 1: 0111 + 1 = 1000.
  3. Convert the positive binary number to decimal:

    • 1000 in binary is 8 in decimal.

Since the original number was negative, 1000 represents -8.

Summary

  • 4-bit Two's Complement Range: -8 to 7
  • Representation of -8: 1000

Common Mistakes and Clarifications

  1. Misunderstanding the Range:

    • It's crucial to recognize that in 4-bit two's complement, -8 is the smallest number and is represented as 1000.
    • Attempting to represent +8 in 4 bits is not possible because it exceeds the maximum positive value (7).
  2. Incorrect Inversion:

    • When finding the two's complement, ensure you invert all bits correctly and add 1 to the LSB.
    • Skipping the addition of 1 leads to the one's complement, which is not the correct representation for two's complement.
  3. Sign Bit Confusion:

    • Always check the MSB to determine if the number is positive (0) or negative (1).
    • In 1000, the MSB is 1, confirming it's a negative number.

Final Answer

In 4-bit two's complement representation:

  • The binary number 1000 represents -8.
  • This is because:
    1. The MSB 1 indicates a negative number.
    2. Applying the two's complement process to 1000 yields 8, confirming that 1000 stands for -8.

Understanding this representation is fundamental for working with signed integers in computing, ensuring accurate arithmetic operations and data interpretation.

Fixed-Point and Floating-Point Notation & Normalization

Numbers in computers are represented using fixed-point or floating-point notation, depending on precision and range requirements.


1. Fixed-Point Notation

Fixed-point representation stores numbers with a fixed number of digits before and after the decimal (or binary) point.

Example:

Consider an 8-bit representation with 4 integer bits and 4 fractional bits (Q4.4 format):

Binary Decimal Equivalent
1010.1100 -5.25 (in 2's complement)
0011.1010 3.625
  • Pros: Faster operations, simpler hardware.
  • Cons: Limited range and precision.

2. Floating-Point Notation

Floating-point representation allows a wide range of values by using a mantissa (significand), exponent, and sign bit.

General Representation (Base 10)

[ N = \text{Sign} \times \text{Mantissa} \times \text{Base}^{\text{Exponent}} ]

IEEE 754 Standard (Single Precision - 32-bit)

Sign (1-bit) Exponent (8-bit) Mantissa (23-bit)
0 (positive) 10000011 (Exponent = 131, i.e., 4) 01000000000000000000000 (1.5 in mantissa)

Example in IEEE 754 Single Precision (32-bit)

For +12.375, we convert it to binary:

  • Binary: 1100.011
  • Normalized Form: 1.100011 × 2³
  • IEEE 754 Encoding:
    • Sign Bit: 0 (positive)
    • Exponent: 3 + 127 = 13010000010
    • Mantissa: 10001100000000000000000
  • Final Representation: 0 10000010 10001100000000000000000

3. Normalization in Floating-Point

Normalization ensures the mantissa has only one non-zero digit before the decimal.

Example:

  • 0.0001011 × 2⁴Not normalized
  • 1.011 × 2⁰Normalized

This allows for more efficient storage and calculations.


Q&A

Q1: Why is floating-point preferred over fixed-point in scientific computations?

A: Floating-point allows a wider range of values and greater precision, crucial for large-scale calculations.

Q2: What is the largest positive number in IEEE 754 single precision?

A: 01111111011111111111111111111111, which is approximately 3.4 × 10³⁸.

Q3: What is denormalization in floating-point numbers?

A: When the exponent is all zeros, the number is stored in a denormalized form, which helps represent very small values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment