You are on page 1of 48

Number Systems

Table of Content

Basic Definitions
What is a Number?
What is Number Systems?
Why is the Number System Important?
Types of Number System
The Decimal Number System
The Binary Number System
Converting from Binary to Decimal
Converting from Decimal to Binary
The Hexadecimal Number System

2
Basic Definitions
A bit is a single binary digit (a 1 or 0).
A byte is 8 bits
A word is 32 bits or 4 bytes
Long word = 8 bytes = 64 bits
Quad word = 16 bytes = 128 bits
Programming languages use these standard
number of bits when organizing data storage
and access.
What do you call 4 bits?
(hint: it is a small byte)

3
What is a Number?
A number is a mathematical value used for counting
or measuring or labelling objects. Numbers are used
to perform arithmetic calculations. Examples of
numbers are natural numbers, whole numbers,
rational and irrational numbers, etc. 0 is also a
number that represents a null value.
A number has many other variations such as even
and odd numbers, prime and composite numbers.
Even and odd terms are used when a number is
divisible by 2 or not, whereas prime and composite
differentiate between the numbers that have only
two factors and more than two factors, respectively.
Number Systems
The on and off states of the capacitors in
RAM can be thought of as the values 1
and 0, respectively.
Therefore, thinking about how information
is stored in RAM requires knowledge of
the binary (base 2) number system.
Let’s review the decimal (base 10)
number system first.

5
Why is the Number System
Important?

The number system helps to represent


numbers in a small symbol set.
Computers, in general, use binary numbers 0
and 1 to keep the calculations simple and to
keep the amount of necessary circuitry less,
which results in the least amount of space,
energy consumption and cost.
Types of Number System

There are various types of number


systems in mathematics. The four most
common number system types are:
Decimal number system (Base- 10)
Binary number system (Base- 2)
Octal number system (Base-8)
Hexadecimal number system (Base- 16)
The Decimal Number System
• The decimal number system is a
positional number system.
• Example:
 5 6 2 1 1 X 100 =
1
 10 10 10 10
3 2 1 0
2 X 101 =
20
 6 X 102 =
600
5 X 10 =
8
 3
The Decimal Number System
• The decimal number system is also
known as base 10. The values of the
positions are calculated by taking 10 to
some power.
• Why is the base 10 for decimal
numbers?
o Because we use 10 digits, the digits 0
through 9.

9
The Binary Number System
• The binary number system is also
known as base 2. The values of the
positions are calculated by taking 2
to some power.
• Why is the base 2 for binary
numbers?
o Because we use 2 digits, the digits 0
and 1.

10
The Binary Number System
• The binary number system is also a
positional numbering system.
• Instead of using ten digits, 0 - 9, the
binary system uses only two digits, 0
and 1.
• Example of a binary number and the
values of the positions:
 1 0 0 1 1 0 1
 26 25 24 23 22 21 20 11
Converting from Binary to Decimal
 1 0 0 1 1 0 1 1 X 20 = 1
 26 25 24 23 22 21 20 0 X 21 = 0
 1 X 22 = 4
 20 =1 24 = 16 1 X 23 = 8
 21 =2 25 = 32 0 X 24 = 0
 22 =4 26 = 64 0 X 25 = 0
 23 =8 1 X 26 = 64

7710 12
Converting From Decimal to Binary
• Make a list of the binary place values up to the number being converted.
• Perform successive divisions by 2, placing the remainder of 0 or 1 in each of
the positions from right to left.
• Continue until the quotient is zero.
• Example: 4210

25 24 23 22 21 20
32 16 8 4 2 1
1 0 1 0 1 0

13
Working with Large Numbers
 0101000010100111 = ?

• Humans can’t work well with binary


numbers; there are too many digits to
deal with.
• Memory addresses and other data can be
quite large. Therefore, we sometimes
use the hexadecimal number system.

14
The Hexadecimal Number System

The hexadecimal number system is also


