UNIHIVE

Study Hub UNIPORT IFT 211

IFT 211 · Digital Logic Design

Codes: BCD, Gray, ASCII, EBCDIC and Unicode

How digits and characters are coded in bits, and where each code is used.

Our notes

2 min read

Why codes?

A code is an agreed meaning for bit patterns. Number codes make decimal display easy; character codes let computers store text.

BCD (8421 code)

Each decimal digit is written as 4 bits. 739 = 0111 0011 1001. Patterns 1010 to 1111 are not used. BCD wastes some space (pure binary 739 needs 10 bits, BCD needs 12) but drives seven-segment displays, calculators and money calculations without rounding.

Excess-3 and 2421

  • Excess-3: add 3 to each digit before coding: 5 → 1000. It is self-complementing (the 9's complement is found by flipping the bits).
  • 2421: another weighted code with the same property.

Gray code

Neighbouring values differ in only one bit: 0 = 000, 1 = 001, 2 = 011, 3 = 010, 4 = 110, 5 = 111, 6 = 101, 7 = 100. Used in shaft-position encoders so a reading taken during a change is never far wrong, and in Karnaugh map labels.

  • Binary to Gray: keep the first bit, then each Gray bit = this binary bit XOR the one before it. 1011 → 1110.

ASCII

The American Standard Code for Information Interchange uses 7 bits (128 characters): control codes 0 to 31, space 32, digits "0" to "9" at 48 to 57, "A" to "Z" at 65 to 90, "a" to "z" at 97 to 122. Lower case is upper case + 32 (one bit different). Extended 8-bit versions add accented letters, but they differ from one another.

EBCDIC

Extended Binary Coded Decimal Interchange Code, an 8-bit code from IBM mainframes. Letters are not in one continuous block, so programs that assume ASCII order fail on EBCDIC data. Still met in banking and older mainframe systems.

Unicode

One code for every writing system: Latin, Arabic, Chinese, and Nigerian letters such as ẹ, ọ, ṣ and the naira sign ₦ (U+20A6). Code points run to over a million. Stored as:

  • UTF-8: 1 to 4 bytes per character; ASCII characters keep their one-byte codes. The standard for the web.
  • UTF-16: 2 or 4 bytes (Windows, Java).
  • UTF-32: always 4 bytes.

Worked example

Code the word "Hi5" in ASCII hex: H = 72 = 48₁₆, i = 105 = 69₁₆, 5 = 53 = 35₁₆, so 48 69 35.

Exams usually ask

  • Code numbers in BCD, excess-3 and Gray; code text in ASCII.
  • Compare ASCII, EBCDIC and Unicode.

Common mistake

Confusing the digit "5" (ASCII 53) with the number 5 (binary 101).

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

Practise

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All IFT 211 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.