codes. outline binary coded decimal (bcd) gray code binary-to-gray conversion gray-to-binary...

25
Codes

Upload: silas-sparks

Post on 30-Dec-2015

261 views

Category:

Documents


7 download

TRANSCRIPT

Codes

Outline

Binary Coded Decimal (BCD) Gray Code

Binary-to-Gray Conversion Gray-to-Binary Conversion

Other Decimal Codes

Self-Complementing Codes

Alphanumeric Codes

Error Detection Codes

Outline

Binary Coded Decimal (BCD) Gray Code

Binary-to-Gray Conversion Gray-to-Binary Conversion

Other Decimal Codes

Self-Complementing Codes

Alphanumeric Codes

Error Detection Codes

Binary Coded Decimal (BCD)

Decimal numbers are more natural to humans. Binary numbers are natural to computers. Quite expensive to convert between the two.

If little calculation is involved, we can use some coding schemes for decimal numbers.

One such scheme is BCD, also known as the 8421 code.

Represent each decimal digit as a 4-bit binary code.

Binary Coded Decimal (BCD)

Some codes are unused, eg: (1010)BCD, (1011) BCD, …, (1111)

BCD. These codes are considered as errors.

Easy to convert, but arithmetic operations are more complicated.

Suitable for interfaces such as keypad inputs and digital readouts.

Decimal digit 0 1 2 3 4BCD 0000 0001 0010 0011 0100Decimal digit 5 6 7 8 9BCD 0101 0110 0111 1000 1001

Binary Coded Decimal (BCD)

Examples:

(234)10 = (0010 0011 0100)BCD

(7093)10 = (0111 0000 1001 0011)BCD

(1000 0110)BCD = (86)10

(1001 0100 0111 0010)BCD = (9472)10

Notes: BCD is not equivalent to binary.

Example: (234)10 = (11101010)2

Decimal digit 0 1 2 3 4BCD 0000 0001 0010 0011 0100Decimal digit 5 6 7 8 9BCD 0101 0110 0111 1000 1001

Outline

Binary Coded Decimal (BCD) Gray Code

Binary-to-Gray Conversion Gray-to-Binary Conversion

Other Decimal Codes

Self-Complementing Codes

Alphanumeric Codes

Error Detection Codes

Gray Code

Unweighted (not an arithmetic code). Only a single bit change from one code number to the next. Good for error detection.

Decimal Binary Gray Code Decimal Binary Gray code0 0000 0000 8 1000 11001 0001 0001 9 1001 11012 0010 0011 10 1010 11113 0011 0010 11 1011 11104 0100 0110 12 1100 10105 0101 0111 13 1101 10116 0110 0101 14 1110 10017 0111 0100 15 1111 1000

Q. How to generate 5-bit standard Gray code?Q. How to generate n-bit standard Gray code?

Gray Code

0000000100110010

0110011101010100

00010000

0010001100010000

01000101011101100010001100010000

11001101111111101010101110011000

Generating 4-bit standard Gray code.

Gray Code

0

0

1

0 0

0

0 1 0 0

1

1

1 0 1

1 1 0 1 1 1

1 0 0

sensors

Binary coded: 111 110 000

0

0

0

1 1

1

0 1 0 0

0

1

1 0 1

0 1 1 1 0 0

1 1 0

mis-aligned sensors

1

0

1

1 1

1

0 0 0 1

0

0

0 1 00 0 1 0 1 1

1 1 0

mis-aligned sensors

Gray coded: 111 101

Binary-to-Gray Code Conversion

Retain most significant bit. From left to right, add each adjacent pair of binary code bits to

get the next Gray code bit, discarding carries. Example: Convert binary number 10110 to Gray code.

1 0 1 1 0 Binary

1 Gray

1 + 0 1 1 0 Binary

1 1 Gray

1 0 + 1 1 0 Binary

1 1 1 Gray

1 0 1 + 1 0 Binary

1 1 1 0 Gray

1 0 1 1 + 0 Binary

1 1 1 0 1 Gray

(10110)2 = (11101)Gray

Gray-to-Binary Conversion

Retain most significant bit. From left to right, add each binary code bit generated to the

Gray code bit in the next position, discarding carries. Example: Convert Gray code 11011 to binary.

1 1 0 1 1 Gray

1 Binary

1 1 0 1 1 Gray

+

1 0 Binary

1 1 0 1 1 Gray

+

1 0 0 Binary

1 1 0 1 1 Gray

+

1 0 0 1 Binary

1 1 0 1 1 Gray

+

1 0 0 1 0 Binary

