Omit Section 2.7, and the chapter appendix 2A.
Binary examples
- Bits, bytes and nibbles.
- Bits easy to represent physically.
- Group bits to make larger symbols.
- Eight bits is a byte.
- Half a byte is a nibble.
- Binary system.
- Standard numbers are base 10. Can use any base.
- Binary (base 2) works nicely with bits.
- Conversion between bases.
- To decimal
- By place value.
- By doubling (binary).
- From decimal
- By place value.
- By remainders.
- Octal and hexadecimal.
- Addition and subtraction in binary.
- Numbers are fixed in size.
- There finitely many of them.
- Overflow: Results maybe outside the representable range.
- There is a left-most bit.
- Signed and unsigned.
- Unsigned numbers. Range for n bits is 0–2n−1.
- Signed magnitude.
- Use the left-most bit for the sign, usually 1 for negative.
- Range −(2n−1−1)–2n−1−1
- Two ways to write zero.
- Operations must special-case on the sign bit.
- Two's complement.
- Usual representation these days.
- Represent −x as 2n−x in n bits.
- Easily computed as (2n−1)−x+1.
Invert the bits and add 1.
- When doing arithmetic, the extra 2n falls off the left.
(2n−x)+x=2n, but that bit falls off the left,
so the result is zero.
Takes advantage of the fixed size.
- A left bit of one identifies negative numbers.
- Range −2n−1–2n−1−1
- Zero is unique; extra negative value.
- Enlarge by extending the sign bit.
- One's complement.
- Older alternative to two's complement.
- Represent −x as 2n−1−x in n bits.
- Range −2n−1−1–2n−1−1
- Negative and positive zero.
- Operations on fixed-size binary numbers.
- Change the representation size.
- Shortening
- Remove bits on the left.
- Accurate so long as the sign bit doesn't change, and no different
bit is removed.
- Lengthening: Add copies of the sign bit at the left.
- Why does it work?
- For positive numbers, adding or removing zeros on the left
does not change the value.
- Lengthen negative by one bit:
(2n−x)+2n=2×2n−x=2n+1−x
- Shorten negative by one bit:
(2n−x)−2n−1=2×2n−1−2n−1−x=2n−1−x
- Addition.
- Carry-out: When the bit left of your number is a one.
- Overflow: When the result doesn't fit in your bit size.
- Unsigned numbers.
- Add normally.
- Carry-out is overflow.
- Two's complement.
- Add normally.
- Carry-out is not equivalent to overflow.
- Overflow is determined by sign.
- Adding a negative and a positive cannot overflow.
Will be a carry-out if the result is positive.
- Adding two negatives or two positives is an overflow if the
result has the wrong sign.
- Alternative: carry into the sign bit should equal the carry out.
- Subtraction.
- For two's complement, compute a−b as a+(−b)
- Same operation works for unsigned, but overflow is the lack
of carry-out.
- Multiplication.
- Simple multiplication.
- Multiply by each digit in the multiplicand.
- Add the products allowing for place value.
- Must special-case negative multiplier.
- Booth's Algorithm.
- Works on tc without special case for negative.
- Treats runs of 1's as a group.
- A run of k one's:
2i−1+2i−2+…+2i−k is treated as
2i−2i−k.
- On the right end of a run of 1's, subtract the multiplicand
(properly shifted): contributes −2i−k.
- Just left of the run, add the multiplicand, which
contributes 2i.
- Some examples here, and more on
pp. 80 and 81 in your text.
- Reduces the number of adds when there are runs of 1's.
- Can increase the number of adds when there are lots of single ones.
- Shifting multiplies (or divides) by powers of two.
- Floating point organization.
- Binary and fractional place values.
- Exponential notation, decimal and binary.
- Formats.
Text simple format:
S
5 exp
8 sig
IEEE single
precision:
S
8-bit exp
23 significand bits
IEEE double
precision:
S
11-bit exp
52 significand bits
- Signed-magnitude.
- Implied 1 on the significand (IEEE format).
- Biased exponent notation.
- Represent the exponent as the true value plus a constant bias.
- Allows negative exponents to be represented.
- Bias near the center of exponent range.
- 15 for book format.
- 127 for 32-bit IEEE.
- 1023 for 64-bit IEEE.
- Special values.
Zero | All zero bits |
+/- Infinity | S 11111111 000...000 |
NaN | S 11111111 non-zero significand |
- Designed so integer comparisons are correct.
- Range of double:
- Errors accumulate.
- Basic math rules can be violated: a+(b+c)≠(a+b)+c
for some values.
- Binary-coded decimal.
- Packed. Two digits per byte.
- Zoned, which is really just character data.
- Sometimes used for business calculations.
- Character codes.
- ASCII. 7-bit code often extended to 8.
- EBCDIC. IBM mainframes only. 8-bit code.
- Unicode. To allow many, many more characters so any language can
be coded.
- Assigns “codepoints” (numbers) to any number of characters.
- Often said to be a 16-bit system, but there is no limit on the
number of codepoints.
- Three encodings.
- Simplest is UTF-32. Just represent each character as a 32-bit
number.
- UTF-8.
- Variable-length codes, multiples of 8 bits.
- A plain 7-bit ascii file is also a valid UTF-8 file. Keeps
from breaking existing software.
- UTF-16.
- Variable size codes, minimum 16 bits.
- Used internally in some langauges and systems, but not
often written to files.
- We will not cover the section on error detection and correction.
Questions
Chapter questions:
4th or 5th ed:
1, 2, 16, 20, 23, 27, 32, 34, 36, 43, 51.
3rd ed: 1, 2, 6, 8, 10, 25, 33.
Consider the 8-bit twos complement number 11100101. What does it
look like as 16-bit?
Consider the number (positive) 56. What is the smallest number
of bits that can be stored in as a twos complement number?
Express the number 48713 as packed BCD in 32 bits.