41
Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

Embed Size (px)

Citation preview

Page 1: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

Digital LogicLecture 2

Number Systems

ByDr Zyad DwekatThe Hashemite UniversityComputer Engineering Department

Page 2: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 2

Outline

Introduction. Basic definitions. Number systems types. Conversion between different number

systems. Examples.

Page 3: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 3

Introduction There are 4 numbering systems:

Binary: 2 digits (0 and 1). Decimal: 10 digits (0 – 9). Octal: 8 digits (0 – 7). Hexadecimal: 16 digits (0 – 9 and A – F).

The binary system is what used by computers. The decimal system is what used by humans

in nature (also called natural number system). Hexadecimal and Octal are a compromise

representation between the binary and the decimal.

Page 4: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 4

Basic Definitions I The number of digits used within a number system is called the Radix or the Base.

So:Binary Base = 2Octal Base = 8Decimal Base = 10Hexadecimal Base = 16

The point in the number, i.e. fractional number, is called radix point. so, based on the radix or base you call it, e.g. in decimal you call it decimal point and in binary you call it binary point.

The used digits in each numbering system are borrowed from the decimal system and completed by the alphabet characters (capital letters).

In other words, you are able to use the numerals 0-9 and the alphabets A-Z based on the number of digits you need. Then you pick the wanted number of digits in order.

Page 5: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 5

Basic Definitions II For binary, you need two digits, starting from the numerals set (i.e.

0-9) you get 0 and 1 (just two digits). E.g. 10001110. For octal, you need 8 digits, following the same approach you get

the digits 0-7. E.g. 756. For decimal, you need 10 digits then you get 0-9. E.g. 190. For hexadecimal you need 16 digit, starting by the numerals you get

0-9 but still you need an additional 6 digits. For this purpose you move to the alphabet letters and pick 6 letters, so you get A-F. E.g. AD4.

In decimal these letters correspond to the following:A 10B 11C 12D 13E 14F 15

Page 6: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 6

Basic Definitions III To differentiate between numbers from the different numbering

systems use one of the following: Add a subscript of the base to the number.

e.g: (1000111)2 Binary (176)8 Octal (AD23)16 Hexadecimal

Add the following letters at the end of the number: B (Binary), Q (Octal), nothing for Decimal, H (Hexa). E.g. 10010B

The universal approach to represent hexadecimal numbers is to start it with a 0 and append ‘h’ to the end of the number. E.g. 0Dh.

A less common approach to represent binary and hexadecimal numbers is to proceed the number with a:% for binary$ for hexadecimale.g. %101001, $AD5

Any number proceeded by ‘0x’ is a hexadecimal number. Such representation is most commonly used in programming languages such as C++.

Page 7: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 7

Basic Definitions IV Every number in any numbering system can be

represented as a weighted sum or a power series of the system radix.

The coefficient of each term in the series is the digit value where the power of the base of that term depends on the location of the digit within the number with respect to the radix point (left or right).

E.g.: (1890)10 = 1x103 + 8x102 + 9x101 + 0x100

(45.678)10 = 4x101+5x100+6x10-1+7x10-2+8x10-3

(ADF)16 = 10x162 + 13x161 + 15x160

Note: the result of this power series is the decimal equivalent of the original number as we will see later.

Page 8: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 8

Basic Definitions V In computer hardware the following are used:

Bit = 0 or 1Nibble = 4 bits.Byte = 8 bitsWord = 2 byte = 16 bits210 = 1 Kilo = 1024220 = I Mega230 = 1 Giga240 = 1 Tera

1 keyboard character needs 1 byte to store its value. Every thing in computers is power of 2. The architecture of the computer is determined based

on its word length, e.g. 32 bit or 64 bit architecture.

Page 9: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 9

Basic Definitions VI

The least significant bit (LSB) is the bit or digit that is located on the right most location within a number.

The most significant bit (MSB) is the bit that is located on the leftmost position within a number.

