0% found this document useful (0 votes)
39 views

Chapter 2 Number System and Codes

Data in computers is represented in binary form using only 1s and 0s. This is because at a fundamental level, computers can only understand electrical signals representing these two states. Various number systems like decimal, binary, octal and hexadecimal are used to represent numeric data in different bases. Numbers can be converted between these number systems by dividing the number by the new base to obtain remainders that make up the number in that base.

Uploaded by

senawagari07f
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Chapter 2 Number System and Codes

Data in computers is represented in binary form using only 1s and 0s. This is because at a fundamental level, computers can only understand electrical signals representing these two states. Various number systems like decimal, binary, octal and hexadecimal are used to represent numeric data in different bases. Numbers can be converted between these number systems by dividing the number by the new base to obtain remainders that make up the number in that base.

Uploaded by

senawagari07f
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Introduction

• Data in computers is represented in binary form.


• The represented data can be number, text, movie, color (picture), sound, or anything
else.
• It is up to the application software that presents the data to portray the data accordingly.
• We enter data into a computer using letters, digits & special symbols.
• But inside the computer, there is no color, letter, digit or any other character inside the
computer system unit.
• Just like any other electrical device, computers understand and respond to only the flow
of electrical charge.
• They also have storage devices that work based on magnetism.
1
… Cont’d

• This shows that the overall structure of computers work only in binary conditions : :

– (the semi-conductors are conducting or not conducting,


– a switch is closed or opened,
– a magnetic spot is magnetized or demagnetized).
• Hence, data must be represented in the form of binary code that has a corresponding
electrical signal.

2
Number Systems
• Basically, there are two types of number systems.
1. Non-positional number system:
• The symbols of the number have the same value regardless of its position in the
number.
• The value of a symbol (digit) in a number does not depend on the position of the
digit in number.
2. Positional number system:
• The value of a symbol in the number is determined by its position, the symbol and
the base of the number system.
• In all positional number systems, the base has the following properties:
3
… Cont’d

I. It determines the number of different symbols it has.


– For example, there are 10 different symbols in base 10 (decimal) number system and
there are 2 symbols in base 2 (binary) number system.
II. The maximum value of a single digit is one less than the base value.
– For example, the largest single digit number in the decimal (base 10) number system
is 9.

III. The positional value of each symbol is expressed by the power of the base.

Table 2.1 Positional Interpretation of a Number in Base 7

4
Decimal Number System
• The decimal number system, also called the base 10 number system, is the number
system we use in our day-to-day life.
• The preference of this number system by humans is attributed to their nature that
humans have 10 fingers.
• It is believed that humans start counting using their fingers.
• This fact is the basis for the preference of the decimal number system by humans.
• The decimal number system has 10 different symbols identified as 0, 1, 2, 3, 4, 5, 6, 7,
8, and 9.
• All decimal numbers are written as a combination of these 10 digits.
• Example: number 83 means eight tens plus three:
83 = (8 * 10) + 3 5
… Cont’d

• The number 4728 means four thousands, seven hundreds, two tens, plus eight:
4728 = (4 * 1000) + (7 * 100) + (2 * 10) + 8
• The decimal system is said to have a base, or radix, of 10.
• This means that each digit in the number is multiplied by 10 raised to a power
corresponding to that digit’s position:

• The same principle holds for decimal fractions, but negative powers of 10 are used.
• Thus, the decimal fraction 0.256 stands for 2 tenths plus 5 hundredths plus 6
thousandths:

6
… Cont’d

• A number with both an integer and fractional part has digits raised to both positive and
negative powers of 10:

• In any number, the leftmost digit is referred to as the most significant digit, because it
carries the highest value.
• The rightmost digit is called the least significant digit.
• In the above example, the 4 on the left is the most significant digit and the 6 on the right
is the least significant digit.
• In general, for the decimal representation of X = {…d 2d1d0.d-1d-2d-3…}, the value of X is:

