Vous êtes sur la page 1sur 23

40

Computer Fundamentals & Programming in C

Chapter

BINARY NUMBER SYSTEM


Data is stored in a computer in the form of 0s and 1s. This is because computers understand
only binary language (which consists of just two digits, 0 and 1). Therefore, for a computer, all data
and information is reduced to numbers, whether the user stores songs, pictures, numbers, or
documents, all information is treated as binary numbers.
In this chapter, we will learn about the different types of codes that convert human language,
consisting of numeric and alphanumeric characters, into machine language which the computer
understands.

2.1 NUMBER SYSTEM


A number system defines a set of values to represent quantity. We talk about the number of
people attending a class, the number of modules taken by each student and use numbers to represent
grade.
Number System can be categorized in two systems:
(a) Non-Positional Number System

(b) Positional Number System.

Non-Positional Number System


In ancient times, people used to count on fingers,
when the fingers became insufficient for counting, stones,
pebbles or sticks were used to indicate values. But it was
very difficult to perform arithmetic with such a number
system as there is no symbol for zero.

Positional Number System


In this system the value of each digit is defined not
by the symbol but also by the symbol position. Positional
Number System is used to perform arithmetic. Existing
Positional number system is decimal number system.
Apart from the decimal number system, there are binary
number systems, octal number system and hexadecimal
number system.

People use the base-10 system


because we have 10 fingers!

1 3 5 7 8 9 6
6
5
4
3
2
1
0
10 10 10 10 10 10 10

Base (Radix)
In the number system the base or radix tells the number of symbols used in the system. In the
earlier days, different civilizations were using different radixes. The Egyptian used the radix 2, the
Babylonians used the radix 60 and Mayans used 18 and 20. The base of a number system is indicated
by a subscript (decimal number) and this will be followed by the value of the number. For example
(952) 10, (456) 8, (314) 16.
40

Binary Number System

41

Number System that is used by the computers is:


q Binary [ 2 ]:
Uses 2 symbols: the digits 0 and 1.
Some numbers in this system: 0, 000, 1010.
q Decimal [ 10 ]:
Uses 10 symbols: the digits 0, 1, 2, ,9.
Some numbers in this system: 1, 35, 543.
q Octal [ 8 ]:
Uses 8 symbols: the digits 0, 1, 2, 3, 4, 5, 6, 7.
q Hexadecimal [ 16 ]: Uses 16 symbols: 0, 1, 2, ., 9, A, B, C, D, E, F.
Notes: When written down, a number may be ambiguous regarding which system it belongs
to. So we will associate a subscript to clear such ambiguities. 1010 in the binary system will be
denoted as 1010[2] to distinguish it from 1010 of the decimal system, which would be denoted as
1010[10]. Similarly, 1010 of the octal system, would be denoted as 1010[8], and 1010 of the Hex
system would be denoted as 1010[16].

Decimal Number System


The decimal system is the system which we use in everyday counting. The number system
includes the ten digits from 0 through 9. These digits are recognized as the symbols of the decimal
system. Each digit in a base ten number represents units ten times the units of the digit to its right.
For example:
9542= 9000 + 500 + 40 +2= (9 103) + (5 102) + (4 101) + (2 100)

Binary Number System


Computers do not use the decimal system for counting
and arithmetic. Their CPU and memory are made up of
=1
=0
millions of tiny switches that can be either in ON and OFF
states. 0 represents OFF and 1 represents ON. In this way
we use binary system. A simple electronic switch can
represent both computer numbers.
The numbers in binary system are represented as combinations of these two symbols.
The decimal system uses power of 10 and binary system uses powers of 2. The binary digit is also
referred to as Bit (the acronym for Binary Digit). A string of 4-bits is called a nibble and a string of 8bits is called a byte. A byte is the basic unit of data in computers. The number 125 actually means 1*
102 + 2* 101 +5*100. In binary system, the same number (125) is represented as 1111101 meaning
1*26 +1* 25 +1* 24 +1*23 +1* 22 +0* 21 +1*20.
Computers perform operations on binary number groups called words. Today, most computers
use 32-, 64-, or 128-bit words.

Octal Number System


The octal system is commonly used with computers. The octal number system with its 8 digit
0,1,2,3,4,5,6, and 7 has base 8. The octal system uses a power of 8 to determine the digit of a
numbers position.

Hexadecimal Number System


Hexadecimal is another number system that works exactly like the decimal, binary and octal
number systems, except that the base is 16. Each hexadecimal represents a power of 16. The
system uses 0 to 9 numbers and A to F characters to represent 10 to 15 respectively.
Conversions. Any number in one number system can be converted into any other number
system. There are the various methods that are used in converting numbers from one base to another.

42

Computer Fundamentals & Programming in C

2.2 CONVERSIONS
Conversions of Decimal to Binary
The method that is used for converting of decimals into binary is known as the remainder
method. We use the following steps in getting the binary number :

(a) Divide the decimal number by 2.


