You are on page 1of 63

CHAPTER 2

Number Systems, Operations and


Codes
Introduction

• The binary system and digital codes are


fundamental to computers and to digital
electronic in general.
• The binary number system and its relationship to
other number system such as decimal,
hexadecimal and octal is presented.
• Arithmetic operations with binary numbers are
covered to provide a basis for understanding how
computers and many other types of digital
systems work.
Number System

 Decimal 0 ~9

 Binary 0 ~1

 Octal 0 ~7

 Hexadecimal 0 ~F
Decimal Number

• The decimal number composed of 10


symbols.

• 0,1,2,3,4,5,6,7,8,9; these symbols as


digits of the numbers. The decimal
number also called the base 10 number
because it has ten digits.

4
Decimal Number
• In decimal number system each of the ten
digits, 0 through 9
Example:
Express the decimal
number 2745.214 as a
sum of the values of
each digit

Note:
MSB = Most Significant Bit
LSB = Least Significant Bit
Decimal Number
Solution:
weights 103 102 101 100 10-110-210-3

2 7 4 5 . 2 1 4

2745.21410 = (2x103) + (7x102) + (4x101) + (5x100) +


(2x10-1) + (1x10-2) + (4x10-3)

6
Decimal Number
Questions:

Express the decimal number below as a


sum of the value of each digit

a) 57
b) 387.31
c) 13.542

7
Binary Number
• The binary number system is another way to
represent quantities. There are 1 (HIGH) and 0
(LOW)
• The binary numbering system has base 2 with
each position weighted by a factor of 2:
Binary Number
• The position of each digit (bit) in a binary
number can be assigned a weight.
• For example: 1011.101
– 1011.101 is a binary number
– 1 is a digit, 0 is a digit, 1 is a digit…

weights 23 22 21 20 2-1 2-2 2-3

1 0 1 1 . 1 0 1

9
Example
Convert the binary whole number 1101101 to
decimal:
Weight: 26 25 24 23 22 21 20
Binary Number : 1 1 0 1 1 0 1

1101101 = 26 + 25 + 2 3 + 22 + 20
= 64 + 32 + 8 + 4 + 1
= 109
Example:
binary to decimal number
weights 23 22 21 20 2-1 2-2 2-3

1 0 1 1 . 1 0 1

1011.1012=(1x23) + (0x22) + (1x21) +


(1x20) + (1x2-1) + (0x2-2) + (1x2-3)
= 8 + 0 + 2 + 1 + 0.5 + 0 + 0.125
= 11.62510

11
Example:
binary to decimal number
Questions:

Convert the binary number below to


decimal

a) 0.1101
b) 1010111
c) 101011.011

12
Binary Weight

28 27 26 25 24 23 22 21 20 . 2-1 2-2
256 128 64 32 16 8 4 2 1 Decimal 0.5 0.25
point

The left most bit is the MSB and the right most bit is LSB.

The value of a bit is determined by its position in the


number.
In general, with n bits you can count up to a number equal
to 2n – 1.
For example, with three bits (n = 3), you can count from 0
to 7.
= 2n – 1
= 23 – 1
=7
Binary Weight

For example, with four


bits (n = 4):
= 2n – 1
= 24 – 1
= 15
Decimal-to-Binary Conversion

2 ways to convert a decimal number to


binary number:
• Sum of Weights Method
• Repeated Division by 2 Method
Decimal Number  Binary Number

Sum-of-Weights Method
To get the binary number for a given decimal, find the binary
weights that add up to the decimal number.

Example 1:
The decimal number is 11. So you can expressed as the sum
of binary weight as follows:

11 = 8 + 2 + 1 or 11 = 23 + 21 + 20
Placing 1s in appropriate weight:
23 22 21 20

8 4 2 1
1 0 1 1

So, the binary number for 11 is 1011 16


Decimal Number  Binary Number

Sum-of-Weights Method

Example 2:

Given 5710

26 25 24 23 22 21 20

64 32 16 8 4 2 1
0 1 1 1 0 0 1

