Download presentation
Presentation is loading. Please wait.
1
Computer Fundamentals and Programming in C
Chapter 4 DATA REPRESENTATION Computer Fundamentals and Programming in C
2
Computer Fundamentals and Programming in C
Introduction The computer hardware is a digital machine that can work at an extremely fast rate. The main component of a digital device is a transistor. The transistor is a bi-stable device, i.e., it can operate only in two states: fully conducting and non-conducting states. Therefore, it is used as a switch. The switch is on or off when it is in the fully conducting and non-conducting states respectively. Since the basic component of a computer is a two-state device, the data is accordingly represented in a computer by way of the presence or absence of electronic signals. This is called a binary or two-state representation of data in terms of two numbers, 0 and 1. Consequently, the number system is called a binary number system Computer Fundamentals and Programming in C
3
Computer Fundamentals and Programming in C
Decimal Number System In our day-to-day life, we work with the decimal number system having 10 digit values 0 through 9 as ten symbols. Therefore, it is a base-10 counting number system. A digit of a number written in the decimal system derives its meaning from its following two values: Symbol value: It is the digit’s value ranging from 0 to 9. Positional value: It is determined by the position of the digit in the number, counting from the right hand side to the left, i.e., units (100 = 1), tens (101 = 10), hundreds (102 = 100), thousands (103 = 1000) etc. For example, the decimal number 108 derives its meanings in the manner shown in Table Computer Fundamentals and Programming in C
4
Computer Fundamentals and Programming in C
Binary Number System As the name suggests, this number system has only two symbols: 0 and 1. Therefore, it is a base-2 system. The binary symbols ‘0’ and ‘1’ are also called binary nits or simply bits. A group of 8 bits is called a byte. A group of four bits is called a nibble. In this number system, the various bits of a number assume the symbol and positional value in a manner similar to decimal system. Computer Fundamentals and Programming in C
5
Conversion of a Binary Number into a Decimal Number
It may be noted that in the binary number system, the positional value signifies units(20 = 1), twos(21 = 2), fours(22 = 4), eights(23 = 8), sixteens (24 = 16) etc. Computer Fundamentals and Programming in C
6
Computer Fundamentals and Programming in C
Conversion of a Decimal Number into a Binary Number A decimal number can be converted into a binary number by the following methods: Double-Dabble method: The steps for the double-dabble method are: Divide the decimal number by 2. Write the dividend under the number. This becomes the new number. Write the remainder at the right in a column. Repeat steps 1 to 3 until a ‘0’ is produced as the new number. Write the 1s and 0s of the remainder in the bottom-up order, which forms the required number. Computer Fundamentals and Programming in C
7
Example 1. Convert decimal 19 to its equivalent binary number.
The equivalent binary number of 19 is: (Read bottom-up from the column of remainders) Computer Fundamentals and Programming in C
8
Computer Fundamentals and Programming in C
Direct Method In this method, we make use of the positional values of the binary number system. The method is as follows: Write the positional values of the binary system as shown below: … … Now compare the decimal number with the positional values listed above. The decimal number (say 19) lies between 16 and 32. So 16 becomes the bound and therefore place 1 at a position below 16 as shown below: … Subtract the positional value from the decimal number (19−16 =3). Now the result (3) becomes the new number. Repeat steps 2 and 3 till the number becomes 0, as shown below: Computer Fundamentals and Programming in C
9
Computer Fundamentals and Programming in C
Place 0s at the rest of the positional values as shown below: The sequence of binary digits ( ) so obtained is the required binary number. Computer Fundamentals and Programming in C
10
Computer Fundamentals and Programming in C
Example 2. Convert decimal 73 to its equivalent binary number by the direct method. Solution: The various steps are given below: Computer Fundamentals and Programming in C
11
Computer Fundamentals and Programming in C
Conversion of a Fractional Decimal Number into a Fractional Binary Number A fractional decimal number can be converted into its equivalent binary fractional number by the following steps: Multiply the decimal fraction by 2. The resultant number contains an integer part and a fraction part. Write the integer part in a column. The fraction part becomes the new number. Repeat steps 1 and 3 until the fractional part becomes zero or the desired number of binary places are obtained. The 1s and 0s written in the column constitute the required fractional binary number. Computer Fundamentals and Programming in C
12
Computer Fundamentals and Programming in C
Example 4. Convert the decimal into its equivalent fractional binary number. Solution: The various steps in the conversion are given in Table . The fractional binary number is read from the column of the integer part starting from top to down. The equivalent binary number obtained from the table 4.5 is given below: (0.625)10 = (0.101)2 Fractional decimal number Operation (Multiply by ) Resultant number Fractional part Integer part 0.625 0.625 * 2 1.250 .250 1 0.250 0.250 * 2 0.500 .500 0.500 * 2 1.000 Computer Fundamentals and Programming in C
13
Computer Fundamentals and Programming in C
Conversion of a Binary Number into a Decimal Number A binary number can be converted into its equivalent decimal number by the following methods. Direct method: The steps followed in this method are given below: Write the binary digits as powers of their respective positional value from the right. Convert each power of two into its decimal equivalent form. Multiply each decimal equivalent value by the binary digit in that position Add these terms to give the decimal number. Computer Fundamentals and Programming in C
14
Computer Fundamentals and Programming in C
For example, the conversion of binary number goes through the steps shown below: The equivalent decimal number obtained by the direct method is given below: ( )2 = (229)10 Computer Fundamentals and Programming in C
15
Computer Fundamentals and Programming in C
Double Dabble Method: The steps for this method are given below: Multiply the leftmost digit of the binary number with 2. Keep the result and discard the digit. Add the result of step 1 to the next digit. Now this becomes the leftmost digit. Repeat steps 1 and 2 till all the digits are exhausted. The final result is the required decimal number. Let us convert the binary number by the double dabble method. The steps are shown below: Computer Fundamentals and Programming in C
16
Conversion of a Fractional Binary Number into a Fractional Decimal Number
A fractional binary number can be converted into its equivalent decimal fractional number by the following procedure: Write the fractional binary number as negative powers of 2, taking the various digit positions after the binary point as 1,2,3…and so on. Convert each power of two into its decimal equivalent (see Table 4.8) Add these decimal values to produce the required final fractional decimal number. Table 4.8 decimal equivalent of some negative powers of 2 Negative power of 2 Decimal equivalent 0.5 0.25 0.125 0.0625 : Table 4.8 decimal equivalent of some negative powers of 2 Computer Fundamentals and Programming in C
17
Computer Fundamentals and Programming in C
Example 8. Convert the fractional binary number (0.1011) into its equivalent decimal number. Solution: The various steps are given below: Computer Fundamentals and Programming in C
18
Computer Fundamentals and Programming in C
Octal Number Notation This number system is a base 8 counting number system consisting of digit values 0 through 7. It is widely used as a shorthand way for expressing binary quantities. The octal system groups three binary bits together into one octal digit symbol (0-7), as shown in the Table below: Octal Binary 1 2 3 4 5 6 7 000 001 010 011 100 101 110 111 The advantage of the octal notation is that the conversion from binary to octal and vice versa becomes a very simple exercise. A binary number can be converted into its equivalent octal notations by the following procedure: Divide the binary number into groups of three bits each, starting from right to left. Replace each group by its octal equivalent Computer Fundamentals and Programming in C
19
Computer Fundamentals and Programming in C
For example, let us convert the binary number ‘11001’ into its octal equivalent by the following steps: Group the binary number ’11001’ : into groups of three bits each Replace each group by its octal equivalent : 3 1 Computer Fundamentals and Programming in C
20
Computer Fundamentals and Programming in C
Example 9. Convert the binary number ‘ ’ to its octal equivalent. Solution: The various steps are given below: Group the binary number ‘ ’ into groups of three bits each Replace each group by its octal equivalent Computer Fundamentals and Programming in C
21
Computer Fundamentals and Programming in C
Conversion of Decimal Numbers to Octal Numbers A decimal number can be converted into its octal equivalent by the following method: Divide the number by 8. Write the dividend under the number. This becomes the new number. Write the remainder at the right in a column. Repeat steps 1 to 3 until a 0 is produced as the new number. The digits written as remainders (bottom up) comprise the required octal number. Example 12. Convert the decimal 35 into its octal number. Solution: The various steps are as given: Computer Fundamentals and Programming in C
22
Computer Fundamentals and Programming in C
Conversion of Octal Numbers to Decimal Numbers An octal number can be converted into the equivalent decimal number by the following method: Write out the octal digits as powers of 8 Convert each power of 8 into its decimal equivalent. Add these terms to produce the required decimal number. For example, the octal number 753 can be converted into its equivalent decimal number as shown: Computer Fundamentals and Programming in C
23
Computer Fundamentals and Programming in C
Example 14. Convert the octal ‘527’ to its equivalent decimal number. Solution: The various steps are given below: Computer Fundamentals and Programming in C
24
Computer Fundamentals and Programming in C
Hexadecimal Notation The hexadecimal number system is a base-16 counting system. It uses 16 symbols: 0−9 and the capital letters A, B, C, D, E, and F. The alphabets A through F represent the numbers 10 through 15 respectively. Each hexadecimal digit is equivalent to a group of 4 binary bits, as shown in Table. Hexadecimal numbers Hexadecimal Binary 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111 Since each hexadecimal digit is equivalent to a group of 4 binary bits, the conversion of hexadecimal numbers to binary and vice versa becomes an easy exercise, as discussed in the following sections. Computer Fundamentals and Programming in C
25
Computer Fundamentals and Programming in C
Conversion of Binary Numbers into Equivalent Hexadecimal Numbers A binary number can be converted into the equivalent hexadecimal number by the following method: Divide the binary number into groups of 4 bits each, starting from right to left. Replace each group by its hexadecimal equivalent. Example 15. Convert the binary number ‘ ’ into its equivalent hexadecimal number. Solution: Group the given binary number into groups of 4 bits each: Replace each group by its hexadecimal equivalent: F E 1 Note: A fractional binary number can be converted into its equivalent hexadecimal number by grouping the binary bits from the binary point towards the right hand side. Thereafter each group is replaced by its corresponding hexadecimal digit. Computer Fundamentals and Programming in C
26
Computer Fundamentals and Programming in C
Conversion of Decimal Numbers to the Equivalent Hexadecimal Numbers A decimal number can be converted to the equivalent hexadecimal number by the following method: Divide the number by 16. Write the dividend under the number. This becomes the new number. Write the remainder in a column on the right. If the number is greater than or equal to 10 then write its equivalent symbol from Table 4.10. Repeat steps 1 to 3 until a 0 is produced as the new number. The digits written as remainders (bottom up) comprise the required hexadecimal number. Example 18. Convert the decimal 95 to its equivalent hexadecimal number. Solution: The various steps for the conversion are given below: Computer Fundamentals and Programming in C
27
Computer Fundamentals and Programming in C
Conversion of Hexadecimal Numbers to Decimal Numbers Write out the Hexadecimal digits as powers of 16. Convert each power of 16 into its decimal equivalent. Multiply each decimal equivalent value by the hexadecimal digit in that position Add these terms to produce the required decimal number. For example, the hexadecimal number A2D can be converted into its decimal equivalent as shown below: Computer Fundamentals and Programming in C
28
Computer Fundamentals and Programming in C
Data Representation We know that computers work with binary numbers whereas we work with alphabets, decimal numbers, symbols, and special characters. For instance, the data about a student could be written as given below: Name Roll Number Marks Akshay The above data is not in a machine readable form. Therefore, we must convert this into a suitable binary form. A brief discussion in the following sections throws some light on the various types of data. Computer Fundamentals and Programming in C
29
Computer Fundamentals and Programming in C
Number Representation A decimal number is converted into its binary equivalent. The binary number can be either positive or negative. The sign of the number, by convention, is represented by the most significant bit of the binary number. The most significant bit is also called a sign bit. For a positive number, the sign bit is set to a ‘0’ and for a negative number the sign bit is set to ‘1’. Depending upon the size of the internal registers of the CPU, binary numbers may be represented using 8, 16, 32, or 64 bits. For example, the 8-bit representation of +12 and −12 is given below: (+12) 10 = ( ) 2 (-12) 10 = ( ) 2 It may be noted that for an n-bit representation, n-1 bit data can be represented because 1 bit is reserved for the sign bit. The numbers further can be of two types: integer and real. Computer Fundamentals and Programming in C
30
Computer Fundamentals and Programming in C
Integer Representation An integer is represented by fixed point representation. In this representation, the decimal is assumed at the extreme right of the number. The leftmost bit is reserved for the sign bit. The magnitude of the number is represented in one of the following three ways: Signed magnitude representation: For an N-bit integer number, 1 bit is reserved for the sign and N − 1 bits are used for representing the magnitude of the number. The format of an 8-bit integer in signed magnitude form is given below: Computer Fundamentals and Programming in C
31
Computer Fundamentals and Programming in C
Thus, +17 and −17 will be represented as shown below: The disadvantage of this representation is that during arithmetic operations such as addition and subtraction, the sign bit has to be considered along with the magnitude. At the implementation level, this representation creates complications. Moreover, the number zero has two representations in this convention, as shown below: Computer Fundamentals and Programming in C
32
Computer Fundamentals and Programming in C
Signed 1’s Complement Representation: The 1’s complement of a binary integer can be obtained by inverting the bits of the number i.e., by replacing the digit 0 by 1 and the digit 1 by 0. For instance, the 1’s complement of the number ‘ ’ is ‘ ’. In fact, in the 1’s complement form a positive integer is represented as such, i.e., without any change. For example, (+17)10 is represented as 1’s complement form as shown below: A negative integer in this representation is represented as the 1’s complement of its corresponding positive integer. Thus (-17)10 is represented in 1’s complement form as shown below: Computer Fundamentals and Programming in C
33
Computer Fundamentals and Programming in C
However, in this representation also, the ‘0’ (zero) has two representations leading to an anomalous situation as shown below: Computer Fundamentals and Programming in C
34
Computer Fundamentals and Programming in C
Signed 2’s Complement Representation: The 2’s complement of a negative binary number is obtained by adding binary 1 to its 1’s complement. For example, 2’s complement of (-17)10 is obtained as shown below: In 2’s complement form, a positive integer is represented as such, i.e., without any change. For example, (+17)10 is represented as given below: A negative number in this form is represented as the 2’s complement of its corresponding positive integer. For instance, the 2’s complement of (-17)10 is given below: Computer Fundamentals and Programming in C
35
Computer Fundamentals and Programming in C
The advantage of this representation is that zero has only one representation. The (+0)10 is represented as given below: Let us try to represent (-0)10 in 2’s complement form. Thus, the 2’s complement of (+0)10 is the same as (-0)10 as shown below: Computer Fundamentals and Programming in C
36
Computer Fundamentals and Programming in C
Example 20. Express the following in signed magnitude form 35 −27 Solution: The 8-bit binary equivalent of 35 is : In signed magnitude form, it is represented as given below: The 8-bit binary equivalent of 27 is : The binary number (−27) will have the same magnitude with the leftmost bit set to 1, indicating that it is a negative number, as shown below: Computer Fundamentals and Programming in C
37
Computer Fundamentals and Programming in C
Floating point representation A floating point binary number is represented in the following form: ±𝑴∗ 𝟐 ±𝒆 Where: ±: is the sign plus or minus M: is the mantissa or significant e: is the exponent For example, the binary number can be represented in any of the following ways: 10111* 2-2 * 20 * 21 * 22 * 23 *24 Computer Fundamentals and Programming in C
38
Computer Fundamentals and Programming in C
In order to simplify and generalize the floating point operations, the following steps are taken. The mantissa part is considered to be a fixed point number with the decimal point situated at the extreme left of the most significant digit. For example, the mantissa part of the following binary number is a fixed point number or fraction: The mantissa part of the number is suitably shifted (left or right) to obtain a non-zero digit at the most significant position. This activity is called normalization. For example, the mantissa of the above given number has a 0 digit at the most significant position. This mantissa can be shifted one position towards the left to obtain the number given below: Computer Fundamentals and Programming in C
39
Computer Fundamentals and Programming in C
In fact, a floating point number is considered normalized if its mantissa does not contain zero as the most significant bit. In a 16-bit representation, let us assume that 10 bits are reserved for the mantissa and 6 for the exponent. Now, the normalized number given in the previous slide can be represented as shown below: Computer Fundamentals and Programming in C
40
Computer Fundamentals and Programming in C
Example 23. Represent the following floating point number in 16-bit representation. Solution: The given number is: The normalized floating point number = * 24 The 16-bit representation is given below: Computer Fundamentals and Programming in C
41
Computer Fundamentals and Programming in C
Internal Storage Encoding of Characters We know that the computer system works with binary numbers and therefore, several coding systems have been devised to represent the data and information as equivalent binary systems so that man- machine communication becomes easier and more efficient. The various computer codes are given below. BCD (Binary Coded Decimal): In this code an individual decimal digit of a decimal number is replaced by its equivalent group of four binary digits. For example, the decimal number 2016 is represented in BCD as shown below: Computer Fundamentals and Programming in C
42
Computer Fundamentals and Programming in C
EBCDIC (Extended Binary Coded Decimal Interchange Code): It is an 8-bit code used to represent alphanumeric data. The code has two parts of 4 bits each: zone bits and numeric bits. This code was popular with IBM mainframes and other larger computer systems. Table 4.12 gives EBCDIC codes for alphanumeric data. ASCII (American Standard Code for Information Exchange): It is a 7-bit code representing 128 different codes. It has two parts: three zone bits and four numeric bits. Most of the microprocessors and IBM-PCs use this code. Out of 128 ASCII codes, 96 are printable characters whereas 32 ASCII codes are used for control functions. Table 4.11 gives ASCII codes for alphanumeric data. Computer Fundamentals and Programming in C
43
Computer Fundamentals and Programming in C
ISCII (Indian Scripts Standard Code for Information Interchange): India is a land of many languages. All Indian languages are based on Brahmi, an ancient language of Bharat. There are 18 main languages, written using 10 scripts, namely, Devanagari, Gurumukhi, Gujarati, Oriya, Bengali, Assamese, Telugu, Kannada, Malayalam, and Tamil scripts. Since their source is the same, the phonetic structure of these languages is similar. This means that though an akshara (character) is written in different styles, its phonetic information is fairly uniform across the languages. Therefore, a standard code for the representation of Indian languages became possible. The tables for ISCII Indian Devanagari scripts is given in Fig. Computer Fundamentals and Programming in C
44
Table - Common computer codes
UNICODE: Unicode is a 16-bit (2 byte) standard code. It can code characters and therefore is used to code text of any written language of the world. It assigns each character a unique code. The Unicode standard for the nine scripts of India is based in ISCII-88. It is also abbreviated as TUSSI (The Unicode Standard for the nine Scripts of India). Table - Common computer codes Computer Fundamentals and Programming in C
45
Computer Fundamentals and Programming in C
Parity Bit A parity bit is an additional non-data bit that is added to binary data with a view to verify the validity of coded data. Two types of parity system are used: odd parity and even parity. Odd parity means that the total number of 1’s in the coded data including the parity bit should always be odd. For example, if the code is ‘ ’ then the parity bit 1 will be added to the code so that the number of 1’s becomes odd, as shown below: Similarly, if the code is ‘ ’ then the parity bit 0 will be added to the code so that the total number of 1’s remains odd, as shown below: Computer Fundamentals and Programming in C
46
Computer Fundamentals and Programming in C
Even parity means that the total number of 1’s in the coded data including the parity bit should always be even. For example, if the code is ‘ ’ then the parity bit 0 will be added to the code so that the number of 1’s remains even, as shown below: Similarly, if the code is ‘ ’ then the parity bit 1 will be added to the code so that the total number of 1’s becomes even, as shown below: It may be noted that most of the computer systems are designed as odd-parity machines. Therefore, the transmission system can detect all single bit malfunction or error during communication of data between devices. Computer Fundamentals and Programming in C
47
Table 4.12. ASCII codes for the string ‘COMPUTER’
Example 24. Represent the string ‘COMPUTER’ in ASCII code. Assume that odd-parity is being used for the transmission of data from one device to another. Solution: We assume that the string is being transferred character by character from one device to another. We will use Table 4.11 for the purpose of coding the characters of the given string in ASCII code. The required representation is given in Table 4.12. Table ASCII codes for the string ‘COMPUTER’ Character Parity bit ASCII Code ‘C’ ‘O’ ‘M’ ‘P’ ‘U’ ‘T’ ‘E’ ‘R’ 1 Computer Fundamentals and Programming in C
48
Binary Arithmetic Operations
We can perform the following basic arithmetic operations on binary numbers in the same manner as we do with decimal numbers. Binary addition Binary subtraction Binary multiplication Binary division Computer Fundamentals and Programming in C
49
Computer Fundamentals and Programming in C
Binary Addition Binary numbers are added in the same manner as decimal numbers. Since this number system comprises only two digits 0 and 1, the rules of binary digit addition are very simple, as listed in Table . Operation Sum carry 1 Let us add two binary numbers: 0101 (5) and 1001 (9). The operation is given below: The answer is : 1110, which is equivalent to decimal number 14. Computer Fundamentals and Programming in C
50
Computer Fundamentals and Programming in C
Example 28. Add two binary numbers: 1011 and 0111. Solution: The operation is given below: Since it is a 4-bit addition, the carry generated from the most significant 4th bit has been discarded. Computer Fundamentals and Programming in C
51
Computer Fundamentals and Programming in C
Binary Subtraction Binary numbers are subtracted in the same manner as decimal numbers. The rules of binary digits subtraction are very simple, as listed in Table Operation Difference Borrow 1 For borrowing from the adjacent column, the following rule is used: Rule of borrowing: If 1 is present in the immediate next left column then borrow it. If 1 is not available in the immediate next left column then borrow from the next left column. Now the result of subtraction will be 1. Repeat steps 1 and 2 to complete the subtraction process. Let us subtract a binary number from another: 0101 (5) from 1001 (9). The operation is as given: Computer Fundamentals and Programming in C
52
Computer Fundamentals and Programming in C
Example 31: Subtract from Solution: The operation is given below: Note: The above method of binary subtraction is somewhat cumbersome. The method given below is a simpler method to perform binary subtraction. Computer Fundamentals and Programming in C
53
Computer Fundamentals and Programming in C
Binary subtraction by 2’s complement: In a subtraction operation, the subtrahend is subtracted from the minuend. For instance, in examples of binary subtractions given above, the number 1 and number 2 are minuend and subtrahend respectively. From Section , we know that a negative number can be represented by its 2’s complement. The steps for binary subtraction are given below: Convert the subtrahend to its 2’s complement. Add the 2’s complement obtained in step 1 to the minuend to obtain the desired answer. Let us subtract two binary numbers: 1001 (9) and 0101 (5). The operation is given below: Given: minuend : 1001 subtrahend : 0101 2’s complement of subtrahend = 1010 The subtraction operation is as given: The result is: 0100 (4) It may be noted that carry generated from most significant bit has been discarded. Computer Fundamentals and Programming in C
54
Computer Fundamentals and Programming in C
Example 32. Subtract from 10002 Solution: Given minuend : 1000 subtrahend : 0001 2’s complement of subtrahend = 1111 The subtraction operation is given below: The result is : 0111 Computer Fundamentals and Programming in C
55
Computer Fundamentals and Programming in C
Binary Multiplication Binary numbers are multiplied in the same manner as decimal numbers. Since it comprises only two digits 0 and 1, the rules of binary digit multiplication are very simple, as listed in the Table below. Operation Product 0 * 0 0 * 1 1 * 0 1 * 1 1 In the multiplication operation we have two terms: multiplicand and multiplier. The rightmost bit of the multiplier is multiplied with all the digits of the multiplicand to obtain a partial product. The operation is repeated for the rest of the bits of the multiplier to obtain partial products. Each partial product is written in a new line shifted one place to the left. The sum of the partial products gives the final product. Computer Fundamentals and Programming in C
56
Computer Fundamentals and Programming in C
Example 35: Perform multiplication of and Solution: Given multiplicand : 1010 multiplier : 1010 The multiplication operation is given below: It is left to the reader to verify the answer. Computer Fundamentals and Programming in C
57
Computer Fundamentals and Programming in C
Binary Division Binary division is performed in the same manner as is done for decimal numbers. The dividend is divided by a divisor to obtain a quotient and a possible remainder. Let us divide by 1012 Given: Dividend: 11112 Divisor: 1012 The operation is as given: Computer Fundamentals and Programming in C
58
Computer Fundamentals and Programming in C
Example 36: Divide by 1102 Solution: The solution is as follows: Dividend: Divisor: 1102 The operation is: Computer Fundamentals and Programming in C
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.