(b) Write the remainder (which is either 0 or 1) at the right most position.
(c) Repeat the process of dividing by 2 until the quotient is 0 and keep writing the remainder
after each step of division.
(d) Write the remainders in reverse order.
EXAMPLEConvert (45)10 into binary number system.

45

22

Remainder
1

11

Thus, (45)10 = (101101)2


Note : In every number system
(a) The first bit from the right is referred as LSB (Least Significant Bit)
(b) The first bit from the left is referred as MSB (Most Significant Bit).

Conversions of Decimal Fractions to Binary Fractions


For converting decimal fractions into binary fractions, we use multiplication. Instead of looking
for a remainder we look for an integer. The following steps are used in getting the binary fractions :
(a) Multiply the decimal fraction by 2.
(b) If a non-zero integer is generated, record the non-zero integer otherwise record 0.
(c) Remove the non-zero integer and repeat the above steps till the fraction value becomes 0.
(d) Write down the number according to the occurrence.
EXAMPLEFind the binary equivalent of (0.75)
0.75 2 = 1.50
0.50 2 = 1.00

10

Number (to be recorded)


1
1

Thus, (0.75)10 = (0.11)2.


Moreover, we can write (45.75)10 = (101101.11)2.
Remark : If the conversion is not ended and still continuing; we write the approximation in 16 bits.

43

Binary Number System

EXAMPLEFind the binary equivalent of (0.9)10 .


0.9
0.8
0.6
0.2
0.4
0.8
0.6
0.2
0.4
0.8
0.6
0.2
0.4
0.8
0.6
0.2
0.4
0.8
Thus,

2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2

=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=

1.8
1.6
1.2
0.4
0.8
1.6
1.2
0.4
0.8
1.6
1.2
0.4
0.8
1.6
1.2
0.4
0.8
1.6

Number (to be recorded)


1
1
1
0
0
1
1
0
0
1
1
0
0
1
1
0
0
1

(0.9)10 = (0.111001100110011001)2 .

Conversion of Decimal to Octal


In converting decimal to octal, we follow the same process of converting decimal to binary.
Instead of dividing the number by 2, we divide the number by 8.
EXAMPLEConvert (45)10 into octal number system.

Thus,

45

Remainder
5

(45)

10

= (55) .
8

Conversions of Decimal Fractions to Octal Fractions


We follow the same steps of conversions of decimal fractions to binary fractions. Here, we
multiply the fraction by 8 instead of 2.
EXAMPLEFind the octal equivalent of (0.75)10.
Number (to be recorded)
0.75 8 = 6.00
Thus, (0.75)10 = (0.6)8.
And (45.75)10 = (55.6)8.

44

Computer Fundamentals & Programming in C

Conversion of Decimal to Hexadecimal


We divide by 16 instead of 2 or 8. If the remainder is in between 10 to 16, then the number
is represented by A to F respectively.
EXAMPLEConvert (45)10 into hexadecimal.

16

45

16

Remainder
D

16

Thus (45)10 = (2D)16.


Conversions of Decimal Fractions to Hexadecimal Fractions
Here, we multiply the fraction by 16 instead of 2 or 8. If the non-zero integer is in between 10
to 16, then the number is represented by A to F respectively.
EXAMPLEFind the hexadecimal equivalent of (0.75)10.
Number (to be recorded)
0.75 16 = 12.00

C (12 = C)

Thus, (0.75)10 = (0.C)16.


And (45.75)10 = (2D.C)16.
Conversions of Binary to Decimal
In converting binary to decimal, we use the following steps :
(a) Write the weight of each bit.
(b) Get the weighted value by multiplying the weighted position with the respective bit.
(c) Add all the weighted value to get the decimal number.
EXAMPLEConvert (101101)2 into decimal number system.
Binary Number

Wt. of each bit

25

24

23

22

21

20

Weighted Value

1 25

0 24

1 23

1 22

02

1 20

Solved Multiplication

32

Thus,

(101101) 2 = 1 25+ 0 24 + 1 23 + 1 22 + 0 21 + 1 20.


= 32

= 45
Conversions of Binary Fractions to Decimal Fractions
The conversions of binary fractions to the decimal fractions is similar to conversion of binary
numbers to decimal numbers. Here, instead of a decimal point we have a binary point. The exponential
expressions (or weight of the bits) of each fractional placeholder is 2-1, 2-2

45

Binary Number System

EXAMPLEConvert (101101.11)2 into decimal number system.


Binary Number

Wt. of each bit

25

24

23

22

21

20

21

22

Weighted Value

1 25

0 24

1 23

1 22

02

1 20

1 21 1 22

Solved Multiplication

32

0.5

Thus,

0.25

(101101.11)2 = 32 + 0 + 8 + 4 + 0 + 1 + 0.5 + 0.25 = 45.75

Conversions of Binary to Octal


We use the following steps in converting binary to octal:
(a) Break the number into 3-bit sections starting from LSB to MSB.
(b) If we do not have sufficient bits in grouping of 3-bits, we add zeros to the left of MSB so
that all the groups have proper 3-bit number.
(c) Write the 3-bit binary number to its octal equivalent.
EXAMPLEConvert (101101)