So, the binary number for 57 is 0111001

17
Decimal Number  Binary Number

Repeated Division by 2 Method

• Another method for converting decimal integers


uses repeated division by 2.

• It requires repeatedly dividing the decimal number


by 2 and writing down the reminder after each
division until a quotient of 0 is obtained.
Decimal Number  Binary Number

Repeated Division by 2 Method


A systematic method of converting whole numbers from decimal to
binary is the repeated division-by-2 process.
Example 1: Remainder Answer:
Convert the
12 0 LSB
decimal number
12 to binary. 2
6
1100
6
3 0
Question:
2
3 Convert
1 1 decimal number
2 39 to binary?
Note: Stop when
1
the whole-number 0
quotient is 0 2 1 MSB
Decimal Number  Binary Number

Repeated Division by 2 Method


Example 2:
Convert decimal numbers 13 to binary

MSB 1101 LSB


Decimal-to-Binary Conversion
Converting decimal fractions to Binary:

2 ways to convert a decimal number to


binary number:
• Sum of Weights Method
• Repeated multiplication by 2
Converting Decimal Fractions to Binary
-Sum of Weights Method
• To get the binary number for a given fractional decimal, find the binary
weights that add up to the decimal number.
Note:

Example:
The decimal number is 0.625. So you can expressed as the sum of binary
weight as follows:

0.625 = 0.5 + 0.125 or 0.625 = 2-1 + 2-3


Placing 1s in appropriate weight:

2-1 2-2 2-3


1 0 1
So, the binary number for 0.625 is 0.101
Converting Decimal Fractions to Binary
-Repeated Multiplication by 2

• Multiplying by 2 until the fractional product is zero or


until the desired number of decimal places is reached.

• The carry digits, or carries, generated by the


multiplications produce the binary number.
The first carry produced is the MSB, and the last
carries is the LSB.
Converting Decimal Fractions to Binary
-Repeated Multiplication by 2
Example:
Convert decimal numbers 0.3125 to binary

Carry MSB 0.0101LSB


0.3125 x 2 = 0.625 0

0.625 x 2 = 1.25 1

0.25 x 2 = 0.50 0

0.5 x 2 = 1.00 1
Binary Arithmetic

• Binary arithmetic is essential in all digital


computers and in many other types of
digital systems.
• Addition, Subtraction, Multiplication, and
Division
Binary Addition
The four basic rules for adding binary digits:

Note: In fourth case, a binary addition is creating a sum of (1 + 1 = 10),


where 0 is written in the sum column and a carry of 1 over to the next
column.

Example:
Binary Addition
The four basic rules for adding binary digits:

Questions:
Add the following binary numbers:

a)11 + 11
b)100 + 10
c) 111 + 11
d)110 + 100
Binary Subtraction
The four basic rules for subtracting binary digits:

Example:
Binary Subtraction
The four basic rules for subtracting binary digits:

Questions:
Subtract the following binary numbers:

a) 11-01
b) 1001-0111
c) 10101-00111
Binary Multiplication
The four basic rules for multiplication in binary digits:
Binary Division
Division in binary follows the same procedure as division in
decimal.
Example:
1 1 0 ÷ 11 = ??
Exercise

Questions:

a) 1101 x 11
b) 101 x 101
c) 1110 ÷ 10
d) 10010 ÷ 110
1’s and 2’s Complements of
Binary Numbers
• The 1’s and 2’s Complements of Binary
Numbers are very important because they
permit the representation of negative
numbers.
• The method of 2’s complement arithmetic
is commonly used in computers to handle
negative numbers
Finding the 1’s Complement
The 1’s complement of a binary number is found by
changing all 1s to 0s and all 0s to 1s.
Example:
1 0 1 1 0 0 1 0 (Binary Number)
0 1 0 0 1 1 0 1 (1’s Complement)

NOT Gate

Example of inverters used to obtain the 1’s complement of a binary number.


Finding the 2’s Complement

The 2’s complement of a binary number is found by