7
Binary Number System
• In the binary system, we have only two digits, 1 and 0.
• Thus, numbers in the binary system are represented to base 2.
• To avoid confusion, we will sometimes put a subscript on a number to indicate its base.
• For example, 8310 and 472810 are numbers represented in decimal notation or, more
briefly, decimal numbers.
• The digits 1 and 0 in binary notation have the same meaning as in decimal notation:

• Again, fractional values are represented with negative powers of the radix:

• In general, for the decimal representation of Y = {…b 2b1b0.b-1b-2b-3…}, the value of Y is:
8
Octal Number System
• Octal number system also called base 8 number system,
ↆ has 8 different symbols: 0, 1, 2, 3, 4, 5, 6, and 7.
• The octal number system is used to write binary numbers in short form.
• An octal umber has about one-third of the digits in its binary equivalent.

9
Hexadecimal Number System

• The hexadecimal number system, also called base 16 number system,


ↆ has 16 different symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.
• The hexadecimal number system is usually referred as hex for short.
• It is used to write binary numbers in short form.
• A hex number has about one-fourth of the digits in its binary equivalent.
• Memory addresses and MAC addresses are usually written in hex.
• In hexadecimal number system, Binary digits are grouped into sets of four bits, called a
nibble.
• Each possible combination of four binary digits is given a symbol, as follows:

10
Converting from one Base to Another Base
1. Conversion from Decimal to Base m
Step 1: Divide the given decimal number by m (the desired base).
• The result will have a quotient and a remainder.
Step 2: Divide the quotient by m.
• Still you get a quotient and a remainder.
Step 3: Repeat step 2 until the quotient becomes 0.
• You should note that we are conducting integer division.
• In integer division n/m, the quotient is 0 whenever n < m.
Step 4: Collect and arrange the remainders in such a way that the first remainder is the least
significant digit and the last remainder is the most significant digit (i.e., RnRn-1 … R2R1).

11
… Cont’d

• Example: Convert the following decimal number 47 into binary, octal, and hexadecimal.
a) Conversion to binary
• In order to convert the given decimal numbers into binary (base 2), they are divided by 2.

• Since the quotient becomes 0 at the last division, the division has to stop and we should
collect the remainders starting from the last one.
• Hence the result is 1011112.
• Note that, starting from the second division, at each consecutive division the quotient of
12
… Cont’d

b) Conversion to Octal
• Here the numbers are divided by 8 because the required base is octal (base 8).

c) Conversion to Hexadecimal
• Since the conversion now is into hexadecimal (base 16) the given decimal numbers are
divided by 16.

• The hexadecimal equivalent for the decimal 15 is F and that of 2 is 2.


• Therefore, 47 = 2F16
13
… Cont’d

2. Conversion from Base m to Decimal


Step 1: Multiply each digit by its positional value.
Step 2: Calculate the sum of the products you get in step 1.
• The resulting sum you get is the decimal equivalent for the given number in base m.
Example 1: Convert the binary number 110001 into decimal.

• It is evident that calculating the product of 0s since the product is 0 and do not contribute
anything to the final result.
• However, you should remember to skip the positional value as well.
14
… Cont’d

Example 2: Convert the octal number 22 into decimal.

Example 3: Convert the hexadecimal number D1 into decimal.


D116 = (13 × 161) + (1 × 160); you should be aware that the calculations are in decimal thus the
hex digit D must first be converted into its decimal equivalent (13).

15
… Cont’d

3. Conversion from Binary to Octal


• It is possible to use decimal number system as an intermediate base to convert from any base
to any other base.
• However, for conversion from binary to octal or vice versa, there is a very simple method.
• Example 1:Convert the binary numbers 110011 and 1101111 to octal.

• The bits are grouped in three with the equivalent octal digit given below
the three bit group.
• Thus, 1100112 = 638

• Since we are left with a single bit at the leftmost position, two
0s are added at the front to make create a three-bit group.
• The result shows that 11011112 = 1578.

16
… Cont’d

4. Conversion from Octal to Binary