into octal.

Binary Number

101

101

Octal Number

Thus, (101101)2 = (55)8 .


EXAMPLEConvert (1101101)2 into octal.
Binary Number

001

101

101

Octal Number

Thus (1101101)2 = (155)8.


Conversions of Binary Fractions to Octal Fractions
We use the following steps in converting binary fractions to octal fractions:
(a) Break the fraction into 3-bit sections starting from MSB to LSB.
(b) In order to get a complete grouping of 3 bits, we add trailing zeros in LSB.
(c) Write the 3-bit binary number to its octal equivalent.
EXAMPLEConvert (101101.11)2 into octal.
Binary Number

101

101

110

Octal Number

Thus, (101101)2 = (55.6)8.


Conversions of Binary to Hexadecimal
We convert binary to hexadecimal in the similar manner as we have converted binary to octal.
The only difference is that here, we form the group of 4 bits.

46

Computer Fundamentals & Programming in C

EXAMPLEConvert (101101)2 into hexadecimal.


Binary Number

0010

1101

Decimal Number

13

Hexadecimal Number

Thus, (101101)2 = (2D)16.


Conversions of Binary Fractions to Hexadecimal Fractions
We convert binary fractions to hexadecimal fractions in the similar manner as we have converted
binary fractions to octal fractions. The only difference is that here we form the group of 4 bits.
EXAMPLEConvert (101101.11)

into hexadecimal.

Binary Number

0010

1101

1100

Decimal Number

13

12

Hexadecimal Number

Thus, (101101.11)2 = (2D.C)16.


Conversions of Octal to Decimal
We follow the same steps of conversion of binary to decimal. The only difference is that here
weight of nth bit is 8n-1 instead of 2n-1.
EXAMPLEConvert (55)8 into decimal number system.

Thus,

Octal Number

Wt. of each bit

81

80

Weighted Value

58

5 80

Solved Multiplication

40

(55)8 = 40 + 5 = 45

Conversions of Octal Fractions to Decimal Fractions


The weight of the bit of the fraction placeholder is 8-1, 8-2. We follow the same steps of
conversion of binary fractions to decimal fractions.
EXAMPLEConvert (55.6)8 into decimal number system.
Octal Number

Wt. of each bit

81

80

81

Weighted Value

58

5 80

6 81

Solved Multiplication

40

0.75

Thus, (55.6)8 = 40 + 5 + 0.75 = 45.75

47

Binary Number System

Conversions of Octal to Binary


We use the following steps in converting octal to binary:
(a) Convert each octal digit into 3-bit binary equivalent.
(b) Combine the 3-bit section by removing the spaces to get the binary number.
EXAMPLEConvert (55)8 into binary.
Octal Number

Binary Number

101

101

Thus, (55)8 = (101101)2.


EXAMPLEConvert (456)8 into binary.
Octal Number

Binary Number

100

101

110

Thus, (456)8 = (100101110)2.


Conversions of Octal Fractions to Binary Fractions
We follow the same steps of conversion of octal to binary.
ExampleConvert (55.6)8 into binary.

Thus,

Octal Number

Binary Number

101

101

110

(55.6)

= (101101.11)2.

Conversions of Octal to Hexadecimal


The conversion involves the following steps :
(a) Convert each octal digit to 3 bit binary form.
(b) Combine all the 3-bit binary numbers.
(c) Group them in 4-bit binary form by starting from MSB to LSB.
(d) Convert these 4-bit blocks into their hexadecimal symbols.
EXAMPLEConvert (55)8 into hexadecimal.
Octal Number

Binary Number

101

101

Combining the 3-bit binary block, we have 101101.


Grouping them in 4-bit binary form:
Binary Number

0010

1101

Hexadecimal Symbol

Thus, (55)8 = (2D)16.

48

Computer Fundamentals & Programming in C

Conversions of Octal Fractions to Hexadecimal Fractions


The method of conversion is based on the same procedure that we have discussed in conversions
of octal to hexadecimal.
EXAMPLEConvert (55.6)8 into hexadecimal.
Octal Number

Binary Number

101

101

110

Combining the 3-bit binary block, we have 101101.110.


Grouping them in 4-bit binary form:
Binary Number

0010

1101

1100

Hexadecimal Number

Thus, (55)8 = (2D.C)16.


Conversions of Hexadecimal to Decimal
We do the conversion of hexadecimal to decimal as we have done the conversion of binary to
decimal. Here weight of nth bit is 16n-1 instead of 2n-1.
EXAMPLEConvert (2D)16 into decimal.
Hexadecimal Number

D(=13)

Wt. of each bit

161

160

Weighted Value

2 16

13 160

Solved Multiplication

32

13

Thus, (2D)16 = 32 + 13 = 45.


Conversions of Hexadecimal Fractions to Decimal Fractions
We do the conversion of hexadecimal fractions to decimal fractions in the similar manner as we
have done the conversion of binary fractions to decimal fractions. Here weight of bit is
16-1, 16-2.
EXAMPLEConvert (2D.C)16 into decimal.
Hexadecimal Number