known as base 16. The values of the
positions are calculated by taking 16 to
some power.
Why is the base 16 for hexadecimal
numbers ?
 Because we use 16 symbols, the

digits 0 and 1 and the letters A


through F. 15
The Hexadecimal Number System

Binary Decimal Hexadecimal Binary Decimal Hexadecimal


0 0 0 1010 10 A
1 1 1 1011 11 B
10 2 2 1100 12 C
11 3 3 1101 13 D
100 4 4 1110 14 E
101 5 5 1111 15 F
110 6 6
111 7 7
1000 8 8
1001 9 9

16
The Hexadecimal Number System

Example of a hexadecimal number and


the values of the positions:
 3 C 8 B 0 5 1
 166 165 164 163 162 161 160

17
Example of Equivalent Numbers

 Binary: 1 0 1 0 0 0 0 1 0 1 0 0 1 1 12

 Decimal: 2064710

 Hexadecimal: 50A716

 Notice how the number of digits gets


smaller as the base increases. 18
Converting To and From Decimal

Decimal10
0123456789
Successive Weighted
Division Multiplication
Weighted Successive
Multiplication Division

Successive Weighted
Division Multiplication

Octal8 Hexadecimal16
01234567 0123456789ABCDEF

Binary2
01

19
Counting . . . 2, 8, 10, 16
Decimal Binary Octal Hexadecimal
0 00000 0 0
1 00001 1 1
2 00010 2 2
3 00011 3 3
4 00100 4 4
5 00101 5 5
6 00110 6 6
7 00111 7 7
8 01000 10 8
9 01001 11 9
10 01010 12 A
11 01011 13 B
12 01100 14 C
13 01101 15 D
14 01110 16 E
15 01111 17 F
16 10000 20 10
17 10001 21 11
18 10010 22 12
20
19 10011 23 13
Review: Decimal ↔ Binary
Base10 Successive
Division
Base2
DECIMAL BINARY
a) Divide the decimal number by 2; the remainder is the LSB of the binary
number.
b) If the quotation is zero, the conversion is complete. Otherwise repeat step
(a) using the quotation as the decimal number. The new remainder is the
next most significant bit of the binary number.

Base2 Weighted
Multiplication
Base10
BINARY DECIMAL
a) Multiply each bit of the binary number by its corresponding bit-weighting
factor (i.e., Bit-0→20=1; Bit-1→21=2; Bit-2→22=4; etc).
b) Sum up all of the products in step (a) to get the decimal number.
21
Conversion Process Decimal ↔ BaseN
(Any base including Binary2, Octal8, Hexidecimal16)

Base10 Successive
Division
BaseN
DECIMAL ANY BASE
a) Divide the decimal number by N; the remainder is the LSB of the ANY
BASE Number .
b) If the quotient is zero, the conversion is complete. Otherwise repeat step
(a) using the quotient as the decimal number. The new remainder is the
next most significant bit of the ANY BASE number.

BaseN Weighted
Multiplication
Base10
ANY BASE DECIMAL
a) Multiply each bit of the ANY BASE number by its corresponding bit-
weighting factor (i.e., Bit-0→N0; Bit-1→N1; Bit-2→N2; etc).
b) Sum up all of the products in step (a) to get the decimal number.
22
Decimal ↔ Octal Conversion
The Process: Successive Division
• Divide the decimal number by 8; the remainder is the LSB of the
octal number .
• If the quotation is zero, the conversion is complete. Otherwise repeat
step (a) using the quotation as the decimal number. The new
remainder is the next most significant bit of the octal number.

Example:
Convert the decimal number 9410 into its octal equivalent.
11
8 94 r  6  LSB

1  9410 = 1368
8 11 r 3

0
8 1 r  1  MSB 23
Example: Dec → Octal
Example:
Convert the decimal number 18910 into its octal equivalent.

Solution:

23
8 189 r 5  LSB

2  18910 = 2758
8 23 r 7

0
8 2 r2  MSB