Step 1: For each octal digit, find the equivalent three digit binary number.
Step 2: If there are leading 0s for the binary equivalent of the leftmost octal digit, remove them.
Example: Find the binary equivalent for the octal numbers 73 and 160.

• Since there are no leading 0s at the leftmost position


(the bits for octal 7), there is no 0 to remove.
• Therefore, 738 = 1110112.

• The binary equivalent for the leftmost octal digit


1 has two 0s.
• To get the final result, remove them and
concatenate the rest.
• Therefore, 1608 = 11100002.
17
… Cont’d

5. Conversion from Binary to Hexadecimal


Step 1: Starting from the rightmost bit, group the bits in 4.
 If the remaining bits at the leftmost position are fewer than 4, add 0s at the front.
Step 2: For each 4-bit group, find the corresponding hexadecimal number.
Example: Convert the binary numbers 1110110001 and 10011110 to hexadecimal.

18
… Cont’d

6. Conversion form Hexadecimal to Binary


Step 1: For each hexadecimal digit, find the equivalent four digit binary number.
Step 2: If there are leading 0s for the binary equivalent of the leftmost hexadecimal digit,
remove them.
Example: Find the binary equivalents for the hexadecimal numbers 1C and 823.
• After removing the leading 0s for the binary equivalent
of the leftmost hexadecimal number 1, the result
becomes 11100.
• Therefore, 1C16 = 111002.
• There is no leading 0s for the binary equivalent of the
hexadecimal number 8; we simply concatenate the binary
digits to get the final result.
• Hence, 82316 = 1000001000112.
19
… Cont’d

7. Conversion from Octal to Hexadecimal of Vice Versa


• The decimal number system can be used as an intermediate conversion base.
• As it is shown in the above sections, however, it the binary number system is quite
convenient for conversion to or from octal and hexadecimal.
• To convert an octal number to a hexadecimal number or from hexadecimal to octal, the
binary number system is used as an intermediate base.
Step 1: Convert the given number into binary.
Step 2: Convert the binary number you got in step 1 into the required base.
Example 1: Convert the octal number 647 to hexadecimal.
Step 2: Convert 1101001112 to hexadecimal
Step 1: Convert 6478 to binary 0001 1010 0111
6 4 7 1 A 7
110 100 111 Therefore, 6478 = 1A716 20
… Cont’d

Example 2: Find the octal equivalent for the hexadecimal number 3D5
Step 1: Convert 3D516 to binary
3 D 5
0011 1101 0101
Step 2: Convert 11110101012 to octal
001 111 010 101
1 7 2 5
Therefore, 3D516 = 17258

21
Integer Representation
• Integer can be represented as unsigned and signed
• Signed cane be classified into 3 such as :
1. Sign and magnitude
2. 1's complement
3. 2's complement
• Properties to follow for both unsigned and signed integers
 Only one bit pattern per value
 Equal number of positive and negative values
 Maximum ranges of values
 No gaps in the range

22
… Cont’d

A. Unsigned integer
Number of bits Range
• It is an integer without a sign.
8 0 - 255
• Its range is between 0 and positive infinity.
16 0 - 65,535
• Example 1: store 7 in an 8-bit memory location
Solution: first convert 7 in to binary which is 111
second add five 0’s to make total of N(8) bits which is
00000111
• Example 2: store 258 in an 16-bit memory location
Solution: first convert 258 in to binary which is 100000010
second add seven 0’s to make total of N(16) bits which is
0000000100000010
23
… Cont’d

Examples of unsinged integer in 2 different computers

Decimal 8-bit location 16-bit location


7 0000000000000111
00000111
234 11101010 0000000011101010

258 Overflow 0000000100000010

24760 Overflow 0110000010111000

1,245,678 Overflow Overflow

24
… Cont’d