D(=13)

C(=12)

Wt. of each bit

161

160

161

Weighted Value

2 16

13 160

13 161

Solved Multiplication

32

13

0.75

Thus, (2D.C)16 = 32 + 13 + 0.75 = 45.75.


Conversions of Hexadecimal to Binary
We use the following steps:
(a) Convert each hexadecimal digit to its 4-bit binary equivalent.
(b) Combine all the binary numbers.

49

Binary Number System

EXAMPLEConvert (2D)16 into binary.


Hexadecimal Number

D(=13)

Binary Number

0010

1101

Thus, (2D) 16 = (00101101) 2 = (101101) 2.


Conversions of Hexadecimal Fractions to Binary Fractions
We use the same steps of hexadecimal to binary conversion.
EXAMPLEConvert (2D.C)16 into binary.
Hexadecimal Number

D(=13)

C(=12)

Binary Number

0010

1101

1100

Thus, (2D)16 = (00101101.1100)2 = (101101.11)2.


Conversions of Hexadecimal to Octal
We convert each hexadecimal digit in binary. Combine all the binary numbers. Again group
them into 3-bit form. Convert the 3-bit block in octal.
EXAMPLEConvert (2D)16 into octal.
Hexadecimal Number

D(=13)

Binary Number

0010

1101

Combining the binary number, we get 00101101=101101


Grouping the binary number in 3-bit.
Binary Number

101

101

Octal Number

Thus (2D)16 = (55)8.


Conversions of Hexadecimal Fractions to Octal Fractions
We follow the same steps of hexadecimal to octal conversion.
EXAMPLEConvert (2D.C)16 into octal.
Hexadecimal Number

D(=13)

C(=12)

Binary Number

0010

1101

1100

Combining the binary number, we get 00101101.1100=101101.11

50

Computer Fundamentals & Programming in C

Grouping the binary number in 3-bit


Binary Number

101

101

110

Octal Number

Thus (2D.C)16 = (55.6)8.


The diagram below shows all the conversion cases covered above.

[10]

[2]
[8]

[16]

This means that we can now handle any type of conversion, even conversions between the
number systems that are not directly connected on the above diagram. For example, to convert
[8 ] [ 10 ], we can convert [ 8 ] [ 2 ] followed by [ 2 ] [ 10 ]. Or, to convert [ 8 ] [ 16 ], we
can convert [ 8 ] [ 2 ] followed by [ 2 ] [ 16 ]. etc.

2.3 BINARY ADDITION AND SUBTRACTION


Binary addition is performed in the same manner as decimal addition. The complete table for
binary addition is as follows:
0+0=0
0+1=1
1+0=1
1 + 1 = 0 plus a carryover of 1
Carry over are performed in the same manner as in decimal arithmetic. Since 1 is the largest
digit in the binary system, any sum greater than 1 requires that a digit be carried.
Examples:

Decimal
5
6
11

Binary
101
110
1011

Decimal
3
5
9

Binary
11.01
101.11
1001.00

Subtraction is the inverse operation of addition. To subtract, it is necessary to establish a


procedure for subtracting a larger from a smaller digit. The only case in which this occurs with binary
numbers is when 1 is subtracted from 0. It is necessary to borrow 1 from the next column to the left.
This is the binary subtraction table.

51

Binary Number System

0
1
0
1

0=0
0=1
1 = 1 with a borrow of 1
1=0

Examples:

Decimal
9
5

Binary
1001
101

100

Decimal
16
3

Binary
10000
11

13

11111

2.4 BINARY MULTIPLICATION AND DIVISION


The table for binary multiplication is very short, with only four entries instead of the many for
normal decimal multiplication
0*0=0
0*1=0
1*0=0
1*1=1
The following examples of binary multiplication illustrate the simplicity of each operation. It is
only necessary to copy the multiplicand if the digit in the multiplier is 1 and to copy all 0s if the digit
in the multiplier is a 0.

111
101 *

10.1
10.1 *

111
000
111

101
000
101

100011

110.01

The complete table for binary division is as follows:


0/1=0
1/1=1
Examples:

111
110

101010
110
1001
110
110
110
0

111.1
110

101101
110
1010
110
1001
110
0110
110
0

52

Computer Fundamentals & Programming in C

