Chapter 2 - Number & Codes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

Chapter 2: Number Systems &

Digital Codes
Decimal, Binary, Hexadecimal & Octal Numbers
Number Conversions
Binary Arithmetic
Binary Codes
Number Systems

Mohd Afzan Othman


Decimal Numbers
• Use Base-10 system.
• 10 digits/symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.
• The value of a digit is determined by its position in the number. For
example to express the quantity twenty three:

• The position of each digit indicates the magnitude of the quantity


and can be assigned by a weight.
Mohd Afzan Othman
Decimal Numbers
• The weight of whole numbers are positive powers of ten, that
increases from right to left, beginning from 100 = 1.

• Fraction numbers, the weight are negative powers of ten, that


decreases from left to right beginning with 10-1 = 0.1.

• The value of decimal number is a sum of the digits after each digits
multiplied by its weight.

Mohd Afzan Othman


Decimal Numbers
• Example: 2745.214

• Solution:
2745.214 = (2 x 103) + (7 x 102) + (4 x 101) + (5 x 100) + (2 x 10-1) +
(1 x 10-2) + (4 x 10-3)
= (2 x 1000) + (7 x 100) + (4 x 10) + 5 + (2 x 0.1) +
(1 x 0.01) + (4 x 0.001)
= 2000 + 700 + 40 + 5 + 0.2 + 0.01 + 0.004
Mohd Afzan Othman
Binary Numbers
• Use Base-2 system.
• 2 binary digits (bits)/symbols: 0 and 1.
• Example: 00, 01, 10, 11, 100, 101, 110, 111, …
• The value of a bit is determined by its position in the number.
• The position of 0 and 1 indicates its weight, or value within
number.

• The right-most bit is the LSB (least significant bit), in binary whole
number it has weight 20 = 1. The weight increase from right to left
by power of two.
Mohd Afzan Othman
Binary Numbers
• The left-most bit is the MSB (most significant bit).

• Fraction numbers, the weight are negative powers of two, that


decreases from left to right beginning with 2-1 = 0.5.

Mohd Afzan Othman


Binary Numbers:
Binary to Decimal Conversion
• The decimal value of any binary number can be found by adding
the weights of all bits that are 1 and discarding the weights of all
bits that are 0.
• Example: Convert the binary whole number 1101101 to decimal.
• Solution: Determine the weight of each bit that is a 1, then
calculates the sum of the weights.

Mohd Afzan Othman


Binary Numbers:
Binary to Decimal Conversion
• Example: Convert the fractional binary number 0.1011 to decimal.
• Solution: Determine the weight of each bit that is a 1, then
calculates the sum of the weights.

Mohd Afzan Othman


Binary Numbers:
Binary to Decimal Conversion
• Example: Convert the binary number 1011.101 to decimal.
• Solution: Determine the weight of each bit that is a 1, then
calculates the sum of the weights.

1011.101 = 23 + 21 + 20 + 2-1 + 2-3


= 8 + 2 + 1 + 0.5 + 0.125
= 11.62510

Mohd Afzan Othman


Binary Numbers:
Decimal to Binary Conversion (Whole Number)
• Repeated Division-by-2 Method:
– Dividing the decimal number by 2.
– Repeating dividing each resulting quotient by 2 until the is 0 whole-
number quotient.
– Take the remainders generated from the division and form the binary
number.
– The first remainder is set as LSB, and the last remainder as MSB.

Mohd Afzan Othman


Binary Numbers:
Decimal to Binary Conversion (Whole Number)
• Example: Convert the decimal number 12 to binary.

Mohd Afzan Othman


Binary Numbers:
Decimal to Binary Conversion (Fractional Number)
• Repeated Multiplication-by-2 Method:
– Multiplying the decimal number by 2.
– Repeating multiplying each resulting fractional part by 2 until the
fractional product is 0.
– Take the carries generated by the multiplication to form the binary
number.
– The first carry product is set as MSB, and the last carry as LSB.

Mohd Afzan Othman


Binary Numbers:
Decimal to Binary Conversion (Fractional Number)
• Example: Convert the fractional decimal number 0.3125 to binary.

Mohd Afzan Othman


