Category: Binary & Logic

  • decimal to two’s complement: Convert Signed Values at a Fixed Width

    decimal to two’s complement: Convert Signed Values at a Fixed Width

    To perform decimal to two’s complement conversion, declare the bit width first. For an 8-bit signed value, the range is −128 through +127. Positive values use ordinary binary padded with leading zeros; negative values use the invert-and-add-one method.

    For two’s complement to decimal decoding, inspect the leftmost bit. A 0 means use ordinary positive binary weights. A 1 means the value is negative, so either use a negative sign-bit weight or invert the bits, add one, and negate the result.

    Choose an 8-bit width and represent positive values such as +13

    Bit width is part of the representation because the same visible bits can have different meanings at different widths. For example, 1101 is an unsigned binary value, but an 8-bit signed representation must contain exactly eight bits.

    To represent +13 in 8-bit two’s complement:

    1. Convert the magnitude, 13, to binary: 1101.
    2. Pad on the left with zeros until there are eight bits: 00001101.
    3. Because the first bit is 0, the pattern represents a positive value.

    Check the result by adding the weights of the 1 bits: 8 + 4 + 1 = 13. Thus, the 8-bit pattern 00001101 converts back to +13.

    Convert a negative decimal value using decimal to two’s complement and binary to two’s complement steps

    For a negative decimal value, first write the positive magnitude at the declared width. Then apply the standard binary to two’s complement process: invert every bit and add one.

    Convert −13 to an 8-bit pattern as follows:

    1. Write positive 13 in eight bits: 00001101.
    2. Invert every bit: 11110010.
    3. Add one: 11110011.

    Therefore, −13 is represented as 11110011 in 8-bit two’s complement. The addition is binary addition, so 11110010 + 1 produces 11110011 without changing the width.

    The round-trip check confirms the result. Start with 11110011, invert it to 00001100, add one to get 00001101, and read that magnitude as 13. Since the original pattern had a sign bit of 1, the decoded result is −13.

    Decode two’s complement to decimal using signed bit weights

    For two’s complement to decimal conversion, use the leftmost bit as the sign bit and apply signed weights. In an 8-bit value, the weights from left to right are:

    −128, 64, 32, 16, 8, 4, 2, 1

    This makes the decoding path direct:

    • For 00001101, add the weights under the 1 bits: 8 + 4 + 1 = +13.
    • For 11110011, add the signed weights under the 1 bits: −128 + 64 + 32 + 16 + 2 + 1 = −13.

    The sign bit changes the interpretation rather than simply adding a positive 128. A leading 0 contributes no sign weight, while a leading 1 contributes −128 in an 8-bit representation. This weighted method provides a second check against the invert-and-add-one method.

    Check the 8-bit signed range, overflow, and round-trip results

    An n-bit two’s-complement representation has the range −2n−1 through 2n−1 − 1. With eight bits, that is −27 through 27 − 1, or −128 to +127. The negative side has one extra value because zero uses the positive sign pattern.

    • Valid positive limit: +127 is 01111111.
    • Valid negative limit: −128 is 10000000.
    • Overflow: +128 and −129 cannot be represented as signed 8-bit values.

    Before converting, verify that the decimal value falls within the selected range. After converting, decode the resulting bit pattern back to decimal. The positive round trip is +13 → 00001101 → +13, and the negative round trip is −13 → 11110011 → −13. If the final value differs, check the declared width, padding, bit inversion, and add-one step.