Programming Fundamentals Loops
Programming Fundamentals Loops
Codes
▪ Binary Coded Decimal (BCD)
▪ Gray Code
❖ Binary-to-Gray Conversion
❖ Gray-to-Binary Conversion
▪ For example:
❖ to represent yes or no: use 0 for no and 1 for yes
❖ to represent 4 seasons: 0 (autumn), 1 (winter), 2(spring) and
3 (summer)
❖ NRIC number: a letter, 7 digits, and a check code
❖ matriculation number (8 alphanumeric) to represent
individual students
Information Representation
▪ Elementary storage units inside computer are
electronic switches. Each switch holds one of two
states: on (1) or off (0).
ON OFF
0 (00)
1 (01)
2 (10)
3 (11)
Information Representation
▪ In general, N bits can represent 2N different values.
▪ For M values, log 2 M bits are needed.
1 bit → represents up to 2 values (0 or 1)
2 bits → rep. up to 4 values (00, 01, 10 or 11)
3 bits → rep. up to 8 values (000, 001, 010. …, 110, 111)
4 bits → rep. up to 16 values (0000, 0001, 0010, …, 1111)
32 values → requires 5 bits
64 values → requires 6 bits
1024 values → requires 10 bits
40 values → requires 6 bits
100 values → requires 7 bits
Positional Notations
▪ Position-independent notation
❖ each symbol denotes a value independent of its position:
Egyptian number system
▪ Relative-position notation
❖ Roman numerals symbols with different values: I (1), V (5),
X (10), C (50), M (100)
❖ Examples: I, II, III, IV, VI, VI, VII, VIII, IX
❖ Relative position important: IV = 4 but VI = 6