24
Octal ↔ Decimal Process
The Process: Weighted Multiplication
• Multiply each bit of the Octal Number by its corresponding bit-
weighting factor (i.e., Bit-0→80=1; Bit-1→81=8; Bit-2→82=64;
etc.).
• Sum up all of the products in step (a) to get the decimal number.

Example:
Convert the octal number 1368 into its decimal equivalent.

1 3 6
82 81 80
Bit-Weighting  136 8 = 9410
64 8 1 Factors

64 + 24 + 6 = 9410
25
Example: Octal → Dec
Example:
Convert the octal number 1348 into its decimal equivalent.

Solution:

1 3 4
82 81 80

64 8 1
\ 1348 = 9210

64 + 24 + 4 = 9210

26
Decimal ↔ Hexadecimal Conversion
The Process: Successive Division
• Divide the decimal number by 16; the remainder is the LSB of the
hexadecimal number.
• If the quotation is zero, the conversion is complete. Otherwise
repeat step (a) using the quotation as the decimal number. The
new remainder is the next most significant bit of the hexadecimal
number.
Example:
Convert the decimal number 9410 into its hexadecimal equivalent.

5
16 94 r  E  LSB

0
16 5 r  5  MSB
 9410 = 5E16

27
Example: Dec → Hex
Example:
Convert the decimal number 42910 into its hexadecimal equivalent.

Solution:
26
16 429 r  D (13)  LSB

1
16 26 r  A (10)  42910 = 1AD16 = 1ADH
0
16 1 r 1  MSB

28
Hexadecimal ↔ Decimal Process
The Process: Weighted Multiplication
• Multiply each bit of the hexadecimal number by its
corresponding bit-weighting factor (i.e., Bit-0→160=1; Bit-
1→161=16; Bit-2→162=256; etc.).
• Sum up all of the products in step (a) to get the decimal number.

Example:
Convert the octal number 5E16 into its decimal equivalent.

5 E
161 160  5E 16 = 9410
Bit-Weighting
16 1 Factors

80 + 14 = 9410

29
Example: Hex → Dec
Example:
Convert the hexadecimal number B2EH into its decimal equivalent.

Solution:

B 2 E
162 161 160

256 16 1
\ B2EH = 286210

2816 + 32 + 14 = 286210

30
Example: Hex → Octal
Example:
Convert the hexadecimal number 5AH into its octal equivalent.
Solution:
First convert the hexadecimal number into its decimal equivalent,
then convert the decimal number into its octal equivalent.
11
5 A 8 90 r  2  LSB
161 160
1
16 1
8 11 r 3

0
80 + 10 = 9010 8 1 r  1  MSB

\ 5AH = 1328
31
Example: Octal → Binary
Example:
Convert the octal number 1328 into its binary equivalent.
Solution:
First convert the octal number into its decimal equivalent, then
convert the decimal number into its binary equivalent.
45
1 3 2 2 90 r  0  LSB
82 81 80 22
2 45 r 1
64 8 1 11
2 22 r 0 \ 1328 = 10110102
64 + 24 + 2 = 9010 5
2 11 r 1
2
2 5 r 1
1
2 2 r 0
0
32
2 1 r  1  MSB
Binary ↔ Octal ↔ Hex
Shortcut
Because binary, octal, and hex number systems are all powers of two
(which is the reason we use them) there is a relationship that we can
exploit to make conversion easier.
1 0 1 1 0 1 0 2 = 132 8 = 5A H
To convert directly between binary and octal, group the binary bits into
sets of 3 (because 23 = 8). You may need to pad with leading zeros.
0 0 1 0 1 1 0 1 0 2= 1 3 28

1 3 2 001 011 010


To convert directly between binary and hexadecimal number systems,
group the binary bits into sets of 4 (because 24 = 16). You may need to
pad with leading zeros.
0 1 0 1 1 0 1 0 2= 5 A 16

