---
title: "Source coding; fixed vs variable length codes"
subject: "Data Communication"
chapter: 8
key: "semester_5/data_communication:source-coding-fixed-variable"
source: https://www.bctengineer.com/q/semester_5/data_communication/source-coding-fixed-variable
site: BCT Engineering
---

# Source coding; fixed vs variable length codes

Data Communication · Chapter 8: Information Theory and Coding

- Asked in: 2071 Chaitra, 1+1+1 marks
- Video: [Fixed length Coding and Variable length Coding|Coding Theory Introduction|Information Theory & Codin](https://www.youtube.com/watch?v=TQiGCxO3mtA) — Easy Electronics
- Answer checked by Claude Sonnet 5.5 on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_5/data_communication/source-coding-fixed-variable
- Whole chapter: https://www.bctengineer.com/md/semester_5/data_communication/chapter-8

## Question

Why is source coding necessary? Differentiate between fixed length codes and variable length codes. What is the purpose of Huffman's coding algorithm?

## Short answer

Source coding removes redundancy to save bandwidth and storage. Fixed-length codes assign equal bits to all symbols, while variable-length codes give shorter codes to frequent symbols, achieving lower average length. Huffman coding builds an optimal prefix code minimizing average length for given probabilities.

## Answer

#### Why source coding is necessary
Raw data (text, audio, images) contains redundancy, so sending it as it is wastes bandwidth and storage. Source coding removes this redundancy and represents the data with the fewest bits (compression), approaching the source entropy.

#### Fixed length vs variable length codes
| Point | Fixed-length code | Variable-length code |
|---|---|---|
| Code length | Same for all symbols | Differs; frequent symbols get shorter codes |
| Average length | Higher, ⌈log₂ M⌉ bits for M symbols | Lower, close to the entropy |
| Decoding | Simple | Needs the prefix property |
| Example | ASCII (7 or 8 bits) | Huffman, Morse |

#### Purpose of Huffman coding
Huffman's algorithm builds the optimal prefix (variable-length) code for given symbol probabilities. It gives the shortest average code length, and so the best lossless compression for symbol-by-symbol coding.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why is source coding necessary?
   A. To add redundancy for error correction
   B. To remove redundancy and represent data with fewer bits
   C. To encrypt the data
   D. To increase the bandwidth used
   Answer: B. Raw data has redundancy; source coding compresses it toward the source entropy, saving bandwidth and storage. (Key point: Need for source coding)

2. Which statement correctly contrasts the two code types?
   A. Variable-length codes give shorter codes to frequent symbols and a lower average length
   B. Fixed-length codes have lower average length than variable-length codes
   C. Variable-length codes need no prefix property
   D. Fixed-length codes assign shorter codes to frequent symbols
   Answer: A. Variable-length codes use short codewords for frequent symbols, so the average length approaches entropy; fixed-length codes use log2 M bits for all. (Key point: Fixed vs variable length)

3. What does Huffman's algorithm produce?
   A. An error-correcting code
   B. The optimal prefix variable-length code with shortest average length for given probabilities
   C. A fixed-length code
   D. A code with equal-probability symbols
   Answer: B. Huffman builds the optimal prefix code, giving the best lossless symbol-by-symbol compression. (Key point: Purpose of Huffman coding)

## Related in Data Communication

- [Source coding vs channel coding](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Asource-vs-channel-coding) — Data Communication, chapter 8
- [Huffman coding principle](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Ahuffman-principle) — Data Communication, chapter 8, asked 3×
- [Huffman coding of a sentence (Khoji Rahechha)](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Ahuffman-khoji-rahechha) — Data Communication, chapter 8
- [Huffman code for symbols A to E (0.4, 0.3, ...)](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Ahuffman-table-abcde) — Data Communication, chapter 8
- [Dataword vs codeword; error coding techniques](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Adataword-codeword-error-coding) — Data Communication, chapter 8
- [Huffman code for five symbols (0.2, 0.35, ...)](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Ahuffman-five-symbols-02-035) — Data Communication, chapter 8

## Related in other subjects

- [Importance of source coding; Shannon-Fano vs fixed length (seven symbols)](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Asource-coding-sf-fixed-seven) — Communication Systems, chapter 6
- [Variable length coding; entropy and fixed code efficiency](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Avariable-length-coding-entropy-six) — Communication Systems, chapter 6
- [Huffman code for five-symbol DMS vs fixed length](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Ahuffman-dms-five-symbols) — Communication Systems, chapter 6
- [Source vs channel coding; Huffman for five symbols](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Asource-channel-huffman-five) — Communication Systems, chapter 6