B. Signed integer
• Used to represent negative and positive integers.
i. Signed and Magnitude
• Left most significant bit represent the sign of the integers and the remaining bits
represent magnitude.
• If the leftmost bit is 0 the number is positive.
• And if the leftmost bit is 1 the number is negative.
• Example 1: store +7 in an 8-bit memory location using sign and magnitude.
Solution: 1) convert 7 in to binary which is 111
2) add four 0’s to make total of N-1(7)-bits which is
0000111
3) add an extra 0 because the number is positive.
So, the result is 00000111 25
… Cont’d

• Example 2: store -258 in an 16-bit memory location using sign and magnitude.
Solution: 1) convert 258 to binary which is 100000010
2) add six 0’s to make total of N-1(15)-bits which is 000000100000010
3) add an extra 1 because the number is negative.
So, the result is 1000000100000010
2. 1's complement
• The 1's complement of a number is found by changing all 1's to 0's and all 0's to 1’s.
• This is called as taking complement or 1's complement.

26
… Cont’d

3. 2's complement
• The 2's complement of binary number is obtained by adding 1 to the Least
Significant Bit (LSB) of 1's complement of the number.
2's complement = 1's complement + 1
• Example:

27
Fixed and floating point representation
1. Fixed Point Representation
• There is fixed number of digits after decimal part.
• Unsigned fixed point number
• Signed fixed point number
A. Unsigned fixed point number
• Example: Represent fixed point representation of unsigned binary number 0110.110
using 4 integer bits and 3 fractional bits.
• Solution: 0110.110
= 0*23+1*22+1*21+0*20+1*2-1+1*2-2+0*2-3
= 0+4+2+0+0.5+0.25+0
= (6.75)10
28
… Cont’d

B. Signed fixed point number


• Can be represented using sign and magnitude.
• Example: Represent (-7.5)10 using 8-bit binary representation with 4 integer and 4
fraction bit.
• Solution: step 1: leave the – sign and take only the positive (7.5)
step 2:convert 7.5 to equivalent binary
(7.5)10 = (111.1) the required bit is 8 so, add 1 zero to leftmost bit and 3
zeros to the rightmost bit.
(7.5)10 = (0111.1000)
step 3: to find the –ve of a given number find two’s complement of the
number.
29
… Cont’d

• Example: Convert decimal number 8.125 to fixed point binary number.


• Solution: 0.125*2 =0.25 0
integer part Fraction part
0.25*2 = 0.5 0
8 = (1000) 0.125 = (.0010)
0.5*2 = 1.0 1
0.0*2 = 0.0 0
• Therefore, (8.125)10 = (1000.0010)2
• Problem 2: The following binary number are stored using two’s complement in a 12-
bit register with a 4 bit after binary point
1) (011111111111).
Integer part
• solution: 011111111.1111 Fraction part
-128 64 32 16 8 4 2 1 0.5 0.25 0.125 0.0625
0 1 1 1 1 1 1 1 1 1 1 1

= 64+32+16+8+4+2+1+0.5+0.25+0.125+0.0625
30
(011111111111) = (127.9375)
… Cont’d

2) (111111111111).
• solution: 111111111.1111 Integer part
Fraction part
-128 64 32 16 8 4 2 1 0.5 0.25 0.125 0.0625
1 1 1 1 1 1 1 1 1 1 1 1

= -128+64+32+16+8+4+2+1+0.5+0.25+0.125+0.0625
(111111111111)2 = (-0.0625)10
2) (000001110010). Integer part
Fraction part
solution: 00000111.0010
-128 64 32 16 8 4 2 1 0.5 0.25 0.125 0.0625
0 0 0 0 0 1 1 1 0 0 1 0

31
=4+2+1+0.125 = (7.125)
Floating Point Representation
• Binary point floats to the right of most significant 1 and an exponent is used.
• It can be represented in the scientific notation.
• It has three parts.
• Mantissa • Scientific Representation is ± mantissa * Base Exponent
• Base
• Exponent
• Example: number Mantissa Base Exponent
9*108 9 10 8
110*27 110 2 7
4346.254 4346254 10 -3

32
… Cont’d

IEEE 754 Floating Point Number Representation