E.g.: for the number 167829, 1 is the MSB and 9 is the LSB.

Page 10: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 10

Why binary system for computers?

Computers are comprised basically of switches.

Switches have only tow states: either ON or OFF.

As we learned, such states can be represented as two values: 1 and 0.

So it is binary since there are two values and each value needs one digit or bit to represent and store it.

Page 11: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 11

Why decimal system for humans?

Created with human history on earth and evolved from nature.

Easy to understand. Compact: need small number of digits to

represent large values. Also, humans have invented shorthand or

abbreviations to simplify large decimal numbers representation to enhance readability, such as million, billion, trillion, etc.

Page 12: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 12

Conversion Between Binary and Decimal I

From Binary to Decimal: Expand the binary number in a power series and

sum the terms as shown previously. The same method is used when there is a radix

point in the number. This method can be used to convert from any

base to decimal. Examples: the binary value 11001010 represents :

= 1x27 + 1x26 + 0x25 + 0x24 + 1x23 + 0x22 + 1x21 + 0x20

= 1x128 + 1x64 + 0x32 + 0x16 + 1x8 + 0x4 + 1x2 + 0x1

= 128 + 64 + 0 + 0 + 8 + 0 + 2 + 0 =202

Page 13: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 13

Conversion Between Binary and Decimal II

From Decimal to Binary: Here there are two cases: integer decimal numbers

and fraction (floating point) decimal numbers. For integer decimal numbers, there are two

methods:1-Repeated Division By 2

For this method, divide the decimal number by 2, If the remainder is 0, on the right side write down a 0.

If the remainder is 1, write down a 1. Repeat the division with the result (new quotient).

This process is repeated till the quotient becomes 0. When performing the division, the remainders which will represent the binary equivalent of the decimal number are

written beginning at the least significant digit (right) and each new digit is written to more significant digit (the left) of

the previous digit.

Page 14: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 14

Integer Decimal to Binary – First Method -- Example

Example:57 / 2 = 28, remainder = 1 (binary number will end with 1)

28 / 2 = 14, remainder = 0

14 / 2 = 7, remainder = 0

7 / 2 = 3, remainder = 1

3 / 2 = 1, remainder = 1

1 / 2 = 0, remainder = 1 (binary number will start with 1)

Therefore, collecting the remainders, 5710 = 1110012

Page 15: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 15

Integer Decimal to Binary – Second Method

2- Sum on the fly List the powers of 2 starting from 0 power where

you stop at the maximum power less than the number you want to convert.

See which of these powers must be included in the sum that will have the result of the decimal

number that you want to convert. These powers will have 1 in its position and the

powers that are not used will have 0. This method is suitable for small to mid size

numbers.

Page 16: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 16

Integer Decimal to Binary – Second Method -- Example

Example: convert 5710 to binary

Answer = 111001

25 24 23 22 21 20

32 16 8 4 2 1

1 1 1 0 0 1

Page 17: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 17

Fraction Decimal to Binary As for integers, for fraction decimal numbers we use

the following method:Repeated Multiplication By 2

For this method, multiply the decimal number by 2, If the integer part of the result is 0, on the right side write

down a 0. If the integer part of the result is 1, write down a 1.

Then remove the integer part from the result and repeat the multiplication by 2 for the fraction part.

This process is repeated till the fraction becomes 0 or till you reach the sufficient accuracy.

When performing the multiplication, the integers which will represent the binary equivalent of the decimal fraction are

written beginning from the right of the decimal point and each new digit is written to less significant digit (the right) of the

previous digit. Could be unending!!!!!

Page 18: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 18

Fraction Decimal to Binary -- Example

Example: convert 0.310 to binary.

0.3 * 2 = 0.6 integer part = 00.6 * 2 = 1.2 integer part = 10.2 * 2 = 0.4 integer part = 00.4 * 2 = 0.8 integer part = 00.8 * 2 = 1.6 integer part = 10.6 * 2 = 1.2 integer part = 1, etc.

