• Home
  • 4.8 Sign Extension

4.8 Sign Extension

View Categories

4.8 Sign Extension

Sign extension is the process of preserving the numerical value of a signed integer when it is converted to a wider signed integer type. In binary representation, this is commonly achieved by extending the sign bit into the newly added higher-order bits.

In C, converting a signed integer to a wider signed integer type generally preserves its value, provided the destination type can represent that value. However, the exact binary representation of signed integers depends on the C implementation.

Example Program #

#include <stdio.h>

int main(void)
{
    signed char small_positive = 10;
    signed char small_negative = -10;

    int positive = small_positive;
    int negative = small_negative;

    printf("Original positive = %d\n", small_positive);
    printf("Converted positive = %d\n", positive);

    printf("Original negative = %d\n", small_negative);
    printf("Converted negative = %d\n", negative);

    return 0;
}

Example Output #

Original positive = 10
Converted positive = 10
Original negative = -10
Converted negative = -10

Explanation #

1. Converting a positive value

signed char small_positive = 10;
int positive = small_positive;
  • small_positive stores the value 10.
  • When it is converted to int, the value remains 10.
  • On a typical system using two’s-complement representation, the binary value is extended with zeros.

2. Converting a negative value

signed char small_negative = -10;
int negative = small_negative;
  • small_negative stores the value -10.
  • Conversion to int preserves the negative numerical value.
  • On a typical two’s-complement system, the sign bit is extended into the additional higher-order bits.

Illustrative Binary Representation #

On a system using 8-bit signed char and 32-bit int with two’s-complement representation:

Value 8-bit representation 32-bit representation
10 00001010 00000000 00000000 00000000 00001010
-10 11110110 11111111 11111111 11111111 11110110

These binary patterns are illustrative of a common implementation, not a universal representation required for every C implementation.

Sign Extension vs. Zero Extension #

Feature Sign extension Zero extension
Typical use Widening a signed binary value Widening an unsigned binary value
Added higher-order bits Copies the sign bit Fills with zeros
Purpose Preserve a signed numerical value Preserve an unsigned numerical value
Example pattern 11110110 → 11111111...11110110 11110110 → 00000000...11110110

Important: C defines conversions between integer types in terms of their numerical values and the rules for the destination type. It does not require programmers to implement sign extension manually. When a signed integer value is converted to a wider signed type that can represent it, the numerical value is preserved.

Key takeaway: Sign extension is a binary-level concept that explains how negative signed values are commonly represented when widened. In normal C programming, assigning a value to a wider signed integer type handles the conversion automatically.

Powered by BetterDocs

Leave a Reply

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