• It is standard which can be represented in two ways:
1. Sigle Precision Format:
• It uses 32 bits for representation.
31 30 23 0
Sign Exponent Mantissa

1 bit 8 bits 23 bits

2. Double Precision Format


• It uses 64 bits for representation.
63 62 51 0
Sign Exponent Mantissa

1 bit 11 bits 52 bits 33


… Cont’d

Example: Represent (1259.125)10 in Single and double precision format.


Solution:
Step 1: Convert decimal number to binary form. Divide it into two parts
• Now combined the two parts
(1259)10 = (10011100011)2

(0.125)10 = (001)2

(1259.125)10 = (10011100011.001)2
Step 2: Normalize the number.
• formula to normalize in single precision is (1.N)2E-127
• formula to normalize in Double precision is (1.N)2E-1023
1.0011100011001*210
Step 3: Single Precision format
34
(1.N)2E-127 =
1.0011100011001*210
… Cont’d

• E - 127 =10 • (137)10 = (10001001)2


• E = 137
• Now convert 137 to binary
31 30 23 0
0 10001001 0011100011001…………
1 bit 8 bits 23 bits
Step 4: Double Precision format
(1.N)2E-1023 =
1.0011100011001*210
• E - 1023 =10 E = 1033 = (10000001001)2
63 62 51 0
0 10000001001 0011100011001….

1 bit 11 bits 52 bits 35


Binary Codes
• In the coding, when numbers, letters or words are represented by a specific group of symbols,
it is said that the number, letter or word is being encoded.
• The group of symbols is called as a code.
• The digital data is represented, stored and transmitted as group of binary bits.
• This group is also called as binary code.
• The binary code is represented by the number as well as alphanumeric letter.
Advantages of Binary Code
 Following is the list of advantages that binary code offers.
• Binary codes are suitable for the computer applications.
• Binary codes are suitable for the digital communications.
• Binary codes make the analysis and designing of digital circuits if we use the binary codes.
• Since only 0 & 1 are being used, implementation becomes easy. 36
… Cont’d

Binary Coded Decimal (BCD)


• It is a system of writing numerals that assigns a 4-bit binary code to each digit 0 through 9
in decimal(base 10) numerals.
• In the BCD, with four bits we can represent sixteen numbers (0000 to 1111).
• But in BCD code only first ten of these are used (0000 to 1001).
• The remaining six code combinations i.e. 1010 to 1111 are invalid in BCD.

• Numbers larger than 9, having 2 or more digits in decimal system are expressed digit by
digit. 1 8 9 5
0001 1000 1001 0101
• Example: BCD representation of base 10 numbers 1895 is
37
… Cont’d

Advantages of BCD Codes


• It is very similar to decimal system.
• We need to remember binary equivalent of decimal numbers 0 to 9 only.
Disadvantages of BCD Codes
• The addition and subtraction of BCD have different rules.
• The BCD arithmetic is little more complicated.
• BCD needs more number of bits than binary to represent the decimal number.
o So BCD is less efficient than binary.

38
… Cont’d

Alphanumeric codes
• Binary digit or bit can represent only two symbols as it has only two states '0' or '1’.
• But this is not enough for communication between two computers because there we
need many more symbols for communication.
• Alphanumeric codes consist of numbers as well as alphabetic characters.
• Commonly used Alphanumeric Codes:
1. ASCII - American Standard Code for Information Interchange
2. EBCDIC - Extended Binary Coded Decimal Interchange Code
• These codes contains: a) 26 alphabets with capital and small letters,
b) numbers (0 – 9)
c) punctuation marks and other symbols.
39
… Cont’d

• ASCII code is a 7-bit code and more commonly used worldwide.


27 = 128 symbols.
• 65 – 90 A – Z
• 97 – 122 a – z
• 48 – 57 0 -9
• EBCDIC is a 8-bit code used in large IBM computers.
• 28 = 256 symbols.

• Binary representation of ASCII code of 'A ‘ is 1000001


• Binary representation of ASCII code of 'N ' is 1001110

End!!!
40

You might also like