Number Systems and Codes
Number Systems and Codes
Information Representations Number Systems Read up DLD for details! Base Conversion Negative Numbers Excess Representation Floating-Point Numbers Decimal codes: BCD, Excess-3, 2421, 84-2-1 Gray Code Alphanumeric Code Error Detection and Correction
Represent information precisely Can be processed Represent yes or no: use 0 and 1 Represent the 4 seasons: 0, 1, 2 and 3 Matriculation number: 8 alphanumeric characters (eg: U071234X)
Examples
0 and 1 Represent false and true in logic Represent the low and high states in electronic devices Byte: 8 bits Nibble: 4 bits (seldom used) Word: Multiples of byte (eg: 1 byte, 2 bytes, 4 bytes, 8 bytes, etc.), depending on the architecture of the computer system
Other units
Examples:
2 bits represent up to 4 values (00, 01, 10, 11)
3 bits rep. up to 8 values (000, 001, 010, , 110, 111) 4 bits rep. up to 16 values (0000, 0001, 0010, ., 1111)
Examples:
32 values requires 5 bits 64 values requires 6 bits 1024 values requires 10 bits 40 values how many bits? 100 values how many bits?
Base or radix is 10 (the base or radix of a number system is the total number of symbols/digits allowed in the system) Symbols/digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } Position is important, as the value of each symbol/digit is dependent on its type and its position in the number Example, the 9 in the two numbers below has different values:
(7594)10 = (7 103) + (5 102) + (9 101) + (4 100)
In general,
(anan-1 a0 . f1f2 fm)10 = (an x 10n) + (an-1x10n-1) + + (a0 x 100) + (f1 x 10-1) + (f2 x 10-2) + + (fm x 10m)
Weighing factors (or weights) are in powers of 10: 103 102 101 100 . 10-1 10-2 10-3 To evaluate the decimal number 593.68, the digit in each position is multiplied by the corresponding weight: 5 102 + 9 101 + 3 100 + 6 10-1 + 8 10-2 = (593.68)10
Weights in powers of 2 Binary digits (bits): 0, 1 Weights in powers of 8 Octal digits: 0, 1, 2, 3, 4, 5, 6, 7. Weights in powers of 16 Hexadecimal digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Weights in powers of R
Octal (base 8)
Base/radix R:
In programming language C
Prefix 0 for octal. Eg: 032 represents the octal number (32)8 Prefix 0x for hexadecimal. Eg: 0x32 represents the hexadecimal number (32)16
341.245 =
Method 1
Sum-of-Weights Method Repeated Division-by-2 Method (for whole numbers) Repeated Multiplication-by-2 Method (for fractions)
Method 2
SUM-OF-WEIGHTS METHOD
Determine the set of binary weights whose sum is equal to the decimal number
(9)10 = 8 + 1 = 23 + 20 = (1001)2 (18)10 = 16 + 2 = 24 + 21 = (10010)2 (58)10 = 32 + 16 + 8 + 2 = 25 + 24 + 23 + 21 = (111010)2 (0.625)10 = 0.5 + 0.125 = 2-1 + 2-3 = (0.101)2
REPEATED DIVISION-BY-2
To convert a whole number to binary, use successive
division by 2 until the quotient is 0. The remainders form the answer, with the first remainder as the least significant bit (LSB) and the last as the most significant bit (MSB). (43)10 = (101011)2
2 2 2 2 2 2 43 21 10 5 2 1 0 rem 1 LSB rem 1 rem 0 rem 1 rem 0 rem 1 MSB
REPEATED MULTIPLICATION-BY-2
To convert decimal fractions to binary, repeated
multiplication by 2 is used, until the fractional product is 0 (or until the desired number of decimal places). The carried digits, or carries, produce the answer, with the first carry as the MSB, and the last as the LSB. (0.3125)10 = (.0101)2
0.3125 2=0.625 0.625 2=1.25 0.25 2=0.50 0.5 2=1.00 Carry 0 1 0 1 MSB
LSB
Decimal to base-R
Whole numbers: repeated division-by-R Fractions: repeated multiplication-by-R
(10 111 011 001 . 101 110)2 = (2731.56)8 = (101 1101 1001 . 1011 1000)2 = (5D9.B8)16 =
READING ASSIGNMENT
Memory addressing
Memory
NEGATIVE NUMBERS
Unsigned numbers: only non-negative values. Signed numbers: include all values (positive and negative) There are 3 common representations for signed binary numbers:
SIGN-AND-MAGNITUDE (1/3)
0 for + 1 for -
sign
magnitude
SIGN-AND-MAGNITUDE (2/3)
Largest value: 01111111 = +12710 Smallest value: 11111111 = -12710 Zeros: 00000000 = +010 10000000 = -010 Range: -12710 to +12710 Question:
For an n-bit sign-and-magnitude representation, what is the range of values that can be represented?
SIGN-AND-MAGNITUDE (3/3)
How to negate 00100001sm (decimal 33)? Answer: 10100001sm (decimal -33) How to negate 10000101sm (decimal -5)? Answer: 00000101sm (decimal +5)
1s COMPLEMENT (1/3)
Given a number x which can be expressed as an n-bit binary number, its negated value can be obtained in 1scomplement representation using: -x = 2n x 1 Example: With an 8-bit number 00001100 (or 1210), its negated value expressed in 1s-complement is: -000011002 = 28 12 1 (calculation in decimal) = 243 = 111100111s (This means that -1210 is written as 11110011 in 1scomplement representation.)
1s COMPLEMENT (2/3)
Essential technique to negate a value: invert all the bits. Largest value: 01111111 = +12710 Smallest value: 10000000 = -12710 Zeros: 00000000 = +010 11111111 = -010 Range: -12710 to +12710 The most significant (left-most) bit still represents the sign: 0 for positive; 1 for negative.
1s COMPLEMENT (3/3)
Examples (assuming 8-bit numbers): (14)10 = (00001110)2 = (00001110)1s -(14)10 = -(00001110)2 = (11110001)1s -(80)10 = -( ? )2 = ( ? )1s
2s COMPLEMENT (1/3)
Given a number x which can be expressed as an n-bit binary number, its negated value can be obtained in 2scomplement representation using: -x = 2n x Example: With an 8-bit number 00001100 (or 1210), its negated value expressed in 2s-complement is: -000011002 = 28 12 (calculation in decimal) = 244 = 111101002s (This means that -1210 is written as 11110100 in 2scomplement representation.)
2s COMPLEMENT (2/3)
Essential technique to negate a value: invert all the bits, then add 1. Largest value: 01111111 = +12710 Smallest value: 10000000 = -12810 Zero: 00000000 = +010 Range: -12810 to +12710 The most significant (left-most) bit still represents the sign: 0 for positive; 1 for negative.
2s COMPLEMENT (3/3)
Examples (assuming 8-bit numbers): (14)10 = (00001110)2 = (00001110)2s -(14)10 = -(00001110)2 = (11110010)2s -(80)10 = -( ? )2 = ( ? )2s
READING ASSIGNMENT
Download from the course website and read the Supplement Notes on Lecture 2: Number Systems. Work out the exercises in there and discuss them in the IVLE forum if you have doubts.
COMPARISONS
4-bit system
Positive values
Value +7 +6 +5 +4 +3 +2 +1 +0 Sign-andMagnitude 0111 0110 0101 0100 0011 0010 0001 0000 1s Comp. 0111 0110 0101 0100 0011 0010 0001 0000
Important!
Negative values
2s Comp. 0111 0110 0101 0100 0011 0010 0001 0000
Value -0 -1 -2 -3 -4 -5 -6 -7 -8 Sign-andMagnitude 1000 1001 1010 1011 1100 1101 1110 1111 1s Comp. 1111 1110 1101 1100 1011 1010 1001 1000 2s Comp. 1111 1110 1101 1100 1011 1010 1001 1000
COMPLEMENT ON FRACTIONS
Negate 0101.01 in 1s-complement Answer: 1010.10 Negate 111000.101 in 1s-complement Answer: 000111.010 Negate 0101.01 in 2s-complement Answer: 1010.11
OVERFLOW
Signed numbers are of a fixed range. If the result of addition/subtraction goes beyond this range, an overflow occurs. Overflow can be easily detected:
positive add positive negative negative add negative positive Range of value: -810 to 710 01012s + 01102s = 10112s 510 + 610 = -510 ?! (overflow!) 10012s + 11012s = 101102s (discard end-carry) = 01102s -710 + -310 = 610 ?! (overflow!)
Any overflow?
+5 + -5 ----0 ----3 + -7 ----10 ---0101 + 1010 ------1111 ------1100 + 1000 ------10100 + 1 ------0101
complement schemes, the excess representation is another scheme. It allows the range of values to be distributed evenly between the positive and negative values, by a simple translation (addition/subtraction). Example: Excess-4 representation on 3-bit numbers. See table on the right.
Value -4 -3 -2 -1 0 1 2 3
111
EXCESS REPRESENTATION (2/2) Example: For 4-bit numbers, we may use excess-7 or
Value -8
Excess-8 Representation 1000 1001 1010 1011 1100 1101 1110 1111
Value
For example, if the binary point is at the end of an 8-bit representation as shown below, it can represent integers from -128 to +127.
binary point
integer part
Fixed point numbers have limited range. Floating point numbers allow us to represent very large or very small numbers. Examples:
0.23 1023 (very large positive number) 0.5 10-37 (very small positive number) -0.2397 10-18 (very small negative number)
3 parts: sign, mantissa and exponent The base (radix) is assumed to be 2. Sign bit: 0 for positive, 1 for negative.
sign mantissa exponent
Mantissa is usually in normalised form (the integer part is zero and the fraction part must not begin with zero)
0.01101 24 normalised 101011.0110 2-4 normalised
Trade-off:
More bits in mantissa better precision More bits in exponent larger range of values
Exponent is usually expressed in complement or excess format. Example: Express -6.510 in base-2 normalised form -6.510 = -110.12 = -0.11012 23 Assuming that the floating-point representation contains 1-bit, 5-bit normalised mantissa, and 4-bit exponent. The above example will be stored as if the exponent is in 1s or 2s complement. 1 11010 0011
Example: Express 0.187510 in base-2 normalised form 0.187510 = 0.00112 = 0.11 2-2 Assume this floating-point representation:1-bit sign, 5-bit normalised mantissa, and 4-bit exponent. The above example will be represented as 0 0 0 11000 11000 11000 1101 1110 0110
If exponent is in 1s complement. If exponent is in 2s complement. If exponent is in excess-8.
READING ASSIGNMENT
DLD page 31 DLD pages 32 - 33 IEEE standard 754 floating point numbers: http://steve.hollasch.net/cgindex/coding/ieeefloat.html
DECIMAL CODES
Decimal numbers are favoured by humans. Binary numbers are natural to computers. Hence, conversion is required. If little calculation is required, we can use some coding schemes to store decimal numbers, for data transmission purposes. Examples: BCD (or 8421), Excess-3, 84-2-1, 2421, etc. Each decimal digit is represented as a 4-bit code. The number of digits in a code is also called the length of the code.
BCD
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Self-complementing code: codes for complementary digits are also complementary to each other. Error-detecting code: biquinary code (bi=two, quinary=five).
Unweighted (not an arithmetic code) Only a single bit change from one code value to the next. Not restricted to decimal digits: n bits 2n values. Good for error detection. Example: 4-bit standard Gray code
Decimal 0 1 2 3 4 5 6 7 Binary 0000 0001 0010 0011 0100 0101 0110 0111 Gray Code 0000 0001 0011 0010 0110 0111 0101 0100 Decimal 8 9 10 11 12 13 14 15 Binary 1000 1001 1010 1011 1100 1101 1110 1111 Gray code 1100 1101 1111 1110 1010 1011 1001 1000
0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 1 0 1 0 1
0 0 1 0 1 0 1 1 0 0
0 1 1 0 0 1 1 0
1 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 1 1 1 0 0 0 0
0 0 1 1 1 1 0 0
0 1 1 0 0 1 1 0
Questions: How to generate 5-bit standard Gray code sequence? 6-bit standard Gray code sequence?
0 0
0 1
1 1 0
0
0
sensors
0 0 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0 1
0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 1 0
0 0 1 1 1 1
1 11 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0
0 10 0 1 0 1 1 1 1 1 0
mis-aligned sensors
0
1
1
1
0
0
0 00 0 1 1 1 1 0 1 1 0 1 0 0 1 1 0
1 1
1 1 1 1 1
1 11 0 1 0 1 1 1 1 1 0
mis-aligned sensors
0 11 01 0 1 1 1 1 0 1 1 1 0 0 1 0
0 0 1 0 1 1 1 1 0 0 1 0 0 0 1 1 0 1
READING ASSIGNMENT
Examples
ASCII
American Standard Code for Information Interchange 7 bits, plus a parity bit for error detection Odd or even parity
ASCII Code 0110000 0110001 ... 0111001 0111010 1000001 1000010 ... 1011010 1011011 1011100
ASCII table
LSBs 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 000 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR O SI 001 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 010 SP ! # $ % & ( ) * + , . / MSBs 011 100 0 @ 1 A 2 B 3 C 4 D 5 E 6 F 7 G 8 H 9 I : J ; K < L = M > N ? O 101 P Q R S T U V W X Y Z [ \ ] ^ _
A: 1000001
110 ` a b c d e f g h i j k l m n o 111 p q r s t u v w x y z { | } ~ DEL
Errors can occur during data transmission. They should be detected, so that re-transmission can be requested. With binary numbers, usually single-bit errors occur.
Parity bit
Even parity: additional bit added to make total number of 1s even. Odd parity: additional bit added to make total number of 1s odd.
ASCII Code 0110000 1 0110001 0 ... 0111001 1 0111010 1 1000001 1 1000010 1 ... 1011010 1 1011011 0 1011100 1
Parity bits
Parity bit can detect odd number of errors but not even number of errors.
Column-wise parity
Row-wise parity
Sometimes, it is not enough to do error detection. We may want to correct the errors. Error correction is expensive. In practice, we may use only single-bit error correction. Popular technique: Hamming code
Can C1 detect a single bit error? Can C1 correct a single bit error?
Sometimes, we use 1 error for single bit error, 2 errors for 2 bits error, etc.
The distance d between any two code words in a code is the sum of the number of differences between the codewords.
The Hamming distance of a code is the minimum distance between any two code words in the code.
What is its efficiency? What is its Hamming distance? Can it correct 1 error?
Parity bits are at positions that are powers of 2 (i.e. 1, 2, 4, 8, 16, ) All other positions are data bits Each parity bit checks some of the data bits
Position 1: Check 1 bit, skip 1 bit (1, 3, 5, 7, 9, 11, ) Position 2: Check 2 bits, skip 2 bits (2, 3, 6, 7, 10, 11, ) Position 4: Check 4 bits, skip 4 bits (4-7, 12-15, 20-23, ) Position 8: Check 8 bits, skip 8 bits (8-15, 24-31, 40-47, )
Set the parity bit accordingly so that total number of 1s in the positions it checks is even.
Answer: 0 1 1 1 0 0 1 0 1 0 1 0
Suppose 1 error occurred and the received data is: 011100101110 How to determine which bit is in error? Check which parity bits are in error.
Answer: parity bits 2 and 8. Answer: 2 + 8 = 10. Hence data bit 10 is in error.
Corrected data: 0 1 1 1 0 0 1 0 1 0 1 0
END