Octal Numbers
• Use Base-8 system.
• 8 digits/symbols: 0 , 1, 2, 3, 4, 5, 6, and 7.
• Example: 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20,…
• The value of a digit is determined by its position in the number.
• The position of each digit indicates the magnitude of the quantity
and can be assigned by a weight.

• In octal whole number it has weight 80 = 1. The weight increase


from right to left by power of eight.
Mohd Afzan Othman
Octal Numbers
• Fraction numbers, the weight are negative powers of eight, that
decreases from left to right beginning with 8-1 = 0.125.
• Convenient way to express binary numbers and codes. Uses 3-bits
binary boundary.

• Example of binary to octal expression using 3-bit boundary.

Mohd Afzan Othman


Octal Numbers:
Octal to Decimal Conversion
• The decimal equivalent can be accomplished by multiplying each
digit by its weight and summing the products.
• Example: Convert the octal whole number 23748 to decimal.
• Solution: Multiply each digit by its weight, then calculates the sum
of the products.

Mohd Afzan Othman


Octal Numbers:
Decimal to Octal Conversion (Whole Number)
• Repeated Division-by-8 Method:
– Dividing the decimal number by 8.
– Repeating dividing each resulting quotient by 8 until the is 0 whole-
number quotient.
– Take the remainders generated from the division and form the octal
number.
– The first remainder is set as LSD, and the last remainder as MSD.

Mohd Afzan Othman


Octal Numbers:
Decimal to Octal Conversion (Whole Number)
• Example: Convert the decimal number 359 to octal.

• Answer: 5478

Mohd Afzan Othman


Octal Numbers:
Decimal to Octal Conversion (Fractional Number)
• Repeated Multiplication-by-8 Method:
– Multiplying the decimal number by 8.
– Repeating multiplying each resulting fractional part by 8 until the
fractional product is 0.
– Take the carries generated by the multiplication to form the octal
number.
– The first carry product is set as MSD, and the last carry as LSD.

Mohd Afzan Othman


Octal Numbers:
Decimal to Octal Conversion (Fractional Number)
• Example: Convert the fractional decimal number 0.3125 to octal.

• Answer: 0.248

Mohd Afzan Othman


Octal Numbers:
Binary to Octal Conversion
• To convert binary to octal number, simply :
– Step 1: Break the binary number into 3-bits group (3-bits boundary),
starting from LSD.
– Step 2: Replace each 3-bits group with equivalent the octal number.

• Example: Convert the binary number 110101 to octal.


• Solution:
binary: 110 101
octal: 6 5
• Answer: 658
Mohd Afzan Othman
Octal Numbers:
Binary to Octal Conversion
• Example: Convert the binary number 1010011 to octal.
• Solution:
binary: 001 010 011
octal: 1 2 3
• Answer: 1238

Mohd Afzan Othman


Octal Numbers:
Octal to Binary Conversion
• To convert octal to binary number, simply replace octal digit with
the appropriate 3-bits group (3-bits boundary).
• Example: Convert the octal number 138 to binary.
• Solution:
octal: 1 3
binary: 001 011
• Answer: 0010112

• Example: Convert the octal number 75268 to binary.


• Solution:
octal: 7 5 2 6
binary: 111 101 010 110
• Answer: 1111010101102
Mohd Afzan Othman
Hexadecimal Numbers
• Use Base-16 system.
• 16 symbols, consists of 10 numeric digits and 6 alphabetic
characters: 0 , 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F.
• Example: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11,12,...
• The value of a digit is determined by its position in the number.
• The position of each digit indicates the magnitude of the quantity
and can be assigned by a weight.
• The weight of hexadecimal whole numbers are positive powers of
sixteen, that increases from right to left, beginning from 160 = 1.

Mohd Afzan Othman


Hexadecimal Numbers
• Fraction numbers, the weight
are negative powers of sixteen,
that decreases from left to right
beginning with 16-1 = 0.0625.
• Compact way to express binary
numbers and codes. Uses 4-bits
binary boundary.

Mohd Afzan Othman


Hexadecimal Numbers:
Hexadecimal to Decimal Conversion
• The decimal equivalent can be accomplished by multiplying each
hexadecimal digit by its weight and summing the products.
• Example: Convert the hexadecimal number B2F816 to decimal.
• Solution: Multiply each digit by its weight, then calculates the sum
of the products.

