Data Representation
Unit: 1
Prepared By
Krishna Dev Thapa Magar
Data Types
❑ In computer science, a data type or simply type is a classification identifying one of various types of
data, such as real, integer or Boolean, that determines the possible values for that type; the operations
that can be done on values of that type; the meaning of the data; and the way values of that type can
be stored.
❑ Data types are used within type systems, which offer various ways of defining, implementing and using
them. Common data types may include:
❑ Integers
❑ Booleans
❑ Characters
❑ Floating-point numbers
❑ Alphanumeric strings
❑ The computer registers contain either data or control information. Data are numbers and other binary-
coded information that are operated on. Control information is a bit or group of bits used to specify
the sequence of command signals needed for data manipulation.
❑ Possible data types in registers:
❑ Numbers used in computations
❑ Letters of the alphabet used in data processing
❑ Other discrete symbols used for specific purposes
❑ All types of data, except binary numbers, are represented in binary-coded form. Numbers are
represented by a string of digit symbols.
❑ Based on number systems two basic data types are implemented in the computer system: fixed point
numbers and floating point numbers. Representing numbers in such data types is commonly known as
fixed point representation and floating point representation.
Number System
Number systems are used to describe the quantity of something or represent certain information. Number
of digits used in a number system is called its base or radix (r). We can categorize number system as below:
❑ Binary number system (r = 2)
Binary number system was introduced by an Indian- scholar Pingala in around 5th -2nd centuries in
BC. Long and short syllables were used by him to illustrate the two types of numbers, it is more like
Morse code. Gottfried Leibniz in 1679 introduced the modern type of binary number system which
we still use. The number system with only two digit 0 and 1 is known as binary number system. Here,
zero is represented by a symbol '0' and one is represented as '1'. Binary is another way of saying base-
2.
Binary number system has the base (or radix) 2 and the numbers in this system are formed with two
digits 1 & 0.
Octal Number System (r = 8)
The octal numeral system, or “oct” for short, is the base-8 number system, and uses the digits 0 to 7. When we
count up one from the 7, we need a new placement to represent what we call 8 since an 8 doesn't exist in Octal.
So, after 7 it is 10.
Hexadecimal Number system (r = 16)
The hexadecimal system is base-16. As its base implies, this number system uses sixteen symbols to represent
numbers. So, in hexadecimal, the total list of symbols to use is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.
Decimal Number System (r = 10)
❑ The number system that we use in our day-to-day life is the decimal number system. The word decimal
comes from “decem”, the Latin word for ten. The decimal number system (also called base-10 or
occasionally denary) has 10 as its base. Any number, from huge quantities to tiny fractions, can be
written in the decimal system using only the ten basic symbols 1, 2, 3, 4, 5, 6, 7, 8, 9, and 0.
❑ Values are represented by the digits and their positions in the number and the type of number system is
called Positional Number System. In decimal number system, the successive positions to the left of the
decimal point represents: units, tens, hundreds, thousands and so on.
❑ Each position represents a specific power of the base (10). For example, the decimal number 1234
consists of the digit 4 in the units position, 3 in the tens position, 2 in the hundreds position, and 1 in the
thousands position, and its value can be written as:
= (1x1000) + (2x100) + (3x10) + (4x1) = (1x103) + (2x102) + (3x101) + (4x100)
= 1000 + 200 + 30 + 1
= (1234)10
Number System Conversion
Decimal to Other Base System Steps:
❑ Step 1 - Divide the decimal number to be converted by the value of the new base.
❑ Step 2 - Get the remainder from Step 1 as the rightmost digit (least significant digit) of new base
number.
❑ Step 3 - Divide the quotient of the previous divide by the new base.
❑ Step 4 - Record the remainder from Step 3 as the next digit (to the left) of the new base number.
❑ Repeat Steps 3 and 4, getting remainders from right to left, until the quotient becomes zero in Step 3.
❑ The last remainder thus obtained will be the most significant digit (MSD) of the new base number.
❑ Example: Decimal Number: (29)10
❑ Calculating Binary Equivalent:
As mentioned in Steps 2 and 4, the remainders have to be arranged in the reverse order so that the first
remainder becomes the least significant digit (LSD) and the last remainder becomes the most significant digit
(MSD).
Decimal Number: (29)10 = Binary Number: (11101)2
Other base system to Decimal System
Steps:
❑ Step 1 - Determine the column (positional) value of each digit (this depends on the position of the digit
and the base of the number system).
❑ Step 2 - Multiply the obtained column values (in Step 1) by the digits in the corresponding columns.
❑ Step 3 - Sum the products calculated in Step 2.
❑ The total is the equivalent value in decimal.
❑ Example: Binary Number: (11101)2
❑ Calculating Decimal Equivalent:
Other Base System to Non-Decimal System
Steps:
❑ Step 1 - Convert the original number to a decimal number (base 10).
❑ Step 2 - Convert the decimal number so obtained to the new base number.
❑ Example: Octal Number: (25)8
Fixed point representations
Fixed point representation is a method of storing numbers in binary format. Fixed point refers to a method of
representing numbers with a fractional part on an ALU that only handles integer operations.
Integer Representation
Integers are whole numbers or fixed-point numbers with the radix point fixed after the least significant bit. They
are contrast to real numbers or floating-point numbers, where the position of the radix point varies. It is
important to take note that integers and floating-point numbers are treated differently in computers.
Computers use a fixed number of bits to represent an integer. The commonly-used bit-lengths for integers are 8-
bit, 16-bit, 32-bit or 64-bit. Besides bit-lengths, there are two representation schemes for integers:
❑ Unsigned Integers: can represent zero and positive integers.
❑ Signed Integers: can represent zero, positive and negative integers. Three representation schemes
had been proposed for signed integers:
❑ Sign-Magnitude representation
❑ 1's Complement representation
❑ 2's Complement representation
Advantages and Disadvantages of unsigned notation
Advantages:
❑ One representation of zero
❑ Simple addition
Disadvantages
❑ Negative numbers cannot be represented.
❑ The need of different notation to represent negative numbers.
In all the above three schemes, the most-significant bit (MSB) is called the sign bit. The sign bit is used to
represent the sign of the integer - with 0 for positive integers and 1 for negative integers. The magnitude of the
integer, however, is interpreted differently in different schemes.
Sign-Magnitude Representation
❑ sign bit is 0 for positive, 1 for negative
❑ magnitude part = absolute value of number
❑ 2 representations for 0. (0000 and 1000)
Example 1: Suppose that n=8 and the binary representation is (0 100 0001)2.
Sign bit is 0 ⇒ positive
Absolute value is (100 0001)2 = (65)10
Hence, the integer is +(65)10
Example 2: Suppose that n=8 and the binary representation is (1 000 0001)2.
Sign bit is 1 ⇒ negative
Absolute value is (000 0001)2 = (1)10
Hence, the integer is –(1)10
Example 3: Suppose that n=8 and the binary representation is (0 000 0000)2.
Sign bit is 0 ⇒ positive
Absolute value is (000 0000)2 = (0)10
Hence, the integer is +(0)1
Example 4: Suppose that n=8 and the binary representation is (1 000 0000)2.
Sign bit is 1 ⇒ negative
Absolute value is (000 0000)2 = (0)10
Hence, the integer is –(0)10
The drawbacks of sign-magnitude representation are:
❑ Addition and subtractions are difficult.
❑ Signs and magnitude, both have to carry out the required operation.
❑ There are two representations and for the number zero, which could lead to inefficiency and confusion.
❑ (0000 0000)2 = +010
❑ (1000 0000)2 = -010
❑ Positive and negative integers need to be processed separately.
1’s complement Representation
❑ sign bit (MSB) is 1 for negative, 0 for positive
❑ negation is complement, e.g. 5 = 0101, -5 = 1010
❑ two representations for 0: 1111 and 0000
Example 1: Suppose that n=8 and the binary representation (0 100 0001)2.
Sign bit is 0 ⇒ positive
Absolute value is (100 0001)2 = (65)10
Hence, the integer is +(65)10
Example 2: Suppose that n=8 and the binary representation (1 000 0001)2.
Sign bit is 1 ⇒ negative
Absolute value is the complement of (000 0001)2, i.e., (111 1110)2 = (126)10
Hence, the integer is –(126)10
Example 3: Suppose that n=8 and the binary representation (0 000 0000)2.
Sign bit is 0 ⇒ positive
Absolute value is (000 0000)2 = (0)10
Hence, the integer is +(0)10
Example 4: Suppose that n=8 and the binary representation (1 111 1111)2.
Sign bit is 1 ⇒ negative
Absolute value is the complement of (111 1111)2, i.e., (000 0000)2 = (0)10
Hence, the integer is -(0)10
Again, the drawbacks are:
❑ There are two representations for zero.
❑ (0000 0000)2 = +010
❑ (1111 1111)2 = -010
❑ The positive integers and negative integers need to be processed separately.
2’s complement Representation
❑ sign bit (MSB) is 1 for negative, 0 for positive
❑ add 1 to 2's complement negative numbers
❑ only one representation for 0
❑ negation is complement + 1, e.g. 5 = 0101, -5 = 1010 + 1 = 1011
Example 1: Suppose that n=8 and the binary representation (0 100 0001)2.
Sign bit is 0 ⇒ positive
Absolute value is (100 0001)2 = (65)10
Hence, the integer is +(65)10
Example 2: Suppose that n=8 and the binary representation (1 000 0001)2.
Sign bit is 1 ⇒ negative
Absolute value is the complement of (000 0001)2 plus 1, i.e., (111 1110)2 + (1)2 = (127)10
Hence, the integer is -(127)10
Example 3: Suppose that n=8 and the binary representation (0 000 0000)2.
Sign bit is 0 ⇒ positive
Absolute value is (000 0000)2 = (0)10
Hence, the integer is +(0)10
Example 4: Suppose that n=8 and the binary representation (1 111 1111)2.
Sign bit is 1 ⇒ negative
Absolute value is the complement of (111 1111)2 plus 1, i.e., (000 0000)2 + (1)2 = (1)10
Hence, the integer is -(1)10
Arithmetic Addition and Subtraction of Signed Numbers
Arithmetic Addition
❑ The addition of 2 numbers in the signed-magnitude system follows the rules of ordinary arithmetic.
❑ Compare their signs, if the signs are the same, we add the two magnitudes and give the sum the
common sign. Look for Overflow.
❑ If the signs are different, we subtract the smaller magnitudes from the larger and give the result the
sign of the larger magnitude.
❑ Negative numbers must be in 2’s complement and that the sum obtained after the addition if negative
is in 2’s-complement form.
In each of the 4 cases, the operation performed is always addition, including the sign-bits. Any carry out of
the sign bit is discarded and negative results are automatically in 2's complement form.
Decimal Fixed-Point Representation
Decimal number representation = f (binary code used to represent each decimal digit). Output of this function is
called the Binary coded Decimal (BCD). A 4-bit decimal code requires 4 flip-flops for each decimal digit.
Example: 4385 = (0100 0011 1000 0101)BCD
Disadvantages of BCD representation:
❑ wastage of memory
❑ Circuits for decimal arithmetic are quite complex.
Advantages of BCD representation:
❑ Eliminate the need for conversion to binary and back to decimal. (since applications like Business data
processing requires less computation than I/O of decimal data, hence electronic calculators perform
arithmetic operations directly with the decimal data (in binary code))
For the representation of signed decimal numbers in BCD, sign is also represented with 4-bits, plus with 4 0's and
minus with 1001 (BCD equivalent of 9). Negative numbers are in 10's complement form.
Consider the Addition: (+375) + (-240) = +135 [0→positive, 9→negative in case of radix 10]
To obtain the 10’s complement of a BCD number, first take the 9’s complement and then add one to the least
significant digit.
Limitation of Fixed-Point Representation
To represent large numbers or very small numbers we need a very long sequences of bits. This is because we have
to give bits to both the integer part and the fraction part.
Floating Point Representation
A floating-point number (or real number) can represent a very large (1.23×1088) or a very small (1.23×10-88) value.
It could also represent very large negative number (-1.23×1088) and very small negative number (-1.23×1088), as
well as zero, as illustrated:
In the decimal system there are 2 ways of floating point representation: Scientific Notation and Floating point
notation.
A floating-point number is typically expressed in the scientific notation, with a fraction (F), and an exponent (E) of
a certain radix (r), in the form of F × r E . Decimal numbers use radix of 10 (F × 10E ); while binary numbers use radix
of 2 (F × 2 E ).
Representation of floating point number is not unique. For example, the number 55.66 can be represented as
5.566×101 , 0.5566×102 , 0.05566×103 , and so on. The fractional part can be normalized. In the normalized form,
there is only a single non-zero digit before the radix point. For example, decimal number 123.4567 can be
normalized as 1.234567×102 ; binary number (1010.1011)2 can be normalized as (1.011011)2×23 .
Thus, there are three parts in the floating-point representation:
❑ The sign bit (S) is self-explanatory (0 for positive numbers and 1 for negative numbers).
❑ For the exponent (E), a so-called bias (or excess) is applied so as to represent both positive and
negative exponent. The bias is set at half of the range. For single precision with an 8-bit exponent,
the bias is 127 (or excess-127). For double precision with a 11-bit exponent, the bias is 1023 (or excess-
1023).
❑ The fraction (F) (also called the mantissa or significand) is composed of an implicit leading bit
(before the radix point) and the fractional bits (after the radix point). The leading bit for normalized
numbers is 1; while the leading bit for denormalized numbers is 0
It is important to note that floating-point numbers suffer from loss of precision when represented with a fixed
number of bits (e.g., 32-bit or 64-bit). This is because there are infinite number of real numbers (even within a
small range of says 0.0 to 0.1).
It is also important to note that floating number arithmetic is very much less efficient than integer arithmetic. It
could be speed up with a so-called dedicated floating-point co-processor. Hence, use integers if your application
does not require floating-point numbers.
In computers, floating-point numbers are represented in scientific notation of fraction (F) and exponent (E) with
a radix of 2, in the form of F×2E . Both E and F can be positive as well as negative.
Note: In Floating Point Number representation, only Mantissa (M) and Exponent (E) are explicitly represented.
The Radix (R) and the position of the Radix Point are implied.
Example A binary number +1001.11 in 16-bit floating point number representation (6-bit exponent and 10- bit
fractional mantissa):
Normalization
A floating-point number is said to be normalized if the most significant digit of the mantissa is nonzero. For
example, decimal number 350 is normalized but 00035 is not. The 8-bit number 00011010 is not normalized.
Normalize it by fraction = 11010000 and exponent = -3
Representation of Zero
- Zero
Mantissa = 0
- Real Zero
Mantissa = 0
Exponent = smallest representable number which is represented as 00 ... 0
Binary and Decimal Codes
Internally, digital computers operate on binary numbers. When interfacing to humans, digital processors, e.g.
pocket calculators, communication is decimal-based. Input is done in decimal then converted to binary for
internal processing. For output, the result has to be converted from its internal binary representation to a decimal
form.
To be handled by digital processors, the decimal input (output) must be coded in binary in a digit by digit manner.
For example, to input the decimal number 957, each digit of the number is individually coded and the number is
stored as 100101010111. Thus, we need a specific code for each of the 10 decimal digits. There is a variety of such
codes, such as:
❑ Weighted codes
❑ Non-Weighted codes
❑ Self-Complementing codes
❑ Reflective codes
❑ Alphanumeric codes
❑ Error detecting and correcting codes
2. Non-Weighted Codes:
Each bit has no positional value
❑ Excess-3 code
❑ Gray code
❑ Five bit BCD
3. Self-Complementing codes or Reflective codes
Code for one digit will be the complement of other
❑ 2421
❑ 5211
❑ Excess-3
4. Sequential Codes
Succeeding number is one more than the previous one
❑ 8421
❑ Excess-3
5. Alphanumeric codes
❑ American Standard Code for Information Interchange (ASCII)
❑ Extended Binary Coded Decimal Interchange Code (EBCDIC)
❑ Hollerith Code
❑ Five bit Baudot Code
❑ Morse Code
6. Error Detection and Correction Codes
For reliable transmission and storage of digital data, error detection and correction is required. Examples of
codes which permit error detection and error correction are: Parity Codes, Hamming Code, etc.
Advantages of BCD Codes
❑ It is very similar to decimal system.
❑ We need to remember binary equivalent of decimal numbers 0 to 9 only, such as those found in digital
clocks or digital voltmeters.
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. So BCD is less efficient
than binary.
ASCII Code:
The American Standard-Code for Information Interchange (ASCII) pronounced "as-kee" is a 7-bit code based on
the ordering of the English alphabets. The ASCII codes are used to represent alphanumeric data in computer
input/output. It represents a total of 128 characters.
These include 95 printable characters including 26 upper-case letters (A to Z), 26 lowercase letters (a to z), 10
numerals (0 to 9) and 33 special characters such as mathematical symbols, space character etc. It also defines
codes for 33 non-printing obsolete characters except for carriage return and/or line feed. The below table lists the
7 bit ASCII code containing the 95 printable characters.
The format of ASCII code for each character is X6X5X4X3X2X1X0 where each X is 0 or 1. For instance, letter D is
coded as 1000100.
More examples are: The ASCll-7 code for ‘d’ is 1100100 as seen from the table. The ASCll-7 code for '+' is 0101011 as
seen from the table.
An eight-bit version of the ASCII code, known as US ASCII-8 or ASCII-8, has also been developed. Since it uses 8-
bits, so this version of ASCII can represent a maximum of 256 characters.
Example 1: With an ASCII-7 keyboard, each keystroke produces the ASCII equivalent of the designated character.
Suppose that you type PRINT X. What is the output of an ASCII-7 keyboard?
Solution: The sequence is as follows:
The ASCII-7 equivalent of P = 101 0000
The ASCII-7 equivalent of R = 101 0010
The ASCII-7 equivalent of I = 1001010
The ASCII-7 equivalent of N = 100 1110
The ASCII-7 equivalent of T = 1-010100
The ASCII-7 equivalent of space = 010 0000
The ASCII-7 equivalent of X = 101 1000
So the output produced is 1010000101001010010101001110101010001000001011000. The output in hexadecimal
equivalent is 50 52 49 4E 54 30 58
Example2: A computer sends a message to another computer using an odd-parity bit. Here is the message in
ASCII-8 Code.
1011 0001
1011 0101
1010 0101
1010 0101
1010 1110
What do these numbers mean?
Solution On translating, the 8-bit numbers into their equivalent ASCII-8 code we get the word 1011 0001 (Q),
10110101 (U), 10100101 (E), 1010 0101 (E), 1010 1110 (N)
So, on translation we get QUEEN as the output.
Error Detection and Correction Code
❑ Error means a condition when output information is not same as input information.
❑ When transmission of digital signals takes place between two systems such as a computer, the
transmitted signal is combined with the "Noise".
❑ The noise can introduce an error in the binary bits travelling from one system to other. That means 0
may change to 1 or a 1 may change to 0.
Error Detecting Codes
Whenever a message is transmitted then there are changes that it get scrambled by noise or data gets
corrupted. When we add some additional data to a given digital message which can help us to detect if an
error occurred during transmission of the message then adding such data is called an error-detecting code. A
simple example of error-detecting code is parity check.
Error Correcting Codes
Along with Error detecting code, we can also pass some data which help to figure out the original message
from the corrupt message that we received. This type of code is called an errorcorrecting code. Error
correcting codes also deploys the same strategy as error detecting codes but additionally, such codes also
detects the exact location of corrupt bit. In error correcting code, parity check has simple way to detect error
along with a sophisticated mechanism to determine the corrupt bit location. Once corrupt bit is located, its
value is reverted (from 0 to 1 or 1 to 0) to get the original message.
How to detect and correct errors?
For the detection and correction of these errors, one or more than one extra bits are added to the data bits at the
time transmitting. These extra bits are called as parity bits. They allow detection or correction of the errors. The
data bits along with the parity bits form a code word.
Binary information may be transmitted through some communication medium, e.g. using wires or wireless media.
A corrupted bit will have its value changed from 0 to 1 or vice versa. To be able to detect errors at the receiver
end, the sender sends an extra bit (parity bit) with the original binary message.
A parity bit is an extra bit included with the n-bit binary message to make the total number of 1’s in this message
(including the parity bit) either odd or even. If the parity bit makes the total number of 1’s an odd (even) number,
it is called odd (even) parity. The table shows the required odd (even) parity for a 3-bit message.
❑ At the receiver end, an error is detected if the message does not match have the proper parity (odd/even).
Parity bits can detect the occurrence 1, 3, 5 or any odd number of errors in the transmitted message. No error
is detectable if the transmitted message has 2 bits in error since the total number of 1’s will remain even (or
odd) as in the original message. In general, a transmitted message with even number of errors cannot be
detected by the parity bit.
❑ Binary information may be transmitted through some communication medium, e.g. using wires or wireless
media. Noise in the transmission medium may cause the transmitted binary message to be corrupted by
changing a bit from 0 to 1 or vice versa. To be able to detect errors at the receiver end, the sender sends an
extra bit (parity bit).
❑ How does error detection take place? The parity checking at the receiver can detect the presence of an error
if the parity of the receiver signal is different from the expect parity. That means if is known that the parity of
the transmitted signal is always going to be "even" and if the received signal has an odd parity then the
receiver can conclude that the received signal is not correct. If presence of error is detected then the receiver
will ignore the received byte and request for retransmission of the same byte to the transmitter.