Answer = 0.010011...

Page 19: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 19

Notes The same methods used to convert from

decimal (integer or fraction) to binary are used to convert from decimal to any other base. Just change the radix or base value that you use in the multiplication or in the division.

When you face a number that have both integer and fraction parts, e.g. 107.82 or 1011.11011, convert each part alone and then combine them.

Page 20: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 20

Why to introduce the hexadecimal and the octal (I)?

Middle approach between binary and decimal systems:

Binary is a must by computers but it is verbose, i.e. need large number of bits to represent small quantities, e.g. 20210 needs 8 bits in binary.

Decimal is preferred by humans, easy to understand and compact (needs small number of bits to represent values).

Conversion between binary and decimal (especially decimal to binary) is not trivial and expensive in terms of the needed resources.

Page 21: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 21

Why to introduce the hexadecimal and the octal (II)?

To keep commonality with binary system, ease of conversion, and the compact feature we need another number system whose base is a power of 2.

Base 4 is not compact, however, base 8 (octal) and base 16 (hex) have this feature where base 16 is the most common.

From here the hexadecimal system have been introduced to be a middle approach between binary, where it is easy to convert between hexa and binary, as we will see, and the compactness feature as decimal.

Recall that computers store data in binary. However, hexa is commonly used in computers to represent locations (addresses) in data storage, in data access, and in RAM.

Page 22: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 22

Conversion Between Binary and Octal

The octal has a base = 8 which is 2 raised to power 3. In other words, each digit in the octal system can substitute for 3 bits in the binary system.

Such observation is utilized in the conversion process between octal and binary.

Page 23: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 23

Binary to Octal Conversion

Start from the radix point to the left and to the right and do the following:

Group the binary bits found in the binary number into groups consist of 3 bits.

Substitute each group with the octal digit that represents the same quantity represented by the binary group.

The same process are used with the bits on the right of the radix point (i.e. fraction).

Page 24: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 24

Binary to Octal Conversion - Example

• Convert the following number into octal: 10010110101111.10111112

010 010 110 101 111 . 101 111 1002

2 2 6 5 7 . 5 7 48

Answer = 22657.5748

Page 25: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 25

Octal to Binary Conversion

Just repeat the inverse of the process used to convert from binary to octal.

In other words, substitute each octal digit with its 3 bit binary equivalent.

Example: convert 567.428 to binary.

5 6 7 . 4 28

101 110 111 . 100 0102

Answer = 101110111.1000102

Page 26: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 26

Octal to Decimal Conversion

From Octal to Decimal: Use the power series expansion method

with the base = 8. Example: convert 675.518 to decimal.

= 6x82 + 7x81 + 5x80 + 5x8-1 + 1x8-2

= 445.64062510

Page 27: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 27

Integer Decimal to Octal Conversion

From Integer Decimal to Octal: Use the repeated division method used in

converting from decimal to binary but the difference here is that you divide by 8 (radix value) instead of 2.

Page 28: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 28

Integer Decimal to Octal Conversion - Example

Convert (44978)10 to Octal

Answer = 1276628

Division Quotient Remainder

44978 / 8 5622 2

5622 / 8 702 6

702/8 87 6

87/8 10 7

10/8 1 2

1/8 0 1

Page 29: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 29

Fraction Decimal to Octal Conversion

Repeated Multiplication By 8 For this method, multiply the decimal number by 8.

Write down the integer part of the result. Then remove the integer part from the result and repeat the

multiplication by 8 for the fraction part. This process is repeated till the fraction becomes 0 or till you

reach the sufficient accuracy. When performing the multiplication, the integers which will

represent the octal equivalent of the decimal fraction are written beginning from the right of the decimal point and each

new digit is written to less significant digit (the right) of the previous digit.

Could be unending!!!!!

Page 30: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 30

Fraction Decimal to Octal Conversion - Example