Mohd Afzan Othman


Hexadecimal Numbers:
Decimal to Hexadecimal Conversion (Whole Number)
• Repeated Division-by-16 Method:
– Dividing the decimal number by 16.
– Repeating dividing each resulting quotient by 16 until the is 0 whole-
number quotient.
– Take the remainders generated from the division and form the
hexadecimal number.
– The first remainder is set as LSD, and the last remainder as MSD.

Mohd Afzan Othman


Hexadecimal Numbers:
Decimal to Hexadecimal Conversion (Whole Number)
• Example: Convert the decimal number 650 to hexadecimal.

• Answer: 28A16

Mohd Afzan Othman


Hexadecimal Numbers:
Decimal to Hexadecimal Conversion (Fractional Number)

• Repeated Multiplication-by-16 Method:


– Multiplying the decimal number by 16.
– Repeating multiplying each resulting fractional part by 16 until the
fractional product is 0.
– Take the carries generated by the multiplication to form the
hexadecimal number.
– The first carry product is set as MSD, and the last carry as LSD.

Mohd Afzan Othman


Hexadecimal Numbers:
Decimal to Hexadecimal Conversion (Fractional Number)

• Example: Convert the fractional decimal number 0.3125 to


hexadecimal.

• Answer: 0.516

Mohd Afzan Othman


Hexadecimal Numbers:
Binary to Hexadecimal Conversion
• To convert binary to octal number, simply :
– Step 1: Break the binary number into 4-bits group (4-bits boundary),
starting from LSD.
– Step 2: Replace each 4-bits group with equivalent the hexadecimal
number.
• Example: Convert the binary number 1100101001010111 to
hexadecimal.
• Solution:
binary: 1100 1010 0101 0111
hexadecimal: C A 5 7
• Answer: CA5716

Mohd Afzan Othman


Hexadecimal Numbers:
Binary to Hexadecimal Conversion
• Example: Convert the binary number 111111000101101001 to
hexadecimal.
• Solution:
binary: 0011 1111 0001 0110 1001
hexadecimal: 3 F 1 6 9
• Answer: 3F16916

Mohd Afzan Othman


Hexadecimal Numbers:
Hexadecimal to Binary Conversion
• To convert hexadecimal to binary number, simply replace
hexadecimal digit with the appropriate 4-bits group (4-bits
boundary).
• Example: Convert the hexadecimal number 10A416 to binary.
• Solution:
hexadecimal: 1 0 A 4
binary: 0001 0000 1010 0100
• Answer: 00010000101001002
• Example: Convert the hexadecimal number CF8E16 to binary.
• Solution:
hexadecimal: C F 8 E
binary: 1100 1111 1000 1110
• Answer: 11001111100011102
Mohd Afzan Othman
Exercises
• Fill in the blanks.

Decimal Binary Octal Hexadecimal

1101.0112
10101.112
245.62510
70310
A8516

Mohd Afzan Othman


Binary Arithmetic

Mohd Afzan Othman


Binary Addition
• The four rules for adding binary digits (bits) are:
0+0=0 Sum of 0 with carry of 0
0+1=1 Sum of 1 with carry of 0
1+0=1 Sum of 1 with carry of 0
1 + 1 = 10 Sum of 0 with carry of 1
• When binary numbers are added, the last condition creates a sum
of 0 in a given column and carry of 1 in the next column to the left
as illustrated in the following addition of 11+1.

• Example: Find 111 + 11?


• Answer: 1010
Mohd Afzan Othman
Binary Subtraction
• The four rules for subtracting bits are:
0-0=0
1-1=0
1-0=1
10-1=1 0 – 1 with a borrow of 1
• When subtracting numbers, needs to borrow from the next column to
the left if try to subtract 1 from 0.
• When one is borrowed from next column to the left, a 10 is created in
the column being subtracted. Example: Find 101 – 011? Answer: 010

Mohd Afzan Othman


Binary Multiplication
• The four rules for multiplying bits are:
0x0=0
0x1=0
1x0=0
1x1=1
• Same manner as with decimal number.
• Involves performing partial product, shifting each successive partial
product one place, then adding all the partial products.
• Example: Find 101 x 111? Answer: 100011