adding 1 to the LSB of the 1’s complement
2’s complement = (1’s complement) + 1

Example: Find the 2’s complement of 10110010


10110010 (Binary number)
+ 01001101 (1’s complement)
1 (Add 1)
01001110 (2’s complement)
Signed Numbers
Digital systems, such as the computer, must be able to
handle both positive and negative numbers. A signed
binary number consists of both sign and magnitude
information. The sign indicates whether a number is
positive or negative and the magnitude is the value of
the number. There are 3 forms in which signed integer
(whole) numbers can be represented in binary:
1. Sign-Magnitude
2. 1’s Complement
3. 2’s Complement
The Sign Bit
The left-most bit in a signed binary number is the sign bit, which tells you
whether the number is positive or negative.

Sign-Magnitude Form
When a signed binary number is represented in sign-magnitude, the left-
most bit is the sign bit and the remaining bits are the magnitude bits. The
magnitude bits are in true (uncomplemented) binary for both positive and
negative numbers.
Example: Decimal number, +25 is expressed as an 8-bit
signed binary number using sign-magnitude form
as:
Magnitude Bit
Sign Bit 00011001
The Sign Bit
Example

+ 25 = 0 0011001 -25 = 1 0011001


Magnitude bit Magnitude bit
Signed bit Signed bit

In signed magnitude form, a negative number has same


magnitude bits as corresponding positive number but the sign
bit is a 1 rather than zero
The Sign Bit
1’s Complement Form
In the 1’s complement form, negative number is the 1’s complement of
corresponding positive number.

Example

Using 8-bits, decimal number for -25 is expressed as 1’s complement of +25

+25 = 00011001

So, in 1’s complement form:

-25 = 1’s complement of +25

= 11100110
The Sign Bit
2’s Complement Form
In the 2’s complement form, negative number is the 2’s complement of
corresponding positive number.

Example

Using 8-bits, decimal number for -25 is expressed as 2’s complement of


+25

+25 = 00011001

So, in 2’s complement form:

-25 = 2’s complement of +25

= 11100111
Exercise:

Express the decimal number -39 as an 8 bit number in the


sign magnitude, 1’s compliment form & 2’s compliment form
The Decimal Value of Signed
Numbers
•Decimal Value of positive and negative
numbers in the sign-magnitude form are
Sign-Magnitude: determined by summing the weights in all
the magnitude bit positions where there
are 1s and ignoring those positions where
there are zeros.

Determine the decimal value of this signed binary


number expressed in sign magnitude: 1 0 0 1 0 1 0 1

26 25 24 23 22 21 20
0 0 1 0 1 0 1 >> 16 + 4 + 1 = 21
The sign bit is 1: Therefore, the decimal number is -21
1’s Compliment: •Decimal Value of positive numbers in the sign-magnitude
form are determined by summing the weights in all the
magnitude bit positions where there are 1s and ignoring
those positions where there are zeros.
•Decimal values of negative numbers are determined by
assigning a negative value to the weight of the sign
bit,summing all the weights where there are 1s,and adding
1 to the result.
Determine the decimal value of this signed binary
number expressed in 1’s compliment:
a) 00010111
-27 26 25 24 23 22 21 20
0 0 0 1 0 1 1 1 >> 16 + 4 + 2 + 1 = +23

b) 11101000
-27 26 25 24 23 22 21 20
• 1 1 1 0 1 0 0 0 >> -128 + 64 + 32 + 8 = -24
•Adding 1 to the result; -24 + 1 = -23
2’s Compliment: •Decimal Value of positive and negative numbers in
the 2’s compliment form are determined by
summing the weights in all the bit positions where
there are 1s and ignoring those positions where
there are zeros. The weight of the sign bit in a
negative number is given in a negative value.

Determine the decimal value of this signed binary


number expressed in 2’s compliment:
a) 01010110
-27 26 25 24 23 22 21 20
0 1 0 1 0 1 1 0 >> 64 + 16 + 4 + 2 = + 86
b) 10101010
-27 26 25 24 23 22 21 20
1 0 1 0 1 0 1 0 >> -128 + 32 + 8 + 2 = - 86
Hexadecimal System

