C programming
C programming
Book of Study:
1. Floyd and Jain- Digital Fundamentals, Eighth Edition,
Pearson Education
Reference:
1. A P Malvino and D P Leach - Digital Principles and
Applications, Fourth edition, Tata McGraw Hill Publishers,
co Ltd.
Module I
Similarly, the part of the number that lies to the right of the
decimal point is known as fractional part.
Example
Consider the decimal number 1358.246. Integer part of this
number is 1358 and fractional part of this number is 0.246.
The digits 8, 5, 3 and 1 have weights of 100, 101, 102 and 103
respectively.
After simplifying the right hand side terms, we will get the
decimal number, which is on left hand side.
The part of the number, which lies to the left of the binary point
is known as integer part. Similarly, the part of the number,
which lies to the right of the binary point is known as fractional
part.
Example
Consider the binary number 1101.011. Integer part of this
number is 1101 and fractional part of this number is 0.011. The
digits 1, 0, 1 and 1 of integer part have weights of 20, 21, 22, 23
respectively. Similarly, the digits 0, 1 and 1 of fractional part
have weights of 2-1, 2-2, 2-3 respectively.
After simplifying the right hand side terms, we will get a decimal
number, which is an equivalent of binary number on left hand
side.
Example
Consider the octal number 1457.236. Integer part of this
number is 1457 and fractional part of this number is 0.236. The
digits 7, 5, 4 and 1 have weights of 80, 81, 82 and 83 respectively.
Similarly, the digits 2, 3 and 6 have weights of 8-1, 8-2, 8-3
respectively.
After simplifying the right hand side terms, we will get a decimal
number, which is an equivalent of octal number on left hand
side.
The part of the number, which lies to the left of the hexadecimal
point is known as integer part. Similarly, the part of the
number, which lies to the right of the Hexa-decimal point is
known as fractional part.
Example
Consider the Hexa-decimal number 1A05.2C4. Integer part of
this number is 1A05 and fractional part of this number is 0.2C4.
The digits 5, 0, A and 1 have weights of 160, 161, 162 and 163
respectively. Similarly, the digits 2, C and 4 have weights of
16-1, 16-2 and 16-3 respectively.
Base Conversions
We have seen the four prominent number systems. Let us
convert the numbers from one number system to the other in
order to find the equivalent value.
Example
Consider the decimal number 58.25. Here, the integer part is
58 and fractional part is 0.25.
⇒ (58.25)10 = (111010.01)2
Example
Consider the decimal number 58.25. Here, the integer part is
58 and fractional part is 0.25.
⇒(58)10 = (72)8
Example
Consider the decimal number 58.25. Here, the integer part is
58 and decimal part is 0.25.
⇒ (58)10 = (3A)16
⇒(.25)10 = (.4)6
⇒ (58.25)10 = (3A.4)16
Example
Consider the binary number 1101.11.
Follow these two steps for converting a binary number into its
equivalent octal number.
Start from the binary point and make the groups of 3 bits on
both sides of binary point. If one or two bits are less while
making the group of 3 bits, then include required number of
zeros on extreme sides.
Write the octal digits corresponding to each group of 3 bits.
Example
Consider the binary number 101110.01101.
Example
Consider the binary number 101110.01101
Example
Consider the octal number 145.23.
Example
Consider the octal number 145.23.
Represent each octal digit with 3 bits.
(145.23)8 = (001 100 101.010 011)2
Example
Consider the octal number 145.23
In previous example, we got the binary equivalent of octal
number 145.23 as 1100101.010011.
Example
Consider the Hexa-decimal number 1A5.2
Example
Consider the Hexa-decimal number 65.4C
Represent each Hexa-decimal digit with 4 bits.
(65.4C)6 = (0110 0101.0100 1100)2
Example
Consider the Hexa-decimal number 65.4C
Unsigned Numbers
Unsigned numbers contain only magnitude of the number. They
don’t have any sign. That means all unsigned binary numbers
are positive. As in decimal number system, the placing of
positive sign in front of the number is optional for representing
positive numbers. Therefore, all positive numbers including
zero can be treated as unsigned numbers if positive sign is not
assigned in front of the number.
Signed Numbers
Signed numbers contain both sign and magnitude of the
number. Generally, the sign is placed in front of number. So, we
have to consider the positive sign for positive numbers and
negative sign for negative numbers. Therefore, all numbers can
be treated as signed numbers if the corresponding sign is
assigned in front of the number.
Example
Consider the decimal number 108. The binary equivalent of
this number is 1101100. This is the representation of unsigned
binary number.
Eugin Lopez. F, Asst. Prof in Computer, CAS, IHRD, Thodupuzha [email protected]
- 18 -
CS1CMT01 Fundamentals of Digital Systems (Complementary) MG University, Kottayam
(108)10 = (1101100)2
It is having 7 bits. These 7 bits represent the magnitude of the
number 108.
If the signed binary number contains ‘N’ bits, then (N-1) bits
only represent the magnitude of the number since one bit (MSB)
is reserved for representing sign of the number.
Example
Consider the positive decimal number +108. The binary
equivalent of magnitude of this number is 1101100. These 7 bits
represent the magnitude of the number 108. Since it is positive
number, consider the sign bit as zero, which is placed on left
most side of magnitude.
(+108)10 = (01101100)2
Sign-Magnitude form
In sign-magnitude form, the MSB is used for representing sign
of the number and the remaining bits represent the magnitude
of the number. So, just include sign bit at the left most side of
unsigned binary number. This representation is similar to the
signed decimal numbers representation.
Example
Consider the negative decimal number -108. The magnitude
of this number is 108. We know the unsigned binary
representation of 108 is 1101100. It is having 7 bits. All these
bits represent the magnitude.
Since the given number is negative, consider the sign bit as one,
which is placed on left most side of magnitude.
(−108)10 = (11101100)2
Example
Consider the negative decimal number -108. The magnitude
of this number is 108. We know the signed binary
representation of 108 is 01101100.
Example
Consider the negative decimal number -108.
We know the 1’s complement of (108)10 is (10010011)2
2’s compliment of (108)10 = 1’s compliment of (108)10 + 1.
= 10010011 + 1
= 10010100
Binary Arithmetic
Let us discuss about the basic arithmetic operations, which can
be performed on any two signed binary numbers using 2’s
complement method. The basic arithmetic operations are
addition and subtraction.
Example 1
Let us perform the addition of two decimal numbers +7 and +4
using 2’s complement method.
The resultant sum contains 5 bits. So, there is no carry out from
sign bit. The sign bit ‘0’ indicates that the resultant sum is
positive. So, the magnitude of sum is 11 in decimal number
system. Therefore, addition of two positive numbers will give
another positive number.
Example 2
Let us perform the addition of two decimal numbers -7 and -4
using 2’s complement method.
The sign bit ‘1’ indicates that the resultant sum is negative. So,
by taking 2’s complement of it we will get the magnitude of
resultant sum as 11 in decimal number system. Therefore,
addition of two negative numbers will give another negative
number.
Example 3
Let us perform the subtraction of two decimal numbers +7 and
+4 using 2’s complement method.
Here, the carry obtained from sign bit. So, we can remove it. The
resultant sum after removing carry is
(+7)10 + (+4)10 = (00011)2
The sign bit ‘0’ indicates that the resultant sum is positive. So,
the magnitude of it is 3 in decimal number system. Therefore,
subtraction of two decimal numbers +7 and +4 is +3.
Example 4
Let us perform the subtraction of two decimal numbers +4 and
+7 using 2’s complement method.
Here, carry is not obtained from sign bit. The sign bit ‘1’
indicates that the resultant sum is negative. So, by taking 2’s
complement of it we will get the magnitude of resultant sum as
3 in decimal number system. Therefore, subtraction of two
decimal numbers +4 and +7 is -3.
Codes
In the coding, when numbers or letters are represented by a
specific group of symbols, it is said to be that number or letter is
being encoded. The group of symbols is called as code. The
digital data is represented, stored and transmitted as group of
bits. This group of bits is also called as binary code.
Binary codes can be classified into two types.
Weighted codes
Unweighted codes
8-4-2-1 code
The weights of this code are 8, 4, 2 and 1.
This code has all positive weights. So, it is a positively
weighted code.
This code is also called as natural BCD (Binary Coded
Decimal) code.
Example
Let us find the BCD equivalent of the decimal number 786. This
number has 3 decimal digits 7, 8 and 6. From the table, we can
write the BCD (8421) codes of 7, 8 and 6 are 0111, 1000 and
0110 respectively.
∴ (786)10 = (011110000110)BCD
2-4-2-1 code
The weights of this code are 2, 4, 2 and 1.
This code has all positive weights. So, it is a positively
weighted code.
It is an unnatural BCD code. Sum of weights of unnatural
BCD codes is equal to 9.
It is a self-complementing code. Self-complementing codes
provide the 9’s complement of a decimal number, just by
interchanging 1’s and 0’s in its equivalent 2421
representation.
Example
Let us find the 2421 equivalent of the decimal number 786. This
number has 3 decimal digits 7, 8 and 6. From the table, we can
write the 2421 codes of 7, 8 and 6 are 1101, 1110 and 1100
respectively.
8 4 -2 -1 code
The weights of this code are 8, 4, -2 and -1.
This code has negative weights along with positive
weights. So, it is a negatively weighted code.
It is an unnatural BCD code.
It is a self-complementing code.
Example
Let us find the 8 4-2-1 equivalent of the decimal number 786.
This number has 3 decimal digits 7, 8 and 6. From the table, we
can write the 8 4 -2 -1 codes of 7, 8 and 6 are 1001, 1000 and
1010 respectively.
Excess 3 code
This code doesn’t have any weights. So, it is an un-weighted
code.
We will get the Excess 3 code of a decimal number by adding
three (0011) to the binary equivalent of that decimal
number. Hence, it is called as Excess 3 code.
It is a self-complementing code.
Example
Let us find the Excess 3 equivalent of the decimal number 786.
This number has 3 decimal digits 7, 8 and 6. From the table, we
can write the Excess 3 codes of 7, 8 and 6 are 1010, 1011 and
1001 respectively.
Therefore, the Excess 3 equivalent of the decimal number 786 is
101010111001
Gray Code
The following table shows the 4-bit Gray codes corresponding
to each 4-bit binary code.
Decimal Binary Gray
Number Code Code
0 0000 0000
1 0001 0001
2 0010 0011
3 0011 0010
4 0100 0110
5 0101 0111
6 0110 0101
7 0111 0100
8 1000 1100
9 1001 1101
10 1010 1111
11 1011 1110
12 1100 1010
13 1101 1011
14 1110 1001
15 1111 1000
This code doesn’t have any weights. So, it is an un-weighted
code.
In the above table, the successive Gray codes are differed in
one bit position only. Hence, this code is called as unit
distance code.
Example
From the table, we know that the Gray code corresponding to
binary code 1000 is 1100. Now, let us verify it by using the above
procedure.
Step 1 − By placing zero to the left of MSB, the binary code will
be 01000.
Parity Code
It is easy to include (append) one parity bit either to the left of
MSB or to the right of LSB of original bit stream. There are two
types of parity codes, namely even parity code and odd parity
code based on the type of parity being chosen.
Here, the number of bits present in the odd parity codes is 4. So,
the possible odd number of ones in these odd parity codes are 1
& 3.
If the other system receives one of these odd parity
codes, then there is no error in the received data. The bits
other than odd parity bit are same as that of binary code.
If the other system receives other than odd parity codes,
then there is an error(s) in the received data. In this case,
we can’t predict the original binary code because we
don’t know the bit position(s) of error.
Alphanumeric codes
Alphanumeric codes are sometimes called character codes
due to their certain properties. Now these codes are basically
binary codes. We can write alphanumeric data, including data,
letters of the alphabet, numbers, mathematical symbols and
punctuation marks by this code which can be easily
understandable and can be processed by the computers. Input
output devices such as keyboards, monitors, mouse can be
interfaced using these codes. 12-bit Hollerith code is the better
known and perhaps the first effective code in the days of
evolving computers in early days. During this period punch
cards were used as the inputting and outputting data. But
nowadays these codes are termed obsolete as many other
modern codes have evolved. The most common alphanumeric
codes used these days are ASCII code, EBCDIC code and
Unicode. Now we will discuss about them briefly.
ASCII code
The full form of ASCII code is American Standard Code for
Information Interchange. It is a seven bit code based on the
English alphabet.
In 1967 this code was first published and since then it is being
modified and updated. ASCII code has 128 characters some of
which are enlisted below to get familiar with the code.
DEC OCT HEX BIN Symbol Description
0 000 00 00000000 NUL Null char
1 001 01 00000001 SOH Start of Heading
2 002 02 00000010 STX Start of Text
3 003 03 00000011 ETX End of Text
4 004 04 00000100 EOT End of Transmission
5 005 05 00000101 ENQ Enquiry
6 006 06 00000110 ACK Acknowledgment
7 007 07 00000111 BEL Bell
Eugin Lopez. F, Asst. Prof in Computer, CAS, IHRD, Thodupuzha [email protected]
- 32 -
CS1CMT01 Fundamentals of Digital Systems (Complementary) MG University, Kottayam
EBCDIC code
The EBCDIC stands for Extended Binary Coded Decimal
Interchange Code. IBM invented this code to extend the Binary
Coded Decimal which existed at that time. All the IBM
computers and peripherals use this code. It is an 8 bit code and
therefore can accommodate 256 characters. Below is given
some characters of EBCDIC code to get familiar with it.
Char EBCDIC Char EBCDIC Char EBCDIC
A 1100 0001 P 1101 0111 4 1111 0100
B 1100 0010 Q 1101 1000 5 1111 0101
C 1100 0011 R 1101 1001 6 1111 0110
D 1100 0100 S 1110 0010 7 1111 0111
E 1100 0101 T 1110 0011 8 1111 1000
F 1100 0110 U 1110 0100 9 1111 1001
G 1100 0111 V 1110 0101 blank ...
H 1100 1000 W 1110 0110 . ...
I 1100 1001 X 1110 0111 ( ...
J 1101 0001 Y 1110 1000 + ...
K 1101 0010 Z 1110 1001 $ ...
L 1101 0011 0 1111 0000 * ...
M 1101 0100 1 1111 0001 ) ...
N 1101 0101 2 1111 0010 - ...
O 1101 0110 3 1111 0011 /
UNICODE
Unicode is the newest concept in digital coding. In Unicode
every number has a unique character. Leading technological
giants have adopted this code for its uniqueness.
Eugin Lopez. F, Asst. Prof in Computer, CAS, IHRD, Thodupuzha [email protected]