2.5 COMPLEMENTS
Complements are quite often used to represent negative numbers in digital computers for
simplifying the subtraction operation and logical manipulation. For instance, the number N2 has to
be subtracted from N1 i.e., N1 - N2 then without using subtraction the complement form of
negative number is formed and then added. It can be pointed out that since the subtraction of
number N2 from N1 is same as the addition of N1 and complement of N2 (i.e.,) N1 + (N2). There are
two types of complements for each base 1 system.
1. The rs Complement
2. The (r-1)s Complement.
When the value of the base is substituted the two types receive the names 2s and 1s complement
for binary number or 10s and 9s complement for decimal numbers. The rs complement is
sometimes called as True Complement and the (r-1)s complement as Radix minus ones
complement.
The (r-1)s Complement
Given a positive number N in base r with an integer part of N digits and a fraction part of m
digits, then (r-1)s complements can be defined as rn rm N. The (r-1)s complement in decimal
system is 9s complement and 1s complement in case of binary. Some numerical examples of
9s complement is as follows:
EXAMPLE 1 : The 9s complement of 52510 is
(105 - 1) 52510
= 99999-52510
= 47489
The 9s complement of 0.3266 is
= (1-10-4 ) 0.3266
= 0.9999 0.3266
= 0.6733
The 9s complement of 25.638 is
= (102 10-3 ) 25.638
= 99.999-25.638
= 74.361
EXAMPLE 2 : The 1s complement of 101100 is
= (26 1) 101100
= (1000000 1) 101100
= 111111-101100
= 010011
The 1s complement of (0.0110)2 is
= (1 2-4 )10 0.0110
= 1-0.0001 0.0110
= 0.1111 0.0110
= 0.1001
The 1s complement of 10.101 is
= (22 2-3) 10 10.101 B
= (100-0.001) B 10.101 B
= 11.1111 10.101
= 1.010

Binary Number System

53

The rs Complement
Given a positive number N in base r with an integer part of n digits the rs complement of N is
defined as rn - N for N 0 and 0 for N=0. The rs complement in decimal system is 10s complement
and 2s complement in case of binary system.
EXAMPLE 1 : The 10s complement of (52510)10 is
= 105 52510
= 47490
The 10s complement of 0.3266 10 is
= 1 0.3266
= 0.6734
The 10s complement of 25.638 10 is
= 102 25.638
= 74.362
The 2s complement of 101100 is
= (26 ) 101100B
= (1000000 101100) B
= 010011
The 2s complement of (0.0110) is
= (1-0.0110) B
= 0.1010
The 2s complement of 10.101 is
= (22 ) D 10.101
= (100 10.101) D
= 01.011
The rs complement can be obtained from the (r 1)s complement after the addition of r to the
least significant digit.
From the examples, one can notice that:
q 9s complement of a decimal number is formed simply by subtracting every digit by 9.
q The 1s complement of a binary number is even simpler to form, the 1s are changed to
0s and 0s are changed to 1s.
For example, one can notice that
q 2s complement of 101101 is obtained from the 1s complement 010010 by adding 1 to
give 010011.
q 10s complement can be formed by forming 9s complement and then adding a 1 to the
least significant digit.

2.6 BINARY CODED DECIMAL (BCD)


The binary coded decimal (BCD) is a type of binary code used to represent a given decimal
number in an equivalent binary form. Its main advantage is that it allows easy conversion to decimal
digits for printing or display and faster calculations.
The most common BCD code is the 8421 BCD code. In this, the BCD equivalent of a decimal
number is written by replacing each decimal digit in integer and fractional parts with its four-bit binary
equivalent (or nibble). Here 8, 4, 2 and 1 represent the weights of different bits in the four-bit
groups, starting from the (MSB) most significant bit (to extreme left) and proceeding towards the least
significant (LSB) bit.

54

Computer Fundamentals & Programming in C

This feature makes it a weighted code, whose main characteristic is that each binary digit in the
four bit group representing a given decimal digit is assigned a weight, and for each group of four bits,
the sum of the weights of those binary digits whose value is 1 is equal to the decimal digit which they
represent.
For example, if we look at table, we find that the decimal digit 9 when represented in 8421
BCD is 1001. Now the decimal digit assigned to first 1 is 8 and to the second 1 is 1. If we add 8 and
1 we get the required decimal number which is 9.
BCD CODE CHART
Decimal

BCD Code

digital

0
1
2
3
4
5
6
7
8
8

0
0
0
0
0
0
0
0
1
1

0
0
0
0
1
1
1
1
0
0

0
0
1
1
0
0
1
1
0
0

0
1
0
1
0
1
0
1
0
1

0
0
0
0
0
0
0
0
1
1

0
0
0
0
1
1
1
1
0
0

0
0
1
1
0
0
1
1
0
0

0
1
0
1
0
1
0
1
0
1

0
0
0
0
0
0
0
0
1
1

0
0
0
0
1
1
1
1
0
0

0
0
1
1
0
0
1
1
0
0

0
1
0
1
0
1
0
1
0
1

The 4221 BCD and 5421 BCD are other weighted BCD codes shown in table. The numbers
4, 2, 2, 1 in 4221 BCD and 5, 4, 2 and 1 in 5421 BCD represent weights of the relevant bits.
Now, let us consider some examples, where we convert the given decimal numbers to BCD.
The 8421 BCD code for 9.2 is 1001.0010.
BCD for 9.2

9 .

1001 0010
The 4221 BCD code for 9.2 is 1111.0010.
The 5421 BCD code for 9.2 is 1100.0010.
BCD code is useful for outputting to displays that are always numeric (0 to 9), such as those
found in digital clocks or digital voltmeters.