45
Binary to HEX Conversion
1. Group the binary number into group of 4-bits
2. Each group is converted to its equivalent HEX digit.
3. Zero are added as needed to complete a 4-bits group

Example

Convert 1100101001010111 to HEX

1100 1010 0101 0111


C A 5 7
HEX to Binary Conversion

Is a reverse process from Binary to HEX Conversion

Example

Determine binary numbers for 10A416

1 0 A 4
0001 0000 1010 0100
HEX to Decimal Conversion

Multiply the decimal value of each hex digit by its weight


and then take the sum of these product.

Example
161 160
Convert 1C16 to decimal 1 C

1C16 = (1 x 16) + (C x 1)
= (1 x 16) + (12 x 1)
= 2810
C16 = 1210
Decimal to HEX Conversion
Example:
• Convert 65010 to HEX

HEX Reminder

Stop when whole


number quotient is zero
MSB 28A LSB
EXERCISE

a) Convert 11110101001001012 to HEX


b) Convert F85C16 to binary
c) Convert 6A16 to decimal
d) Convert 67410 to HEX
Octal System

• Base-8 system
• 8 digits: 0,1,2,3,4,5,6,7
• Convenient way to express binary
numbers and codes

52
BCD

 Binary Coded Decimal (BCD) is


another way to present decimal
numbers in binary form.
 BCD is widely used and combines
features of both decimal and binary
systems.
 Each digit is converted to a binary
equivalent.
BCD
 To convert the number 87410 to BCD:

8 7 4
1000 0111 0100 = 100001110100BCD

 Each decimal digit is represented using 4 bits.


 Each 4-bit group can never be greater than 9.
 Reverse the process to convert BCD to decimal.
Decimal 0 1 2 3 4 5 6 7 8 9
Digit
BCD 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
BCD

 BCD is not a number system.


 BCD is a decimal number with each
digit encoded to its binary equivalent.
 A BCD number is not the same as a
straight binary number.
 The primary advantage of BCD is the
relative ease of converting to and from
decimal.
Binary Coded Decimal (BCD)

Each decimal digit represented by 4-bits binary coded.


Designation 8421 indicate the binary weight of 4-bits. (23, 22, 21, 20)

Decimal 0 1 2 3 4 5 6 7 8 9
Digit
BCD 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001

Invalid Code:
The six code combination are not in used
1010, 1011, 1100, 1101, 1110, 1111
EXERCISE

Convert each of the following decimal number to BCD


a) 35
b) 170

Convert each of the following BCD codes to decimal


a) 10000110
BCD Addition
• BCD is numerical code and can be used in arithmetic
operation.
• Step:

1.Add the two BCD numbers, using the rules for binary addition

2. If a 4 bit sum is equal to or less than 9, it is a valid BCD


number

3. If a 4 bit sum is greater than 9, or if a carry out of the 4 bit is


generated, it is an invalid result.

Add 6(0110) to the for bit sum in order to skip the six invalid
states and return the code to 8421. If a carry results when 6
is added, simply add the carry to the next 4 bit group.
EXAMPLE

a) 0011 + 0100 b) 1001 + 0100

0011 1001
+ 0100 + 0100
0111 1101 Invalid BCD number (>9)
+ 0110 Add 6(0110)
0001 0011 Valid BCD number

1 3
EXERCISE
Add the following BCD numbers
a) 0011 + 0100
b) 1001 + 0100
c) 10000110 + 00010011
d) 01100111 + 01010011
e) 00010110 + 00010101
Alphanumeric codes

• Codes that represent numbers and


alphabetic characters (letters).
• At minimum, the code must represent 10
decimal digits (0-9) and 26 letters (A-Z).
• 6 bits are needed in the code that represent
the numbers and letters.
• ASCII is the most common alphanumeric
code.
ASCII Code
• American Standard Code for International
Interchange
• Used in computers and electronic
equipment

1011001
processor
Thank You

You might also like