5 A 0101 1 010 33
Example: Binary ↔ Octal ↔ Hex
Example:
Using the shortcut technique, convert the hexadecimal number A6 16
into its binary & octal equivalent. Use your calculator to check your
answers.
Solution:
First convert the hexadecimal number into binary by expanding the
hexadecimal digits into binary groups of (4).
A 6 16
\ A616 = 101001102
1010 0110
Convert the binary number into octal by grouping the binary bits into
groups of (3).
010100110 \ 101001102 = 2468

2 4 6 34
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

35
Example:
binary to decimal number
Questions:

Convert the binary number below to


decimal

a) 0.1101
b) 1010111
c) 101011.011

36
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- 2- 2-
1 2 3

So, the binary number 1for 00.625


1 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.0101 LSB
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
THE 8421 BCD CODE
BCD stands for Binary-Coded Decimal.

A BCD number is a four-bit binary group


that represents one of the ten decimal
digits 0 through 9.
Example:
Decimal number 4926 4 9 2 6

8421 BCD coded number 0100 1001 0010 0110


TEST

Convert the BCD coded


number
1000 0111 0001 into
decimal.
BCD Coded Number 1000 0111 0001

Decimal Number 8 7 1
THE GRAY CODE
The Gray code’s most Decimal Gray code

important characteristic is 0 00000


1 00001
that only one digit 2 00011
changes as you increment 3 00010
4 00110
or decrement the count. 5 00111
The Gray code is commonly 6 00101
7 00100
associated with 8 01100
input/output devices such 9 01101
10 01111
as an optical encoder of a 11 01110
shaft’s angular position. 12 01010
13 01011
The Gray code is NOT a 14 01001
BCD code. 15 01000
16 11000
THE ASCII CODE

ASCII is acronym for American Standard


Code for Information Interchange
Represents numbers, letters, punctuation
marks and control characters
Standard ASCII is a 7-bit code (127
characters)
Extended ASCII (IBM ASCII), an 8-bit
code, is also very popular
Extended ASCII adds graphics and math
symbols to code (total of 256 symbols)
BCD(Binary Coded Decimal) Code

• In this code each digit is represented


by a 4-bit binary number.
• The positional weights assigned to the
binary digits in BCD code are 8-4-2-1
with 1 corresponding to LSB and 8
corresponding to MSB. •
Conversion from decimal to BCD
• The decimal digits 0 to 9 are converted into BCD, exactly in
the same way as binary.
In BCD only first ten codes are used (0000 to 1001) •

Conversion of bigger decimal numbers to BCD:


• Express each decimal digit with its equivalent 4-bit BCD
code
• Eg.: Convert (964)10 to its equivalent BCD code. There fore
(964)10 = (1001 0110 0100)BCD
• Hence smallest number in BCD is 0000 i.e., 0 and largest is
1001 i.e., 9 after which 10 will be expressed by combinations
i.e., 0001 0000 and is known as packed BCD.
Gray Code

• Non-weighted code. •
It has a very special feature that only one bit will change, each
time the decimal number is incremented, therefore also called
unit distance code.
Binary and Gray conversions: • For Gray to binary or binary to
Gray conversions let’s understand rules conversions let’s
understand rules for Ex-OR (Ex-OR is represented by symbol )
Rules for EX-OR:
Conversion from Binary to
Gray code:

Step 1: Write MSB of given Binary number as it is.


Step 2: Ex-OR this bit with next bit of that binary number
and write the result.
Step 3: Ex-OR each successive sum until LSB of that binary
number is reached.

1 0 1 0 0 1 1

1 1 1 1 0 1 0

Therefore (1010011)2 = (1111010)Gray


Conversion from Gray to
Binary:

Step 1: Write MSB of given Binary number as it is.


Step 2: Ex-OR this bit with next bit of that binary number and
write the result.
Step 3: Continue this process until LSB of that binary number is
reached.
• Eg.: Convert (1010111)Gray to its equivalent Binary number

1 0 1 0 1 1 1

1 1 0 0 1 0 1

Therefore (1010111)Gray = (1100101)2

You might also like