Mohd Afzan Othman


Binary Division
• The procedure is same as with decimal number.
• Example: Find 110 ÷11? Answer: 10

Mohd Afzan Othman


Digital Codes

Mohd Afzan Othman


Digital Codes:
Overview
• Many digital devices interact with humans.
• Information is entered from the input device to digital system and the
results display through the output device.
• As human prefer the decimal system, information often has to be
converted from decimal to binary (encoding) for processing, and binary to
decimal (decoding) for presentation.
• Special circuit called encoder and decoder are required to perform data
conversion.

Mohd Afzan Othman


Digital Codes:
Overview
• There are few type of codes available:
– BCD Code. (Binary coded decimal)
– Gray Code.
– ASCII Code.
– Unicode.
– EBCDIC Code.

Mohd Afzan Othman


Digital Codes:
BCD Code
• The simplest interface between binary and digital system.
• Each decimal digit uses 4-bits.
• Each 4-bit groups is treated as separate binary number.

• Example: The value 641 is stored as 0110 0100 0001.


decimal number: 6 4 1
BCD code: 0110 0100 0001

• Also known as BCD 8421 code because the numbers indicate as the weight
of each bits.

Mohd Afzan Othman


Digital Codes:
BCD Code
• Convert BCD into its decimal equivalent:
– Step 1: Break the BCD into 4-bit groups, starting from LSB.
– Step 2: Replace each 4-bit group with its equivalent decimal.

• Example: Convert BCD code 0011 0101 0001 to decimal.

4-bit grouping: 0011 0101 0001


decimal number: 3 5 1
• Answer: 35110

Mohd Afzan Othman


Digital Codes:
Gray Code
• Is a non-weighted code.
• Only a single bit change from one code word to the next sequence.
• Good – to minimize the chance of error.

Mohd Afzan Othman


Digital Codes:
Gray Code
a) Binary to Gray Code conversion:
• The most significant bit (left-most) in the Gray Code is the same as
the corresponding MSB in the binary number.
• Going from left to right, add each adjacent pair of binary code to get
next Grey Code. Discard carries.
• Example: The conversion of binary number 10110 are:

• Answer: The Gray Code is 11101.

Mohd Afzan Othman


Digital Codes:
Gray Code
b) Gray Code to Binary conversion:
• The most significant bit (left-most) in binary number is the same as
the corresponding bit in the Gray Code.
• Add each binary number bit generated to the the Gray Code bit in
the next adjacent position. Discard carries.
• Example: The conversion of Grey Code word 11011 to binary:

• Answer: The binary is 10010.

Mohd Afzan Othman


Digital Codes:
Alphanumeric Code
• In complex digital system such computers must process not only numeric
data, but also alphabets, punctuation marks, and other symbols.
• Thus to represent numbers and alphabet characters (letters), a code called
alphanumeric code is needed.
• At minimum, the code must represents 10 digit decimal numbers (0-9) and
26 letters (A-Z), total of 36 items.
• 6-bits are needed in the code that represents the numbers and letters
because 5-bits is not enough (25=32).
• ASCII is the most common alphanumeric code.

Mohd Afzan Othman


Digital Codes:
ASCII Code
• ASCII is the abbreviation of American Standard Code for International
Interchange.
• Used in computers and electronic equipments.
• Most computer keyboards are standardized with ASCII code.
• When entering a letter, a number, or control command, the corresponding
ASCII code goes to the computer.
• ASCII has 128 characters, represents by 7-bit binary code.
• Can be considered as 8-bit with MSB = 0.
• ASCII can be divided into:
– non-graphic commands: The first 32 ASCII characters are only for
control purpose. E.g. Null, line feed, start of text, escape and etc.
– graphic symbols: letter of alphabet (lowercase and uppercase), 10
decimal digits, punctuation signs and other commonly used symbols.

Mohd Afzan Othman


Digital Codes: ASCII Code

Mohd Afzan Othman


Digital Codes:
EBCDIC Alphanumeric Code
• Extended Binary Coded Decimal Interchange Code
• 8-bit character encoding.

Mohd Afzan Othman


Digital Codes:
EBCDIC Alphanumeric
Code

Mohd Afzan Othman

You might also like