2.7 OTHER CODES


(1) ASCII
ASCII stands for American Standard Code for Information Interchange. It is a 7-bit character
code that was introduced by the American National Standard Institute (ANSI) and is used by personal
and workstation computers.
ASCII characters are examples of packed BCD numbers. Value in ASCII codes are represented
as their 4-bit binary equivalent stored in lower nibble, while upper nibble contains 011 and has no
bearing on the value of the represented number. ASCII is the most common format for text files on
computer and on the internet. It can be defines 128 different characters because it is a7-bit code, and
support 27 combinations.

Binary Number System

55

(2) EBCDIC
Extended Binary Coded Decimal Interchange Code (EBCDIC) is eight bits, or one byte, wide
character encoding technique. A character is represented in EBCDIC by eight bit. EBCDIC mainly
used on IBM mainframe and IBM midrange computer operating systems. Each byte consists of two
nibbles, each four bits wide. The first four bits define the class of character, while the second nibble
defines the specific character inside that class.
EBCDIC is easier to use on punch cards. Although it is considered to be an obsolete coding
system today, it is still used in some equipment, to allow for continues used of software written many
years ago that requires EBCDIC communication environment.
EBCDIC characters are similar to ASCII characters. While the lower nibble contains the 4-bit
binary equivalent (as ASCII), the upper nibble on the other hand is padded with 1111, instead of
0111. EBCDIC can code up to 256 different characters.

(3) Excess-3 Code


Excess-3, also called XS3, is a non-weighted code used to express decimal number-s. It is
another important binary code. It is particularly significant for arithmetic operations as it overcomes
the shortcomings encountered while using the 8421 BCD code to add two decimal digits whose sum
exceeds 9. This code is used in some old computers.
The Excess-3 code for a given decimal number is determined by adding 3 to each decimal digit
in the given number and then replacing each digit of the newly found decimal number by its four bit
binary equivalent. The table gives is the Excess-3 code. For example, XS3 code of 24 is obtained as
2

+3

+3

0101 0111
Thus, XS3 code of 24 is 0101 0111.
The key feature of the Excess-3 code is .that it is self complementing. In other words, the ls
complement of an Excess- 3 number is the Excess- 3 code for the 9s complement of the corresponding
decimal number. For example, the Excess- 3 code for decimal 6 is 1001. The ls complement of
1001 is 0110, which is the Excess-3 code for decimal 3, and 3 is the 9s complement of 6. This
property of Excess-3 code makes it useful in some arithmetic operations.

(4) Gray code


The Gray code was designed by Frank Gray at Bell Labs in 1953. It belongs to a class of codes
called the minimum change code. The successive coded characters never differ in more than one-bit.
Owing to this feature, the maximum error that can creep into a system using the binary gray code to
encode data is much less than the worst - case error encountered in case of straight binary encoding.
The Gray code is an unweighted code. Because of this, the gray code is not suitable for arithmetic
operations but finds applications in input/output devices, some analog-to-digital converters and
designation of rows and columns in Karnaugh map etc.
One can easily remember the gray codes. A three-bit gray code can be obtained by merely
reflecting the two-bit code about an axis at the end of the code and assigning a third-bit as 0 above
the axis and as 1 below the axis. The reflected gray code is nothing but code written in reverse order.
By reflecting three-bit code, a four-bit code may be obtained.

56

Computer Fundamentals & Programming in C

0 0

0 0

0 1

0 0

1 1

0 1

1 0

0 1

1 1

1 1

1 0

1 0

Process of obtaining 3-bit gray code by reflecting 2-bit gray code

Now, let us consider a few examples. The four-bit gray code for decimal number 39 is 00101101.
Similarly, gray code for (923.1)10 and (327)10 is
(923.1)10 = (1101 0011 0010.0001) Gray code
(327)10 = (100011 0100)

Gray code

2.8 LOGIC GATES


Logic gates process signals which represent true or false. Normally, the positive supply voltage
+5V represent true and 0V represents false. Other terms which are used for the true and false states
are shown in the table. It is best to be familiar with them all.
Logic States
True

False

High

Low

On

Off

+5V

0V

Gates are identified by their function: AND, OR, NOT, NAND, NOR, EX-OR and EX-NOR.
Capital letters are normally used to make it clear that the term refers to a logic gate.
The above said logic gates can be classified into following categories:
1. Basic Logic Gates :

(a) AND Gate

(b) OR Gate

(c) NOT Gate


2. Universal Gates :
(a) NAND Gate

(b) NOR Gate

3. Combinational Gates :
(a) X-OR Gate

(b) X-NOR Gate

The basic operations are described below with the aid of truth tables.

57

Binary Number System

(1) BASIC LOGIC GATES


AND Gate
The AND gate is an electronic circuit that gives a high output (1) only if all its inputs are high.
A dot (.) is used to show the AND operation i.e., A.B. Bear in mind that this dot is sometimes
omitted i.e., AB.
2 Input AND gate
A
B

