ECE2021-2-Number Systems and Codes
ECE2021-2-Number Systems and Codes
Weights
4
2.1 Binary-to-Decimal Conversions (cont.)
LSB
MSB
The procedure is to find the weights (i.e., powers of 2) for each bit
position that contains a “1”, and then to add them up.
5
Review questions???
6
2.2 Decimal-to-Binary Conversions
• Two ways to convert a decimal whole number to its equivalent
binary-system representation.
1) The decimal number is simply expressed as a sum of powers of 2,
and then 1s and 0s are written in the appropriate bit positions.
20 = 1 23 = 8 26 = 64
21 = 2 24 = 16 27 = 128 Firstly select the weight less than and the most
22 = 4 25 = 32 28 = 256 closer to 76.
Weights
8
2.2 Decimal-to-Binary Conversions (cont.)
• Two ways to convert a decimal whole number to its equivalent
binary-system representation.
2) converting decimal integers uses repeated division by 2;
9
2.2 Decimal-to-Binary Conversions (cont.)
• Repeated division by 2
10
2.2 Decimal-to-Binary Conversions (cont.)
• Flowchart for the repeated-division method of decimal-to-
binary conversion of integers.
START
No
Is Q = 0?
Divided by 2
Yes
Record quotient (Q) and Collect R’s into a desired
remainder (R) binary number with first
as LSB and last R as MSB
END 11
2.2 Decimal-to-Binary Conversions (cont.)
• The same process can be used to convert a decimal integer to any other number
system. Conversion from decimal integers to any base-r system is similar to this
example, except that division is done by r instead of 2.
START
No
Is Q = 0?
Divided by r
Yes
Record quotient (Q) and Collect R’s into a desired
remainder (R) binary number with first
as LSB and last R as MSB
END 12
2.2 Decimal-to-Binary Conversions (cont.)
GENERAL:
To convert a decimal fraction to a number expressed in base r, a similar procedure is
used. However, multiplication is by r instead of 2, and the coefficients found from the
integers may range in value from 0 to r - 1 instead of 0 and 1.
13
2.2 Decimal-to-Binary Conversions (cont.)
14
2.2 Decimal-to-Binary Conversions (cont.)
• Counting Range
Using N bits (or called binary digit), we can represent decimal numbers
ranging from 0 to 2N -1, a total of 2N different numbers
Example:
(a) What is the total range of decimal values that can be represented in
eight bits?
(b) How many bits are needed to represent decimal values ranging from 0
to 12,500?
15
Review questions???
1. Convert 8310 to binary using both methods.
2. Convert 72910 to binary using both methods.
3. How many bits are required to count up to decimal 1 million?
16
2.3 Hexadecimal Number System
17
2.3 Hexadecimal Number System (cont.)
Weights
18
2.3 Hexadecimal Number System (cont.)
• Hex-to-Decimal Conversion
to evaluate the positive/negative powers of 16.
19
2.3 Hexadecimal Number System (cont.)
• Decimal-to-Hex Conversion
(a) Convert 42310 to hex. (b) Convert 21410 to hex.
LSD LSD
MSD
MSD
20
2.3 Hexadecimal Number System (cont.)
• Hex-to-Binary Conversion
Apply a “shorthand” method to represent binary numbers.
Each hex digit is converted to its four-bit binary equivalent.
21
2.3 Hexadecimal Number System (cont.)
• Binary-to-Hex Conversion
The binary number is grouped into groups of four bits, and each
group is converted to its equivalent hex digit. Zeros (shown
shaded) are added, as needed, to complete a four-bit group.
22
2.3 Hexadecimal Number System (cont.)
• Counting in Hexadecimal
When counting in hex, each digit position can be incremented (increased by 1)
from 0 to F. Once a digit position reaches the value F, it is reset to 0, and the
next digit position is incremented.
With N hex digit positions, we can count from decimal 0 to 16N-1 for a
total of 16N different values.
For example, with three hex digits, we can count from 00016 to FFF16,
which is 010 to 409510, for a total of 4096 = 163 different values.
23
2.3 Hexadecimal Number System (cont.)
24
Conversion Summary
• Summary
• Binary/Hex Decimal
• Decimal Binary/Hex
• Hex Binary
• Binary Hex
25
Conversion Summary (cont.)
1. When converting from binary [or hex] to decimal, use the method of taking
the weighted sum of each digit position.
2. When converting from decimal to binary [or hex], use the method of
repeatedly dividing by 2 [or 16] and collecting remainders.
3. When converting from binary to hex, group the bits in groups of four, and
convert each group into the correct hex digit.
4. When converting from hex to binary, convert each digit into its four-bit
equivalent.
26
Review questions???
1. Convert 24CE16 to decimal.
2. Convert 311710 to hex, then from hex to binary.
3. Convert 10010111101101012 to hex.
4. Write the next four numbers in this hex counting sequence:
E9A, E9B, E9C, E9D, _____, _____, _____, _____.
5. Convert 3527 to binary16.
6. What range of decimal values can be represented by a four-
digit hex number?
27
2.4 Binary-Coded-Decimal (BCD) Codes
Definition: When numbers, letters, or words are represented by a
special group of symbols, we say that they are being encoded, and the
group of symbols is called a code (e.g. Morse code).
28
2.4 BCD Codes (cont.)
• Decimal ↔ Straight binary coding
• Binary-coded-decimal (BCD): If each digit of a decimal number is
represented by its binary equivalent, the result is a code called
binary-coded-decimal (hereafter abbreviated BCD).
Four bits are required to code each digit (the binary code for 9 is 1001).
The BCD code does not use the numbers 1010, 1011, 1100, 1101, 1110, and 1111.
29
2.4 BCD Codes (cont.)
Examples:
30
2.4 BCD Codes (cont.)
• Comparison of BCD and Straight Binary Coding
Example:
31
2.4 BCD Codes (cont.)
32
Review questions???
1. Represent the decimal value 178 by its straight binary equivalent.
Then encode the same decimal number using BCD.
2. How many bits are required to represent an eight-digit decimal
number in BCD?
3. What is an advantage of encoding a decimal number in BCD
rather than in straight binary? What is a disadvantage?
33
2.5 The Gray Code
When multiple input conditions are changing at the same time
34
2.5 The Gray Code (cont.)
• Step 1: Start on the most significant bit (MSB) and use it as the Gray MSB;
• Step 2: Compare the MSB binary with the next binary bit (B1). If they are
the same, then G1 = 0. If they are different, then G1 = 1. G0 can be found by
comparing B1 with B0.
The same
35
2.5 The Gray Code (cont.)
B2 vs B1 B1 vs B0 B2 vs G1 B1 vs G0
37
2.6 Putting All Together
• A summary table gives the representation of the decimal numbers 1
through 15 in the binary and hex number systems and also in the BCD
and Gray codes.
• Examine it carefully and make sure you understand how it was
obtained.
38
2.6 Putting All Together (cont.)
39
2.7 The Byte, Nibble, and Word
• Bytes:
Most microcomputers handle and store binary data and information in groups of
eight bits, so a special name is given to a string of eight bits: it is called a byte.
A byte always consists of eight bits, and it can represent any of numerous types
of data or information.
40
2.7 The Byte, Nibble, and Word (cont.)
• Nibble: it is half as big as a byte, it was named a nibble.
41
2.7 The Byte, Nibble, and Word (cont.)
• Words:
• Bits, nibbles, and bytes are terms that represent a fixed number of
binary digits.
• A word is a group of bits that represents a certain unit of information.
• The word size can be defined as the number of bits in the binary word
that a digital system operates on.
42
2.8 Alphanumeric Codes
• A computer should recognize codes that represent letters of the alphabet,
punctuation marks, and other special characters/numbers called
alphanumeric codes. In other words, all of the various characters and functions
that are found on a computer keyboard.
• A, B, C, …, a, b, c, …, 1, 2, 3, …, ., ,, ?, …, +, /, #, …
43
2.8 Alphanumeric Codes (cont.)
44
2.8 Alphanumeric Codes (cont.)
Example: The computer converts each keystroke into its ASCII code and
stores the code as a byte in memory. Determine the binary strings that
will be entered into memory when the operator types in the following C
statement: if (x >3)
45
2.8 Alphanumeric Codes (cont.)
• Solution: Note that a 0 was added to the leftmost bit of each ASCII
code because the codes must be stored as bytes (eight bits).
This adding of an extra bit is called padding with 0s.
padding with 0s
46
2.8 Alphanumeric Codes (cont.)
1. Encode the following message in ASCII code using the hex
representation: “COST=$72.”
2. The following padded ASCII-coded message is stored in
successive memory locations in a computer:
01010011 01010100 01001111 01010000
What is the message?
47
2.8 Alphanumeric Codes (cont.)
48
2.9 Parity Method for Error Detection
• Movement of binary data and codes from one location to another:
• Error can occur
• Electrical noise
• Detection code
• Correction code
49
2.9 Parity Method for Error Detection (cont.)
• Parity bit: A parity bit is an extra bit that is attached to a code
group which is being transferred from one location to another.
• The parity bit is made either 0 or 1, depending on the number of 1s
that are contained in the code group. Two different methods are
used: i) even-parity method; and ii) odd-parity method.
50
2.9 Parity Method for Error Detection (cont.)
• Parity bit:
i) Even-parity method
- Total number of 1s in the code group (including the parity bit) is an
even number.
51
2.9 Parity Method for Error Detection (cont.)
• The parity bit is issued to detect any single-bit errors that occur during
the transmission of a code from one location to another.
• Example: When one computer is transmitting a message to another, the
information is usually encoded in ASCII. What actual bit strings would a
computer transmit to send the message HELLO, using ASCII with even parity?
52
2.9 Parity Method for Error Detection (cont.)
• Parity bit (in summary):
1) Even-parity method
2) Odd-parity method
3) Detection code, not correction code
4) Single-bit error detection: This parity method would not work if two bits
were in error, because two errors would not change the “oddness” or
“evenness” of the number of 1s in the code. In practice, the parity method is
used only in situations where the probability of a single error is very low and
the probability of double errors is essentially zero.
5) Checking the parity, request for retransmission, retransmission
53
2.10 Applications
• Transmitting an ASCII character
54
2.10 Applications (cont.)
55
SUMMARY
1. The hexadecimal number system is used in digital systems and computers
as an efficient way of representing binary quantities.
2. In conversions between hex and binary, each hex digit corresponds to
four bits.
3. The repeated-division method is used to convert decimal numbers to
binary or hexadecimal.
4. Using an N-bit binary number, we can represent decimal values from 0 to
2N - 1.
5. The BCD code for a decimal number is formed by converting each digit of
the decimal number to its four-bit binary equivalent.
56
SUMMARY (Cont.)
6. The Gray code defines a sequence of bit patterns in which only one
bit changes between successive patterns in the sequence.
7. A byte is a string of eight bits. A nibble is four bits. The word size
depends on the system.
8. An alphanumeric code is one that uses groups of bits to represent all
of the various characters and functions that are part of a typical
computer’s keyboard. The ASCII code is the most widely used
alphanumeric code.
9. The parity method for error detection attaches a special parity bit to
each transmitted group of bits.
57
HOMEWORK
58
HOMEWORK (cont.)
59
HOMEWORK (cont.)
60
HOMEWORK (cont.)
61
FUNNY!!!
2.
3.
63
64
VGU
VIETNAMESE-GERMAN UNIVERSITY