(11011)Gray = (10010)2

Outline

Binary Coded Decimal (BCD) Gray Code

Binary-to-Gray Conversion Gray-to-Binary Conversion

Other Decimal Codes

Self-Complementing Codes

Alphanumeric Codes

Error Detection Codes

Other Decimal Codes

Self-complementing codes: excess-3, 84-2-1, 2*421 codes.

Error-detecting code: biquinary code (bi=two, quinary=five).

Decimal Digit BCD8421

Excess-3 84-2-1 2*421 Biquinary5043210

0 0000 0011 0000 0000 01000011 0001 0100 0111 0001 01000102 0010 0101 0110 0010 01001003 0011 0110 0101 0011 01010004 0100 0111 0100 0100 01100005 0101 1000 1011 1011 10000016 0110 1001 1010 1100 10000107 0111 1010 1001 1101 10001008 1000 1011 1000 1110 10010009 1001 1100 1111 1111 1010000

Outline

Binary Coded Decimal (BCD) Gray Code

Binary-to-Gray Conversion Gray-to-Binary Conversion

Other Decimal Codes

Self-Complementing Codes

Alphanumeric Codes

Error Detection Codes

Self-Complementing Codes

Examples: excess-3, 84-2-1, 2*421 codes. The codes that represent the pair of complementary digits are

complementary of each other.

Excess-3 code 0: 00111: 01002: 01013: 01104: 01115: 10006: 10017: 10108: 10119: 1100

241: 0101 0111 0100

758: 1010 1000 1011

Outline

Binary Coded Decimal (BCD) Gray Code

Binary-to-Gray Conversion Gray-to-Binary Conversion

Other Decimal Codes

Self-Complementing Codes

Alphanumeric Codes

Error Detection Codes

Alphanumeric Codes

Apart from numbers, computers also handle textual data.

Character set frequently used includes:

alphabets: ‘A’ .. ‘Z’, and ‘a’ .. ‘z’

digits: ‘0’ .. ‘9’

special symbols: ‘$’, ‘.’, ‘,’, ‘@’, ‘*’, …

non-printable: SOH, NULL, BELL, …

Usually, these characters can be represented using 7 or 8 bits.

Alphanumeric Codes

ASCII: 7-bit, plus a parity bit for error detection (odd/even parity).

EBCDIC: 8-bit code.

Character ASCII Code0 01100001 0110001

. . . . . .9 0111001: 0111010A 1000001B 1000010

. . . . . .Z 1011010[ 1011011\ 1011100

Two widely used standards:Two widely used standards:

ASCII (American Standard Code for Information Interchange)ASCII (American Standard Code for Information Interchange)

EBCDIC (Extended BCD Interchange Code)EBCDIC (Extended BCD Interchange Code)

Alphanumeric Codes

ASCII table:

MSBsLSBs 000 001 010 011 100 101 110 1110000 NUL DLE SP 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 “ 2 B R b r0011 ETX DC3 # 3 C S c s0100 EOT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ‘ 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 O RS . > N ^ n ~1111 SI US / ? O _ o DEL

Outline

Binary Coded Decimal (BCD) Gray Code

Binary-to-Gray Conversion Gray-to-Binary Conversion

Other Decimal Codes

Self-Complementing Codes

Alphanumeric Codes

Error Detection Codes

Error Detection Codes

Errors can occur data transmission. They should be detected, so that re-transmission can be requested.

With binary numbers, usually single-bit errors occur.

Example: 0010 erroneously transmitted as 0011, or 0000, or 0110, or 1010.

Biquinary code uses 3 additional bits for error-detection. For single-error detection, one additional bit is needed.

Error Detection Codes

Parity bit. Even parity: additional bit supplied to make total number of

‘1’s even. Odd parity: additional bit supplied to make total number of ‘1’s

odd.

Example: Odd parity.Character ASCII Code

0 0110000 11 0110001 0

. . . . . .9 0111001 1: 0111010 1A 1000001 1B 1000010 1

. . . . . .Z 1011010 1[ 1011011 0\ 1011100 1

Parity bits

Error Detection Codes

Parity bit can detect odd number of errors but not even number of errors.

Example: For odd parity numbers,

10011 10001 (detected)

10011 10101 (non detected)

Parity bits can also be applied to a block of data: 0110 1

0001 01011 01111 11001 10101 0 Column-wise parity

Row-wise parity

Error Detection Codes

Sometimes, it is not enough to do error detection. We may want to do error correction.

Error correction is expensive. In practice, we may use only single-bit error correction.

Popular technique: Hamming Code (not covered).