C
AND

A
0
0
1
1

B
0
1
0
1

A.B
0
0
0
1

OR Gate
The OR gate is an electronic circuit that gives a high output (1) if one or more of its inputs are
high. A plus (+) is used to show the OR operation.
2 Input OR gate
A
B

A+B
OR

A
0
0
1
1

A+B

B
0
1
0
1

0
1
1
1

NOT gate
The NOT gate is an electronic circuit that produces an inverted version of the input at its
output. It is also known as an inverter. If the input variable is A, the inverted output is known as
NOT A. This is also shown as A, or A with a bar over the top, as shown at the outputs.
NOT gate
A

A
NOT

0
1

1
0

(2) UNIVERSAL GATES


NAND Gate
This is a NOT-AND gate which is equal to an AND gate followed by a NOT gate. The outputs
of all NAND gates are high if any of the inputs are low. The symbol is an AND gate with a small circle
on the output. The small circle represents inversion.

58

Computer Fundamentals & Programming in C

2 Input AND gate


A
B

AB
NAND

A
0
0
1
1

B
0
1
0
1

A.B
1
1
1
0

NOR Gate
This is a NOT-OR gate which is equal to an OR gate followed by a NOT gate. The outputs of
all NOR gates are low if any of the inputs are high. The symbol is an OR gate with a small circle on
the output. The small circle represents inversion.
2 Input NOR gate
A
B

A+B
NOR

A
0
0
1
1

B
0
1
0
1

A+B
1
0
0
0

(3) COMBINATIONAL GATES


X-OR Gate
The Exclusive-OR gate is a circuit which will give a high output if either, but not both, of its two
inputs are high. An encircled plus sign () is used to show the X-OR operation.
2 Input EXOR gate
A
B

AB
EOR

A
0
0
1
1

B
0
1
0
1

AB
0
1
1
0

X-NOR Gate
The Exclusive-NOR gate circuit does the opposite to the X-OR gate. It will give a low output
if either, but not both, of its two inputs are high. The symbol is an X-OR gate with a small circle on
the output. The small circle represents inversion.
2 Input EXOR gate
A
B

AB
ENOR

A
0
0
1
1

B
0
1
0
1

AB
1
0
0
1

59

Binary Number System

TABLE 1: LOGIC GATE SYMBOLS

A
B

A
B

AB

A+B
OR

AND
A
B

A
B

AB

A+B

NAND

NOR
A
B

A B
EOR

A
NOT

A
B

AB
ENOR

Table 2 is a summary truth table of the input/output combinations for the NOT gate together
with all possible input/output combinations for the other gate functions.
TABLE 2: LOGIC GATES REPRESENTATION USING THE TRUTH TABLE
Inputs

Outputs

AND

NAND

OR

NOR

EXOR

EXNOR

NOT gate

KEY POINTS TO REMEMBER


t

t
t

Computers are electronic machine that operate using binary logic. These devices use two
different values to represent the two voltage level (0V for logic 0 and +5V for logic 1).
A string of 4-bits is called a nibble and a string of 8-bits is called a byte.
Computers perform operations on binary number groups called words. Today, most computers
use 32-, 64-, or 128-bit words.
The octal system is commonly used with computers. The octal number system with its 8 digit
0,1,2,3,4,5,6, and 7 has base 8.
Hexadecimal Number system represents a power of 16. The system uses 0 to 9 numbers
and A to F characters to represent 10 to 15 respectively.

60

Computer Fundamentals & Programming in C


t

To convert a decimal number into its binary/octal/hexadecimal equivalent. Simply divide the
decimal number by 2/8/16 and write down the remainder.
To convert a binary/octal/hexadecimal into its decimal equivalent, multiply the place holder
amount by the digit, and then add all the products.
In converting decimal to binary, we divide the decimal number by 2 until the quotient is 0
and, Write the remainders in reverse order.
For converting decimal fractions into binary fractions, we use multiplication. Instead of looking
for a remainder we look for an integer.
In converting decimal to octal, we follow the same process of converting decimal to binary.
Instead of dividing the number by 2, we divide the number by 8.
We divide by 16 instead of 2 or 8. If the remainder is in between 10 to 16, then the number
is represented by A to F respectively.
Complements are quite often used to represent negative numbers in digital computers for
simplifying the subtraction operation and logical manipulation.
The (r-1)s complement in decimal system is 9s complement and 1s complement in case of
binary.
The rs complement in decimal system is 10s complement and 2s complement in case of
binary system.

SHORT ANSWER TYPE QUESTIONS


1. What is Number System?

Ans. A numbering system assigns meaning to the position of the numeric symbols. For example,

2.
Ans.

3.

4.
Ans.

consider this set of symbols: 642