Example: convert 0.35610 to octal.

0.356 * 8 = 2.848 integer part = 20.848 * 8 = 6.784 integer part = 60.784 * 8 = 6.272 integer part = 60.272 * 8 = 2.176 integer part = 20.176 * 8 = 1.408 integer part = 10.408 * 8 = 3.264 integer part = 3, etc.

Answer = 0.266213...8

Page 31: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 31

Conversion Between Binary and Hexadecimal

The Hexa has a base = 16 which 2 raised to power 4. In other words, each digit in the hexa system can substitute for 4 bits in the binary system.

Similar to octal, such observation is utilized in the conversion process between hexa and binary.

Page 32: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 32

Binary to Hexa Conversion

Start from the radix point to the left and to the right and do the following:

Group the binary bits found in the binary number into groups consist of 4 bits.

Substitute each group with the Hexa digit that represents the same quantity represented by the binary group.

The same process are used with the bits on the right of the radix point (i.e. fraction).

Page 33: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 33

Binary to Hexa Conversion - Example

• Convert the following number into Hexa: 10010110101111.10111112

0010 0101 1010 1111 . 1011 11102

2 5 A F . B E16

Answer = 25AF.BE16

Page 34: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 34

Hexa to Binary Conversion

Just repeat the inverse of the process used to convert from binary to hexa.

In other words, substitute each hexa digit with its 4 bit binary equivalent.

Example: convert 56D.4216 to binary.

5 6 D . 4 216

0101 0110 1101 . 0100 00102

Answer = 010101101101.010000102

Page 35: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 35

Hexa to Decimal Conversion

From Hexa to Decimal: Use the power series expansion method

with the base = 16. Example: convert 67B.5116 to decimal.

= 6x162 + 7x161 + 11x160 + 5x16-1 + 1x16-2

= 1659.31640610

Page 36: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 36

Integer Decimal to Hexa Conversion

From Integer Decimal to Hexa: Use the repeated division method used in

converting from decimal to binary but the difference here is that you divide by 16 (radix value) instead of 2.

Page 37: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 37

Integer Decimal to Octal Conversion - Example

Example: Convert 3524310 to hexa

35243 / 16 = 2202, remainder = 11 B (hex number will end with B)

2202 / 16 = 137, remainder = 10 A

137 / 16 = 8, remainder = 9

8 / 16 = 0, remainder = 8 (hex number will start with 8)

Therefore, collecting the remainders, 3524310 = 89AB16

Page 38: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 38

Fraction Decimal to Octal Conversion

Repeated Multiplication By 16 For this method, multiply the decimal number by 16.

Write down the integer part of the result. Then remove the integer part from the result and repeat the

multiplication by 8 for the fraction part. This process is repeated till the fraction becomes 0 or till you

reach the sufficient accuracy. When performing the multiplication, the integers which will

represent the hexa equivalent of the decimal fraction are written beginning from the right of the decimal point and each

new digit is written to less significant digit (the right) of the previous digit.

Could be unending!!!!!

Page 39: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 39

Fraction Decimal to Octal Conversion - Example

Example: convert 0.35610 to hexa.

0.356 * 16 = 5.696 integer part = 50.696 * 16 = 11.136 integer part = B0.136 * 16 = 2.176 integer part = 20.176 * 16 = 2.816 integer part = 20.816 * 16 = 13.056 integer part = D0.056 * 16 = 0.896 integer part = 0, etc.

Answer = 0.5B22D0...16

Page 40: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 40

Conversion Between Octal and Hexadecimal

From octal to hexadecimal: First convert from octal to binary then

convert the result from binary to hexadecimal.

From hexadecimal to octal: First convert from hexa to binary then

convert the result from binary to octal.

Page 41: Digital Logic Lecture 2 Number Systems By Dr Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University 41

Additional Notes

This lecture covers the following material from the textbook: Chapter 1: Sections 1.2 – 1.4