Data Representation - Part I
Representing Numbers Choosing an appropriate representation is a critical decision a computer designer has to make The chosen representation must allow for efficient execution of primitive operations For general-purpose computers, the representation must allow efficient algorithms for (1) addition of two integers (2) determination of additive inverse
With a sequence of N bits, there are 2 N unique representations Each memory cell can hold N bits The size of the memory cell determines the number of unique values that can be represented The cost of performing operations also increases as the size of the memory cell increases It is reasonable to select a memory cell size such that numbers that are frequently used are represented
Binary Representation The binary, weighted positional notation is the natural way to represent non-negative numbers SAL and MAL number the bits from right to left, i.e., beginning with 0 as the least significant digit
Little-Endian vs. Big-Endian Numbering the bits from right to left, beginning with zero is called Little Endian byte order. Intel 80x86 and DECstation 3100 use the Little Endian byte ordering. Numbering the bits from left to right, beginning with zero is called Big Endian byte order. SunSparc and Macintosh use the Big- Endian byte ordering.
Representation of Integers Unsigned Integer Representation Sign Magnitude Complement Representation Biased Representation Sign Extension
Unsigned Integer Representation The representation of a non-negative integer using binary, weighted positional notation is called unsigned integer representation Given n bits, it is possible to represent the range of values from 0 to 2 n - 1 For example an 8-bit representation would allow representations that range 0 to 255
Sign Magnitude An extra bit in the most significant position is designated as the sign bit which is to the left of an unsigned integer. The unsigned integer is the magnitude. A 0 in the sign bit means positive, and a 1 means negative xxx xxxx x
Given an n+1 -bit sign magnitude number the range of values that it can represent is -(2 n -1) to +(2 n -1) Sign magnitude representation associates a sign bit with a magnitude that represents zero, thus it has two distinct representation of zero: and sign bit magnitude
Complement Representation For positive integers, the representation is the same as for sign magnitude For negative numbers, a large bias is added to all negative numbers, creating positive numbers in unsigned representation The bias is chosen so that any negative number representable appears as if it were larger than the largest positive number representable
One’s Complement For positive numbers, the representation is the same as for unsigned integers where the most significant bit is always zero The additive inverse of a one’s complement representation is found by inverting each bit. Inverting each bit is also called taking the one’s complement
Example (3) (-3) (-23) (23) (0) (0) Note: There are two representations of zero
Two’s complement The additive inverse of a two’s complement integer can be obtained by adding 1 to its one’s complement The two’s complement representation for a negative number is the additive inverse of its positive representation An advantage of two’s complement is that there is only one representation for zero
Example (17) (-24) (-17) (24) take the 1’s complement
In two’s complement, one more negative value than positive value is represented - the most negative number has no additive inverse within a fixed precision. For example, has no additive inverse for 8-bit precision. Taking the two’s complement will yield which seems its own additive inverse. This is incorrect and is an example of an overflow. Note that computing the additive inverse is a mathematical operation. Taking the complement is an operation on the representation.
Biased Representation If the unsigned representation includes integers from 0 to M, then subtracting approximately M/2 from the unsigned interpretation would shift the range from -(M/2) to +(M/2) If a sequence has a value N when interpreted as an unsigned integer, it has a value N-bias interpreted as a biased number Usually the bias is either 2 n or 2 n -1 for an (n+1) bit representation
Example 9.3 Assume a 3-bit representation. A possible bias is 2 n-1, which is 4. The following is a 3-bit representation with a bias of 4. bit patterninteger represented (in decimal)
Example 9.4 Given , what is it’s value in a biased-127 representation. Assume an 8-bit representation. The value of the unsigned integer: =6 10 Its value in biased-127 is: = -121
Sign Extension For integer representations, the sizes are commonly 8, 16, 32 and 64. It is occasionally necessary to convert an integer representation from one size to another, e.g., from 8 bits to 32 bits. The point is to maintain the same value while changing the size of the representation
Sign Extension - Unsigned Place the original integer into the least significant portion and stuff the remaining positions with 0 ’s. xxxxxxxx xxxxxxxx 8 bits 16 bits
Sign Extension - Signed The sign bit of the smaller representation is placed into the sign bit of the larger representation The magnitude is put into the least significant portion and all remaining positions are stuffed with 0 ’s. sxxxxxxx s xxxxxxx
Sign Extension - complement For positive number, a 0 is used to stuff the remaining positions. For negative number, a 1 is used to stuff the remaining positions. 0xxxxxxx xxxxxxx 1xxxxxxx xxxxxxx The original number is placed into the least significant portion The original number is placed into the least significant portion