It depends on the numbering system.
642 is 600 + 40 + 2 in BASE 10
The base of a number determines the number of digits (e.g., symbols) and the value of digit
positions.
What is Binary Number System?
Digital computers are made up of electronic circuits, which have exactly 2 states: on and off.
Computers use a numbering system which has exactly 2 symbols, representing on and off.
Decimal is base 10 and has 10 digits: 0,1,2,3,4,5,6,7,8,9
Binary is base 2 and has 2, so we use only 2 symbols: 0 and 1
What is the decimal equivalent of the binary number 01101110?
0 x 27 = 0 x 128 = 0
+ 1 x 2 6 = 1 x 64 = 64
+ 1 x 2 5 = 1 x 32 = 32
+ 0 x 2 4 = 0 x 16 = 0
+1x23 = 1x8 = 8
+1x22 = 1x4 = 4
+1x2 1 = 1x2 = 2
+0x20 = 0x1 = 0
= 110 (decimal)
What is the binary equivalent of the decimal number 103?
103 / 2 = 51, remainder 1 -> rightmost bit
51 / 2 = 25, remainder 1
25 / 2 = 12, remainder 1

Binary Number System

5.
Ans.

6.
Ans.

7.
Ans.

8.

61

12 / 2 = 6, remainder 0
6 / 2 = 3, remainder 0
3 / 2 = 1, remainder 1
1 / 2 = 0, remainder 1-> leftmost bit
(103) = (1 1 0 0 1 1 1)
10
2
What is a Computer Word?
A computer word is a group of bits which are passed around together during computation. The
word length of the computers processor is how many bits are grouped together.
t 8-bit machine (e.g., Nintendo Gameboy, 1989)
t 16-bit machine (e.g., Sega Genesis, 1989)
t 32-bit machines (e.g., Sony PlayStation, 1994)
t 64-bit machines (e.g., Nintendo 64, 1996)
Convert (94D3) into 4 bits of binary code and then Convert binary to decimal.
16
9
4
D
3
= 1001 0100 1101 0011
= 215 + 212 + 210 + 27 + 26 + 24 + 21 + 20
= 32768 + 4096 + 1024 + 128 + 64 + 16 + 2 + 1
= 38099 (decimal)
What is 2s Compliment in binary number system?
Twos complement representation is used because it reduces the complexity of the hardware in
the arithmetic-logic unit of a computers CPU. Using a twos complement representation, all of
the arithmetic operations can be performed by the same hardware whether the numbers are
considered to be unsigned or signed.
Find the 2s complement of the following 8 bit number 00101001.
11010110 First, invert the bits
+ 00000001 Then, add 1
= 11010111
The 2s complement of 00101001 is 11010111

MULTIPLE CHOICE QUESTIONS


1. Binary circuit elements have
(a) One stable state
(b) Two stable state
(c) Three stable state
(d) None of above
2. In the decimal numbering system, what is the MSD?
(a) The middle digit of a stream of numbers (b) The digit to the right of the decimal point
(c) The last digit on the right
(d) The digit with the most weight
3. The octal equivalent of 111010 is
(a) 81
(b) 72
(c) 71
(d) None of above
4. Convert hexadecimal value 16 to decimal.
(a) 2210
(b) 1610
(c) 1010
(d) 2010
5. Which number system is commonly used as a shortcut notation for groups of four binary
digits?
(a) Binary
(b) Decimal
(c) Octal
(d) Hexadecimal

62

Computer Fundamentals & Programming in C

6. 1 nibble equals to
(a) 1 bits
(b) 2 bits
(c) 4 bits
(d) 8 bits
7. Convert binary 111111110010 to hexadecimal.
(a) EE2
(b) FF2
16
16
(c) 2FE
(d) FD2
16
16
8. Which number system is usually followed in a typical 32-bit computer?
(a) Binary
(b) Decimal
(c) Hexadecimal
(d) Octal
9. In which numbering system can the binary number 1011011111000101 be easily converted
to?
(a) Decimal system
(b) Hexadecimal system
(c) Octal system
(d) No need to convert
10. ASCII stands for
(a) American Standard Code for Information Interchange
(b) American Scientific Code for International Interchange
(c) American Standard Code for Intelligence Interchange
(d) American Scientific Code for Information Interchange

ANSWERS
1. (b)

2. (d)

3. (b)

4. (a)

5. (d)

6. (c)

7. (b)

8. (a)

9. (b)

10. (a)

REVIEW QUESTIONS
1.
2.
3.
4.
5.

6.

7.
8.
9.

10.

What is number system?


Why do computer understand only binary language?
Convert: (234)10 (?)2, (?)8, (?)16
Convert the following numbers into the decimal number system :
(a) (1011 0101)2
(b) (5674)8
(c) (A1E2)16
Evaluate:
(ABC)16 ( )2
(766)8 ( )10
(512)10 ( )8
(A9)16 ( )10
(356)10 ( )16
W. A. P. in C Language to convert decimal number into binary number.
Convert 1001 0010 1110 into hexadecimal number system.
Subtract:
(a) 10101011 01111
(b) 11100001 11110111
(c) 1010101010 111101
Add:
(a) 10101011 + 0111111
(b) 11100001 + 1111110111
(c) 1010101010 + 111101

_________

Vous aimerez peut-être aussi