14.5: Negative Numbers

Deep into Pharo · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-SA 3.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    14.5: Negative Numbers

    To know the value of a positive number is simple: we just add all the powers of 2 given by the binary representation as explained at the beginning of this chapter. Getting the value of a negative number is quite simple: we do the same except that we count the sign bit as negative and all the other ones as positive. The sign bit is the most significant bit i.e., the bit that represents the largest value (see Figure \(\PageIndex{1}\)). For example, on 8 bit representation it will be the one associated with the weight \( 2^7 \).

    Negative numbers in 8 bits.
    Figure \(\PageIndex{1}\): Negative numbers on 8 bits.

    Let us illustrate that: −2 is represented on 8 bit encoding as: 1111 1110.

    To get the value out of the bit representation, we simple add: \( -2^7 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 0 * 2^0 \), i.e., \( -128 + 64 + 32 + 16 + 8 + 4 + 2 \) and we get \( -2 \).

    \( -69 \) is represented on 8 bit encoding as: \( 1011 1011 \). To get the value out of the bit representation is simple. We add \( -2^7 + 0 * 2^6 + 2^5 + 2^4 + 2^3 + 0 * 2^2 + 2^1 + 2^0 \), i.e., \( -128 + 32 + 16 + 8 + 2 + 1 \) and we get \( -69 \).

    Following the same principle, check that the value of \( -1 \) is the one described in Figure \(PageIndex{1}\).

    Let us count a bit: on an 8 bit representation we can then encode 0 to 255 positive integers or -128 to \( 64 + 32 + 16 + 8 + 4 + 2 + 1 \) 127. In fact we can encode from \( -1 * 2^7 \) to \( 2^7 - 1 \). More generally on \( N \) bit we can encode \( -1 * 2^{N-1} - 1 \) integer values.


    This page titled 14.5: Negative Numbers was last modified on Mon, 26 Jul 2021 15:16:43 GMT and is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Alexandre Bergel, Damien Cassou, Stéphane Ducasse, Jannik Laval (Square Bracket Associates) via source content that was edited to the style and standards of the LibreTexts platform.