Chapter 5-DATA TYPE AND DATA REPRESENTATIONS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

DATA TYPE AND DATA REPRESENTATION

Data type

A data type is a method of interpreting and representing data held in a computer.

Intrinsic data types


Intrinsic data types are the data types that are defined within a particular programming language.

The main/general forms of data types are as follows:

Integer
 An integer is a positive or negative whole number that does not contain a fractional part.
 Integers are held in pure binary for processing and storage.
 In some programming languages integers can be short and long integers (more bytes are used to store
long integers).
 Integers are made up of digits, which are a single place that can hold numerical values between 0 and
9.
 Digits are normally combined together in groups to create larger numbers.
 For example 6357 has four digits.
 Integers are stored by the computer as binary numbers using a whole number of bytes.
 It is usual to use either 2 bytes (called short integers) or 4 bytes (called long integers), the difference
being simply that long integers can store larger numbers.
 Negative integers can also be stored, these have to be treated as different types of data by the computer
because the Most Significant Bit (MSB) of the data stands for something different than in an ordinary
unsigned integer.

Real
A real is a number that contains a decimal point. Real numbers can also be referred to as single or double,
depending upon the number of bytes in which they are stored.

Boolean
 A Boolean is a data-type that can store one of only two values –True or False.
 Boolean are stored in one byte – True being stored as 11111111 and False as 00000000.
 A simple example of its use would be in the control program for an automatic washing machine.
 One of the important pieces of information for the processor would be to know whether the door was
shut.
 A Boolean variable could be set to 0 if it was open and to 1 if it was shut.
 A simple check of that value would tell the processor whether it was safe to fill the machine with
water

String
 A string (or text) is a series of alphanumeric characters usually enclosed in quotation marks.
 Any type of alphabetic or numeric data can be stored as a string, e.g: “Mutare”, “07/02/1978”, “123”
and “36.85” are all strings.
 Each character within a string will be stored in one byte using its ASCII code.
 However modern systems might store each character in two bytes using its Unicode.
 The maximum length of a string is limited only by the available memory.

Character
A character is any single digit, letter or symbol that can be represented in a computer, for example, 2, t, G, %,
&, M, space, etc. Each character is represented using binary digits, which the computer can understand;
therefore take up a single unit of storage on the computer. Some programming languages refer to this as a Char.
Can be used to represent coded data e.g. M for Male, F for Female.

Compiled By Kapondeni T. Page 1 | 25


Date
Used to represented dates, e.g date of birth, etc. can be long or short dates, e.g dd/mm/yy, dd/mm/yyyy or dd-
MonthName-yyyy. Dates usually take 8 bytes of storage.

In general memory requirements for different data types are as follows:

User-defined data types


These are data types personally designed by programmers to suit their situation, e.g. for record designs. User-
defined data types contain items from several of the different intrinsic data types.

Visual Basic uses User Defined Data types (UDTs) as a way of implementing data structures. In C/C++, they are
called Structures (structs) and in Pascal and COBOL they are called records.

UDTs can be declared only at the module-level (form) (you may not declare a UDT in an individual Sub or
Function).
UDTs may have Public (project-level) or Private (module-level) scope. If the keyword Public or Private is
omitted, the default is Public.

UDTs with Public scope may only be defined in standard modules, not forms.

The syntax for defining a UDT is:

[Public/ Private] Type TypeName


Variable1 As datatype
...
Variablen As datatype
End Type

For example, to define a UDT for an employee record, you might code the following:

Public Type EmployeeRecord


strEmpName As String
dtmHireDate As Date
sngHourlyRate As Single
End Type

To use a UDT, you must define a variable "As" the name following the keyword "Typ e" (in this case,
"EmployeeRecord"). For example:

Dim udtEmpRec As EmployeeRecord


The above defines a variable called "udtEmpRec" which has the attributes defined by the structure
"EmployeeRecord". Thus, it is "udtEmpRec" which you refer to in your procedural statements, NOT
"EmployeeRecord". the following code places data in the individual elements of udtEmpRec:
udtEmpRec.strEmpName = "JOE SMITH"
udtEmpRec.dtmHireDate = #1/15/2001#
Compiled By Kapondeni T. Page 2 | 25
udtEmpRec.sngHrlyRate = 25.50

Benefits of defined data-types


The use of data-types (Intrinsic and user-defined) within a programming language has the following benefits:
• enable the compiler to reserve the correct amount of memory for the data – e.g. 4 bytes for an integer;
• trap errors that a programmer has made and errors that a user of a program can make – a variable
defined as an integer cannot be given a fractional value;
• restrict the values that can be given to the data – a Boolean cannot be given the value “maybe”;
• Restrict the operations that can be performed on the data – a string cannot be divided by 10.

Units Of Data Storage

In general, the units of data storage are as follows:

 I Bit = 1 or 0
 I Nibble = 4 Bits (1/2 a Byte)
 I Byte = 8 Bits
 I Kilobyte (Kb) = 1024 Bytes = 210 Bytes
 1 Megabyte (Mb) = 1024 Kilobytes = 220 Bytes
 1 Gigabyte (Gb) = 1024 Megabytes = 230 Bytes
 1 Terabyte (Tb) = 1024 Gigabytes = 240 Bytes

1. Bit

Bit is short for BInary digiT. It is a single digit in base 2, that is, either 1 or 0. A bit is the smallest unit of data
that the computer can process. Therefore a binary number is composed of these two values only, that is 1 and
0. Bit represents two states, “ON” or “OFF”, true or false, or yes or no

2. Byte
A byte is a group of 8 bits representing a character. A character is any digit, letter or symbol that can be
represented in a computer, for example, 2, t, G, %, &, M, space, etc. Each character is represented using
binary digits, which the computer can understand, therefore take up a single unit of storage on the
computer. With 8 bits in a byte, you can represent 256 values ranging from 0 to 255:

0 = 00000000
1 = 00000001
2 = 00000010
...
254 = 11111110
255 = 11111111
NB: However, the byte size may differ with the architecture of the computer. Other computers use an 8-bit
byte, other 32-bit byte, others 64-bit byte. Thus in general, a byte can be a unit representation of character,
which could be 8, 16, 32 or in 64 bits. However, for this course, we will assume a bit as a group of 8-bits
representing a character.

3. Word
A word is a fixed-size group of bits that can be handled as a unit by the processor.
Word size refers to the number of bits that the CPU can simultaneously process, which could be 8 bits,
16 bits, 32 bits or 64 bits. The bits are processed as a unit during input and output. A 64 bit processor can
process data faster than a 32 bit processor, thus word size affects processor speed.

Compiled By Kapondeni T. Page 3 | 25


DATA REPRESENTATION
The form of data representation is in its character set. All the characters that a system can recognise are called
its character set. Character set (or data representation) can be as follows:

1. American Standard Code for Information Interchange (ASCII)


ASCII uses 7 bits which gives 128 combinations. However the extended ASCII now uses 8 bits so there are
256 different codes that can be used and hence 256 different characters. However, this is not quite true, as
some of the bits can be used for parity checks.
The American Standard Code for Information Interchange (ASCII) is widely used in computers of all types.
ASCII codes are of two types –ASCII-7 and ASCII-8.
• ASCII-7 is a 7-bit standard ASCII code. In ASCII-7, the first 3 bits are the zone bits and the next 4 bits are
for the digits. ASCII-7 allows 27 = 128 combinations. 128 unique symbols are represented using ASCII-7.
ASCII-7 has been modified by IBM to ASCII-8.
• ASCII-8 is an extended version of ASCII-7. ASCII-8 is an 8-bit code having 4 bits for zone and 4 bits for the
digit. ASCII-8 allows 28 = 256 combinations. ASCII-8 represents 256 unique symbols. ASCII is used widely
to represent data in computers.
• The ASCII-8 code represents 256 symbols.
o Codes 0 to 31 represent control characters (non-printable), because they are used for actions like, Carriage
return (CR), Bell (BEL) etc.
o Codes 48 to 57 stand for numeric 0-9.
o Codes 65 to 90 stand for uppercase letters A-Z.
o Codes 97 to 122 stand for lowercase letters a-z.
o Codes 128-255 are the extended ASCII codes.

In the ASCII character set, each binary value between 0 and 127 is given a specific character. Most computers
extend the ASCII character set to use the full range of 256 characters available in a byte. The upper 128
characters handle special things like accented characters from common foreign languages.

You can see the 127 standard ASCII codes below. Computers store text documents, both on disk and in
memory, using these codes. For example, if you use Notepad in Windows OS to create a text file containing
the words, "Four score and seven years ago," Notepad would use 1 byte of memory per character (including 1
byte for each space character between the words -- ASCII character 32). When Notepad stores the sentence in
a file on disk, the file will also contain 1 byte per character and per space.
Try this: Open up a new file in Notepad and insert the sentence, "Four score and seven years ago" in it. Save
the file to disk under the name getty.txt. Then use the explorer and look at the size of the file. You will find
that the file has a size of 30 bytes on disk: 1 byte for each character. If you add another word to the end of the
sentence and re-save it, the file size will jump to the appropriate number of bytes. Each character consumes a
byte.
If you were to look at the file as a computer looks at it, you would find that each byte contains not a letter but a
number -- the number is the ASCII code corresponding to the character (see below). So on disk, the numbers
for the file look like this:
F o u r a n d s e v e n
70 111 117 114 32 97 110 100 32 115 101 118 101 110
By looking in the ASCII table, you can see a one-to-one correspondence between each character and the
ASCII code used. Note the use of 32 for a space -- 32 is the ASCII code for a space. We could expand these
decimal numbers out to binary numbers (so 32 = 00100000) if we wanted to be technically correct -- that is
how the computer really deals with things.
The first 32 values (0 through 31) are codes for things like carriage return and line feed. The space character is
the 33rd value, followed by punctuation, digits, uppercase characters and lowercase characters.
ASCII codes can just be used for representing characters and not for arithmetic calculations.
ASCII codes also occupy a lot of disc storage space.

2. Binary System
Data is represented in 0s and 1s, thus in base 2. It is obtained by dividing the denary number by 2, taking
the remainders only. The number of bits in the answer does not matter unless specified.

Compiled By Kapondeni T. Page 4 | 25


3. BCD
Each decimal digit is represented by its own 4-bit binary code as follows:
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001

The number 3765 is thus coded as 0011 0111 0110 01012

BCD is used to represent some numbers that are not proper numbers (numbers that don’t behave like
numbers). A barcode looks like a number, but if the barcodes are added together the result is not a barcode for
any product. The arithmetic does not give a sensible answer. Values like this that look like numbers but do not
behave like them are often stored in binary coded decimal (BCD). Each digit is simply changed into a four bit
binary number which are then placed after one another in order.
- This has the advantage that it is easy to convert a number from BCD to decimal form and vice versa.
- There is no rounding off numbers when computing fractional numbers, thus no errors due to rounding
off.
- Used in businesses where significant digit needs to be retained.
However:
- As compared to pure binary, more bits are needed to store a number, thus more memory is needed
- Calculations with such numbers are more complex than in pure binary numbers, e.g.
Adding 1 and 19, i.e 0000 00012
+ 0001 10012
0001 10102, the first digit, 1 is wrong and 1010 does not exist in BCD.
The error is caused by the range of numbers used for representing data in BCD. BCD used 4 bits which is 24 =
16 combinations. However the maximum range of numbers represented 9. 6 has to be added to the result if the
sum of bit is greater than 9. Thus adding the result above, 0001 10102 to 6 (0110) gives us 0010 00002, which
is 20 in BCD.

4. EBCDIC
The Extended Binary Coded Decimal Interchange Code (EBCDIC) uses 8 bits (4 bits for zone, 4 bits for digit)
to represent a symbol in the data.
• EBCDIC allows 28 = 256 combinations of bits.
• 256 unique symbols are represented using EBCDIC code. It represents decimal numbers (0-9), lower
case letters (a-z), uppercase letters (A-Z), Special characters, and Control characters (printable and
non-printable e.g. for cursor movement, printer vertical spacing etc.).
• EBCDIC codes are used, mainly, in the mainframe computers.

5. UNICODE
Unicode is a universal character encoding standard for the representation of text which includes letters,
numbers and symbols in multi-lingual environments. This is an international 16-bit data coding method which
represents 65536 different characters. It is enough to represent characters in any language, even Chinese and
hieroglyphics.

A problem arises when the computer retrieves a piece of data from its memory. Imagine that the data is
01000001. Is this the number 65, or is it A?
They are both stored in the same way, so how can it tell the difference?
The answer is that characters and numbers are stored in different memory locations so it knows which
one it is by knowing whereabouts it was stored.

Compiled By Kapondeni T. Page 5 | 25


Signed and Unsigned Numbers

A binary number may be positive or negative. In daily life we use symbols “+” and “-” to represent positive
and negative numbers, respectively. However, binary numbers use 0 (for positive) and 1 (for negative) in the
computer. An n-bit signed binary number consists of two parts – sign bit and magnitude. The left most bit
(Most Significant Bit (MSB)) is the sign bit. The remaining n-1 bits denote the magnitude of the number,
giving us a sign and magnitude as given below.

In an n-bit unsigned binary number, the magnitude of the number n is stored in n bits. An 8-bit unsigned
number can represent data in the range 0 to 255 (28= 256).

Sign and Magnitude representation


01100011 is a positive number since its sign bit is 0
11001011 is a negative number since its sign bit is 1.
An 8-bit signed number can represent data in the range -128 to +127 (-27 to +27-1).

Representation of Negative Numbers


Negative numbers are mostly represented using the complement of number. Complement of numbers can be in
1’s complement or 2’s complement. The complement of a number behaves like the negative of the original
number.

1’s Complement
One’s complement of a binary number is obtained by simply converting 1s to 0s and 0s to 1s. For example,
given the following four-bit binary number 10102, its 1’s complement becomes 01012. The alternating of bits
only applies to negative numbers, positive numbers do not change. For example
+6 = 000001102
-6 = 111110012
-6 is the complement (negative) of +6. Just convert 1s to 0s and 0s to 1s and thus -6 in 1’s complement.

Given the 1's complement we can find the magnitude of the number by taking it's 1's complement. The range
of numbers that can be represented in 1’s complement is found by the formula:
-(2n-1-1) to +(2n-1-1)
If the binary number has 8-bits (n=8). Thus the range of numbers will be from (-127) 100000002 to 011111112
(127)
Therefore the largest number that can be represented in 8-bit 1's complement is = 127. The smallest is -127.
However 1’s complement has a problem that it has two different representations (values) for zero, which are
000000002 and 111111112 both represent zero.

When adding binary numbers using 1’s complement, the carry bit is added back to the sum in the rightmost
position. There is no overflow as long as the magnitude of the result is not greater than 2n-1-1. We do not
throw away the carry bit.

2’S COMPLEMENT
Two’s complement of number is obtained by:
a) Positive numbers remain the same
b) Negative numbers: - Change the number to its 1’s complement.
- Add 1 to the result and the number will be in 2’s complement.

Compiled By Kapondeni T. Page 6 | 25


OR
- Rewrite the bits starting from the right hand side, all 0s take as they are at
their respective position and the first 1 value encountered. The rest alternate a
1 to 0 and a 0 to a 1 and your number will be in 2’s complement.
For example, in 2’s complement,
+6 = 000001102
-6 = 111110102
We can also find the magnitude the 2's complement number. The largest number that can be represented in 8-
bit 2s complement is 011111112 = 127. The smallest is 100000002 = -128. The formula used for range is
-(2n-1) to +(2n-1-1)

2’s Complement representation of 4-bit number

- Range = Lowest Negative =-8; Highest positive = +7


- Therefore range is from 1000 to 0111, it -8 to +7
- If number is added and the result is more than +7, there is an overflow.
- If the result is less than -8, it is an underflow.
- If the sign changes after addition, it’s an overflow.
- In general, overflow occurs if both numbers to be added have the same sign, otherwise no overflow
occurs.
• One way to detect overflow is to check the sign bit of the sum. If the sign bit of the sum does not match
the sign bit of x and y, then there's overflow. This only makes sense.
• Suppose x and y both have sign bits with value 1. That means both representations represent negative
numbers. If the sum has sign bit 0, then the result of adding two negative numbers has resulted in a
non-negative result, which is clearly wrong. Overflow has occurred.
• Suppose x and y both have sign bits with value 0. That means, both representations represent non-
negative numbers. If the sum has sign bit 1, then the result of adding two non-negative numbers has
resulted in a negative result, which is clearly wrong. Overflow has occurred.

Compiled By Kapondeni T. Page 7 | 25


This suggests that one way to detect overflow is to look at the sign bits of the two most significant bits and
compare it to the sum. Refer to diagrams below:

Converting From Binary Two's Complement To Denary

11111011
Step-1 00000100
Complement the number.
Step-2 -00000101
Add one add prefix a minus sign.
Step-3 -5
Convert binary to decimal.

When the addition of two values results in a carry, the carry bit is ignored and is thrown away. There is no
overflow as long as the magnitude is not greater than 2n-1-1 nor less than –(2n-1).
The two's-complement system has the advantage that the fundamental arithmetic operations of addition,
subtraction, and multiplication are identical to those for unsigned binary numbers (as long as the inputs are
represented in the same number of bits and any overflow beyond those bits is discarded from the result). This
property makes the system both simpler to implement and capable of easily handling higher precision
arithmetic. Also, zero has only a single representation, other than in ones'-complement where it has two
values.

Binary arithmetic
The arithmetic operations - addition, subtraction, multiplication and division, performed on the binary
numbers is called binary arithmetic. The basic arithmetic operations performed on the binary numbers are
 Binary conversion
 Binary Addition, and
 Binary Subtraction,

Binary Conversion
This involves converting a number in binary from to either denary (base 10), octal (base 8) or hexadecimal(
base 16)
(a) Conversion from decimal (denary) to Binary
Divide the denary number by 2, listing the remainders until the answer is 0 remainder 1.
Take the remainders only from the last one until the first.
For example:

Compiled By Kapondeni T. Page 8 | 25


The answer is therefore 101002

(b) Binary to decimal conversion


Raise each bit to its binary power equivalent, from right going to the left, starting at 20
Power 24 23 22 21 20
Equivalent to: 16 8 4 2 1
Binary Digits 1 0 1 0 0

Add all the equivalent to values in the table, whose binary digit correspond to 1 and add them. The
result is the denary equivalent.
That is 16 + 4 = 20. This is a short form of (16 x 1) + (4 x1) = 20

(c) Decimal to octal


 Octal number contains only digits from 0 to 7.
 As on binary, take the number, divide it by 8 and take the remainders only, e.g.
78 to octal will be expressed as:

= 1168

(d) Octal to decimal


Power 82 81 80
Equivalent to: 64 8 1
Octal Digits 1 1 6

= (64 x 1) + (8 x 1) + (1 x 6) = 78

(e) Decimal to hexadecimal


 Hexadecimal means base 16.
 A hexadecimal number contains numbers from 0 to 15.
 However, 10 to 15 are represented by uppercase alphabetic characters from A to F
respectively.

The table below illustrates this:


Decimal Number 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Hexadecimal Equivalent 0 1 2 3 4 5 6 7 8 9 A B C D E F

As on binary, take the number, divide it by 16 and take the remainders only, e.g.
Compiled By Kapondeni T. Page 9 | 25
209 to hexadecimal will be expressed as:

This gives us (13)1, but 13 is represented as D in our table above.


Thus the answer will be D116

(f) Hexadecimal to decimal

Power 161 160


Equivalent to: 16 1
Octal Digits 13 (D) 1

= (16 x 13) + (1 x 1) = 209

(g) Binary to Octal


The relationship between binary and octal numbers is that octal (8) is 2 3. Thus binary bits are put in
groups of 3, starting from the right going to the left.
If the last batch (to the left) does not have 3 bits, append 0s to its left in order to get 3 digits in each
batch.
Each group is then separately converted to its decimal equivalent, which will automatically be in base
8. For example, convert 1000111010101 to octal:
i. the groupings in 3s will be as follows: 001 000 111 010 101
ii. 001, converted to decimal is = 1
000 becomes 0
111 becomes 7
010 becomes 2
101 becomes 5

The answer therefore becomes 107258

(h) Octal to Binary


 Take each digit separately and then convert it to binary by dividing it by 2
 Put each binary result obtained into 3 bits, if not append 0s to the left
 Take the binary digits and join them into one binary number.

(i) Binary to hexadecimal


The relationship between binary and hexadecimal numbers is that hexadecimal (16) is 24. Thus binary
bits are put in groups of 4, starting from the right going to the left.
If the last batch (to the left) does not have 4 bits, append 0s to its left in order to get 4 digits in each
batch.
Each group is then separately converted to its decimal equivalent, which will automatically be in base
16.

(j) Hexadecimal to binary


Take each digit separately and then convert it to binary by dividing it by 2
Put each binary result obtained into 4 bits, if not append 0s to the left
Take the binary digits and join them into one binary number.

NB: Pupils should be able to add and subtract hexadecimal numbers, which were left out in this module.
Cognisance should be taken on carry if the answer after adding exceeds 16. Bear in mind also that the decimal
number 10, 11…15 and represented by letters A, B…F respectively.

Compiled By Kapondeni T. Page 10 | 25


Binary Addition
The table below illustrates procedure for binary addition, just like the addition of normal figures.

For 3 inputs, the table will be like:

Addition without a Carry

Addition with Carry

Compiled By Kapondeni T. Page 11 | 25


Binary subtraction
Subtraction of binary numbers follow the principles laid down on the following table:

Simple examples are given below:

Compiled By Kapondeni T. Page 12 | 25


Addition of signed numbers using 1’s complement
When adding numbers, the carry bit is added back to the answer.
1 0 0 0
+1 0 0 0
10 0 0 0
1
0 0 0 1

Octal Numbers (Base 8)

In the octal number system there are only eight different symbols.

Decimal Hexadecimal Binary (4-bit)


0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111

Converting Binary To Octal

Step-1
Divide the binary number into groups of three digits starting at 111 101
the LSB.
Step-2
7 5
Write down the hexadecimal equivalent for each group of digits.
111 1012 = 758
QNS
Express the denary number 78 as:
(i) a binary number stored in an 8 bit byte,
-Divide 78 by 2 and then take the remainders, to get 1001110. This answer has 7 bits but you are
required to give your answer in 8-bit. Add a 0 to the leftmost side of the block to produce 010011102
which is the correct answer.

Compiled By Kapondeni T. Page 13 | 25


(ii) a hexadecimal number,
Divide 78 by 16 and take the remainders. This gives 4 remainder 14, and 0 remainder 4. Take the
remainders only, 14 = E in base 16 and 4 remains the same. This gives the answer 4E16.
(iii) a number stored in binary coded decimal (BCD).
Split 78 into 2 separate digits 7 and 8. Convert each to binary separately. 7 give 111 and 8 gives us
1000. Express each binary form in 4 bits. 8 is already represented in 4 bits. Therefore add an extra bit
to the left of 7 to get 0111. So the answer is 01111000

Explain how the binary value of 78 can be used to write down the equivalent octal value with a
minimum amount of calculation [3]
- The binary value of 78 is 01001110.
- Put this binary number in groups of 3 starting from the right, and this gives us 001 001 110.
- Each group of 3 bit can be converted separately to denary or octal form

Convert -63 and -94 into 2’s complement, 8 bit, binary numbers.
- Convert the first number into its binary form by dividing it by 2 and take the remainders.
- If answer does not have 8 bits, add 0s to the left until the bits add up to 8.
- Convert it to one’s complement by converting 1s to 0s and vice versa.
- Add 1 to the result and the number will be in 2’s complement.
- Perform the above stages for the second number.
The results will be as follows:

-63 in to binary form give 111111, which has 6 bits instead of 8. A leading 0 is added to the left to
make them 8, thus giving us: 00111111. Change 0s to 1s and 1s to 0s, which gives us 11000000.
Add 1 to the number and will give us 11000001. Which is now in 2’s complement of 64 which is -
63.

-94 will give us the answer 10100010.

Add -63 and -94 into 2’s complement


11000001
10100010
1 01100011
Throw carry the carry bit. To get the final answer as 01100011. The extra 1 indicates overflow.

The result above indicates overflow as the result of adding two negative numbers cannot give a positive
answer. There was overflow from positive bits into negative bits. The processor will produce an error because
carry in to MSB is different from carry out.

Compiled By Kapondeni T. Page 14 | 25


Fixed point binary number system
The fixed point number representation assumes that the binary point is fixed at one position. The binary point
is not actually present in the register, but its presence is assumed based on whether the number which is stored
is a fraction or an integer. Thus fixed point numbers can either be fractional or integer.

Fixed Point integer representation

The decimal point in an integer is implied and does not change its position. The register used to store an
integer value will be as follows:

* * * * * * * *

There is no memory space for the decimal point. However computers represent a finite number of digits. This
limitation allows us to evaluate the maximum and minimum possible numbers that can be represented. These
include:

-Maximum Positive Number:-

Positive numbers start with a 0, thus 01111111 = +127

-Minimum Positive Number:-

This evaluates to 00000001 = +1

-Smallest Magnitude Negative Number:-

Negative numbers start with a 1. This gives us 11111111 = -1

-Largest Magnitude Negative Number:-

This is 10000000 = -128

The overall range of numbers here is -2n-1 to 2n-1-1

Compiled By Kapondeni T. Page 15 | 25


Positive Fixed Point fractional Binary Numbers

-Maximum Positive Number:-

This becomes 0.1111111 = 1- 1/128 = 0.9921875

-Minimum Positive Number:-

This evaluates to 0.0000001 = 1/27=0.0078125

-Smallest Magnitude Negative Number:-

Negative numbers start with a 1. This gives us 1.1111111 = -1/27=0.0078125

-Largest Magnitude Negative Number:-

This is 1.0000000 = -1

The decimal point is fixed at one position and therefore does not move. In binary we can have functional
column headings.

27 26 25 24 23 22 21 20 2-1 2-2
128 64 32 16 8 4 2 1 0.5 0.25
1 0 1 0 0 1 0 0 . 1 1
=164.75
Binary Fraction Fraction Decimal
0.1 1/2 0.5
0.01 1/4 0.25
0.001 1/8 0.125
0.0001 1/16 0.0625

4.5.2 Using Two's Complement With Fixed Point Binary Numbers

For negative numbers we use two's complement representation on the entire bit pattern.

Example

Represent -5.2510 in 8-bit binary with the binary point after the fourth digit.

Compiled By Kapondeni T. Page 16 | 25


Step-1
5 . 2 5
Calculate the positive equivalent number
in binary.
0 1 0 1 . 0 1 0 0
Step-2
Change 0s to 1s and 1s to 0s 1 0 1 0 . 1 0 1 1
(Complement).
Step-3
1 0 1 0 . 1 1 0 0
Add 1 to the result.

Advantages and Disadvantages Of The Fixed Point Binary System

 Preserves accuracy as required and is Easy to convert.

 However it has a limited degree of accuracy.

Floating point Arithmetic


A fixed point notation (e.g. two's complement) allows a range of positive and negative integers centred around
0 to be represented
● By assuming a fixed binary or radix point, this format would allow numbers with a fractional component to
be represented
● But – this approach has limitations – very large numbers or very small fractions cannot be represented
● Floating point representation allows us to represent very large numbers and very small fractions

The floating point number representation uses two registers. The first register stores the number without the
binary point. The second register stores a number that indicates the position of the binary point in the first
register.

In decimal notation the number 23.456 can be written as 0.23456 x 102. This means that we need only store, in
decimal notation, the numbers 0.23456 and 2. The number 0.23456 is called the mantissa and the number 2 is
called the exponent. This is what happens in binary.

Similarly, in decimal, 0.0000246 can be written 0.246 x 10-4. Now the mantissa is 0.246 and the exponent is –
4.

The floating point representation of a number has two parts: mantissa and exponent. The mantissa is a signed
fixed point number. The exponent shows the position of the binary point in the mantissa. For example, the
binary number +11001.11 with an 8-bit mantissa and 6-bit exponent is represented as follows -
• Mantissa is 01100111. The left most 0 indicates that the number is positive.
• Exponent is 000101. This is the binary equivalent of decimal number +5.
• The floating point number is Mantissa x 2exponent , i.e. + (.1100111) x 2+5.

Example: consider the binary number 10111. This could be represented by 0.10111 x 25 or 0.10111 x 2101.
Here 0.10111 is the mantissa and 101 is the exponent.
Thus, in binary, 0.00010101 can be written as 0.10101 x 2-11 and 0.10101 is the mantissa and –11 is the
exponent.

It is now clear that we need to be able to store two numbers, the mantissa and the exponent. This form of
representation is called floating point form. Numbers that involve a fractional part, like 2.46710 and 101.01012
are called real numbers.

Compiled By Kapondeni T. Page 17 | 25


Give the denary number which would have 01000000 00000000 as its binary, floating point
representation in this computer
The answer is 0.5 or ½ because it will be 0.1 x 20
It is not possible to represent zero as a normalised floating point number because a normalised value must
have the first two bits of the mantissa different. Therefore one must be a 1- which must represent either -1 or +
½, but not zero.

Weaknesses of Floating Point representation


- One number can have different representations, for example, 2 (010.0000000) can be represented as
0.100000000 x 22
0.010000000 x 23
0.001000000 x 24
- Causes errors as some less significant bits are lost, causing some unnecessary errors

Normalisation
This is done to simplify operations and expressing numbers in standard form.

Normalisation Principles:
Let us look at the following diagram

- A number is expressed in two main parts, which are:


 The mantissa (or fractional) part: this is always a fraction. The binary point is always between the
Sign bit and the MSB.
 The Exponent (or characteristic): it is always an integer (whole number) and can be positive of
negative depending on its sign bit. The left most bit of the exponent is a sign bit.
- The first two digits of the mantissa must be different in a normalised number. Thus:
 If the mantissa is Positive, the Sign Bit is always 0 and the MSB is always 1.
 If the mantissa is negative, the Sign Bit is always 1 and the MSB is always 0.
- For a negative number, there must be NO leading 1s to the left of the MSB, excluding the sign bit.

- For a positive number, there must be NO leading 0s to the left of the MSB, excluding the sign bit.

 With positive numbers, the binary point in the mantissa was always placed immediately before the
first non-zero digit because it allows us to use the maximum number of digits.
Suppose we use 8 bits to hold the mantissa and 8 bits to hold the exponent. The binary number 10.11011
becomes 0.1011011 x 210 and can be held as

The first digit of the mantissa is zero and the second is one. The mantissa is said to be normalised if the first
two digits are different. Thus, for a positive number, the first digit is always zero and the second is always one.
The exponent is always an integer and is held in two's complement form.

Compiled By Kapondeni T. Page 18 | 25


Now consider the binary number 0.00000101011 which is 0.101011 x 2-101. Thus the mantissa is 0.101011 and
the exponent is –101. Again, using 8 bits for the mantissa and 8 bits for the exponent, we have

Because the two's complement of –101, using 8 bits, is 11111011.


The reason for normalising the mantissa is in order to hold numbers to as high a degree of accuracy as
possible.

Care needs to be taken when normalising negative numbers. The easiest way to normalise negative numbers is
to first normalise the positive version of the number. Consider the binary number –1011. The positive version
is 1011 = 0.1011 x 2100 and can be represented by

Notice that the first two digits are different.

As another example, change the decimal fraction –11/32 into a normalised floating point binary number.
Leave the negative sign first and solve it for just 11/32, 11/32 = 1/4 + 1/16 + 1/32 = 0.01 + 0.0001 + 0.00001 =
0.01011; converted to binary equivalent.
Now we have 8 bits mantissa and 8 bits exponent, 00101100 00000000,
It is not normalized so normalize it by removing a 0 from location worth 1/2 in mantissa and subtracting that 1
location from exponent 0 reveals -1. That is, 01011000 11111111; this is Floating Point equivalent of 11/32.
For -11/32 keep the exponent same and in mantissa start from right side. Keep all digits same until first 1 and
toggle rest.

Benefits of Normalisation
- Ensures that a single representation of a number is maintained (standardisation).
- Ensures maximum possible accuracy with a given number of bit is maintained.
- Can be used to detect error conditions such as underflow and overflow
- Tires to maximise the range of numbers that can be represented in a fixed point representation (Range
and accuracy is limited in fixed point representation)

Range and Precision/ Accuracy and Range

● The size of the exponent determines the range of numbers that can be represented
 The range of numbers is expanded by increasing the number of bits that are used to represent the
exponent. This will however decrease precision.
Compiled By Kapondeni T. Page 19 | 25
 Reducing the number of bits in the exponent will reduce the range because power of two which the
mantissa is multiplying by is decreased.
 At the same time decreasing the exponent’s bits will increase accuracy because more digits are
represented after the binary point.

● The size of the significant determines the precision of the numbers that can be represented
 Precision can be increased by increasing the number of bits that are used to represent the significant
 This will decrease the range.
● The only way to increase both range and precision is to use more bits
 that is, the use of single-precision numbers, double-precision numbers, etc

If we use more bits for the mantissa we will have to use fewer bits for the exponent. Let us start off by using 8
bits for the mantissa and 8 bits for the exponent for explanations below:

 The largest positive value we can have for the mantissa is 0.1111111 and
 The largest positive number we can have for the exponent is 01111111.
 This means that we have 0.1111111 x 21111111 = 0.1111111 x 2127.
 This means that the largest positive number is almost 1 x 2127.

Also:
 The smallest positive mantissa is 0.1000000 and
 the smallest exponent is 10000000.
 This represents 0.1000000 x 210000000 = 0.1000000 x 2-128 which is very close to zero; in fact it is 2-129.
Compiled By Kapondeni T. Page 20 | 25
On the other hand:
 The largest negative number (i.e. the negative number closest to zero) is 1.0111111 x 2 10000000 = -
0.1000001 x 2-128
 We cannot use 1.1111111 for the mantissa because it is not normalised. The first two digits must be
different.

Furthermore:
 The smallest negative number (i.e. the negative number furthest from zero) is 1.0000000 x 2 01111111 = -
1.0000000 x 2127 = -2127.

Zero cannot be represented in normalised form. This is because 0.0000000 is not normalised because the first
two digits are the same. A normalised value must have the first two bits of the mantissa different. Therefore
one of them must be a 1 which must represent either -1 or + ½, but not zero. Usually, the computer uses the
smallest positive number to represent zero.

Also, size of number mean the furthest to the left on a number line so -1 is a bigger number than -2. Whereas,
if we talk about largest magnitude negative number then the -2 is greater magnitude than -1 because the
integer value is greater (not considering the sign).

For a positive number n,


2-129 ≤ n < 2127

For a negative number n


-2127 ≤ n < -2-129

Accuracy and Errors

Floating and fixed point numbers will be accurate to the smallest number they can represent.

To increase accuracy, either you can:


 Increase the number of bits used for the mantissa
 Then reduce the number of bits for the exponent
However, the range of numbers represented is reduced because the size of the index of the power of two is
reduced

Round-Off Errors

Often we cannot represent a denary fraction exactly even if we allow many bits in memory. Therefore the
number stored is "rounded off" to the closest possible binary equivalent.

In rounding, the least significant bit may be increased depending on digits removed. The result should
represent the value that is nearest to the original value, e.g

100.1 = 100 (2 s. f)
10101 = 1100 (2 .f)
11011 = 11000 (2 s.f)
11.101 = 100 (2 s.f)

1100 =1100 (3 s.f)


1101 = 1110 (3 s.f)
111.1101 = 1110.11 (6 s.f)
1110.1110 = 1111.00 (6 s.f)

Compiled By Kapondeni T. Page 21 | 25


Truncation Errors

Often, in either floating or fixed point systems, results are calculated with too many places of accuracy to be
represented. We get this type of error when trailing bits are truncated to fit the result in the memory location
available.

Truncation works as follows:

100.1 = 100 (2 s. f)
10101 = 10000 (2 .f)
11011 = 11000 (2 s.f)
11.101 = 11 (2 s.f)

Overflow

 It is a computational process produces a result so large that it cannot be represented.


 Overflow occurs when
 a number is divided by a small number or
 When two large numbers are multiplied together.

Underflow

 An underflow is produced when a result that is smaller in magnitude than the smallest number that can
be represented.
 It occurs when a small number is divided by a large number or
 When small numbers are multiplied together.

Overflow and underflow representation

(a) Integer representation

If the result is outside the possible range, then it is overflow.

(b) Floating Point representation

There exists the largest and smallest positive value. Around 0, there exists a range of values that
cannot be represented (stored) and this is underflow.

NB: Overflow and underflow occur when a result of calculations falls outside the range of values
permitted by the representation of the number.

Compiled By Kapondeni T. Page 22 | 25


Practice Questions
1. (a) Describe how characters are stored in a computer. (3)
b) Explain what is meant by an integer data type. (2)
c) State what is meant by Boolean data. (1)

2. a) Express the number 113 (denary) in


(i) binary
(ii) in BCD
using an appropriate number of bytes in each case. (4)
b) Using the answer obtained in part (a) show how 113 (denary) can be expressed in
(i) octal
(ii) hexadecimal. (4)

3. Explain how the denary number –27 can be represented in binary in


(i) sign and magnitude
(ii) two’s complement
notation, using a single byte for each answer. (4)

4. (a) Add together the binary equivalents of 34 and 83, using single byte arithmetic, showing you working. (3)
(b). Describe a floating point representation for real numbers using two bytes. (4)

5. a) Explain how the fraction part of a real number can be normalised. (2)
b) State the benefit obtained by storing real numbers using normalised form. (1)

6. a) A floating point number is represented in a certain computer system in a single 8 bit byte. 5 bits are used
for the mantissa and 3 bits for the exponent. Both are stored in two’s complement form and the mantissa is
normalised.
(i) State the smallest positive value,
(ii) state the most negative value
that can be stored. Give each answer as an 8 bit binary value and as a decimal equivalent. (4)
b) Explain the relationship between accuracy and range when storing floating point representations of real
numbers. (4)

7. (a) Express the denary number -95 as a two’s complement integer in an eight-bit byte. [2]
(b) Add together the following binary numbers. Show your working.
0 1 1 0 0 1 1 0 and 0 0 1 0 0 1 0 1 [2]

8. Part of the information stored in the data dictionary describes the type of data which is being stored.
A particular piece of data is 10010110.
State what the data stands for if the data dictionary describes it as:
(i) a two's complement binary number; [1]
(ii) a sign and magnitude binary number; [1]
(iii) a binary coded decimal number. [2]

9. Floating-point numbers in a particular computer system are stored using 12 bits. The first 6 bits are used for
the storage of the mantissa and the second set of 6 bits is used to store the exponent.
(a) One way to represent 6.5 as a floating-point number in this representation is 001101000100
Explain why this representation is equivalent to 6.5 [4]
(b) (i) Using the representation above to help you, write the number 6.5 as a floating-point number in
normalised form. [2]
(ii) Explain the effects of changing the representation so that 8 bits are used for the mantissa and 4 bits for the
exponent. [2]
(c) The numbers 011011001101 and 101100001110 are stored with 6 bits for the mantissa and 6 for the
exponent.
Add the exponents of the two floating-point numbers together. [2]

Compiled By Kapondeni T. Page 23 | 25


10. A computer stores floating point numbers using 2 eight-bit bytes.
The first byte is used to store the mantissa and the second stores the exponent.
(a) The normalised form of the floating point representation of 91/2 is 0100110000000100.
(i) Explain this representation of 91/2. [4]
(ii) Give the floating point value of 221/4 using this representation. [2]
(b) It is decided to change the representation by using 10 bits for the mantissa and 6 bits for the exponent.
Explain the effect of this decision on the range and accuracy of the data represented. [4]

11. (a) Express the denary number 94 as:


(i) a BCD value, [2]
(iii) a hexadecimal value. [2]
(b) (i) Work out the answer to the following binary addition sum. (All values are given in two's complement
form. You should show your working.)
01001101
00101011
01000101 +
__________ [2]
(ii) Explain why the binary result does not give the correct answer. [1]

12. (a) Show how the denary number –90 can be represented, using 8 bits, in:
(i) sign and magnitude,
(ii) two’s complement. [2]
(b) The denary number 10¾ is to be represented as a floating point binary number using 12 bits.
The first 8 bits are to be used for the mantissa and the remaining four bits are to be used for the exponent.
(i) Explain what is meant by the mantissa of a floating point number. [2]

(ii) Explain what is meant by the exponent of a floating point number. [2]
(iii) Show why 001010110101 is a floating point representation of 10¾.[3]
(iv) Normalise the floating point value given in (iii). [2]

13. (a) (i) Express the number 93 as an 8 bit binary number. [2]
(iii) Express the number 93 as a number in hexadecimal. [2]
(b) (ii) Describe the connection between binary representation and hexadecimal. [2]

14. A computer stores fractional numbers in floating point binary representation. Five bits are used for the
mantissa and three bits for the exponent. All values are stored in two’s complement form.

(a) By using a diagram of this representation, state the value of each of the bits. [4]
(b) By using 2 ½ as an example, explain how real numbers can be shown in normalised form in this
representation. [3]
(c) State the floating point binary value of - ¾ in this representation. [2]

15. A computer stores numbers in floating point form, using 8 bits for the mantissa and 8 bits for the exponent.
Both the mantissa and the exponent are stored in two’s complement form.
(a) Explain the effect on the
• range
• accuracy
of the numbers that can be stored if the number of bits in the exponent is reduced. [4]
(b) Give the denary number which would have 01000000 00000000 as its binary, floating point representation
in this computer. [2]
(c) Explain why it is not possible to represent zero as a normalised floating point number. [2]

Compiled By Kapondeni T. Page 24 | 25


16. (a) Express the decimal number 109 as
(i) a binary number stored in an 8 bit byte; [2]
(ii) a number in binary coded decimal (BCD); [2]
(iii) a hexadecimal number. [2]
(b) A particular computer stores numbers as 8 bit, two’s complement, binary numbers.
01011101 and 11010010 are two numbers stored in the computer.
(i) Write down the decimal equivalent of 11010010. [2]
(ii) Add the two binary values together and comment on your answer. [3]

17. (a) Express the denary number 78 as


(i) a binary number stored in an 8 bit byte,
(ii) a hexadecimal number,
(iii) a number stored in binary coded decimal (BCD). [6]
(c) (i) Convert -63 and -94 into 2’s complement, 8 bit, binary numbers. [2]
(ii) Add the binary values obtained in (i) together. [2]
(iii) Comment on the result that you obtained in (ii). [2]

18. (a) Express the denary value 109 as


(i) a binary number using an 8-bit byte;
(iii) a hexadecimal number. [4]
(b) Numbers are held in floating point form with one byte for the mantissa (fraction) and one byte for the
exponent (characteristic). All values are held in two’s complement form and the mantissa is normalised.
Using this format, write down the binary floating point values and the denary values of
(i) the largest magnitude, positive number;
(ii) the smallest magnitude, positive number;
(ii) the largest magnitude, negative number;
(iv) the smallest magnitude, negative number.
(The denary values may be left as a product of a power of 2). [8]
(c) Explain how accuracy can be improved in a floating point representation and state an effect it can have on
the number represented. [3]

19. (a) Represent


(i) +102,
(ii) +117
as 8-bit numbers in two’s complement form [2]
(b) (i) Add the answers in part (a) together to give a binary result. [2]
(ii) Turn your binary answer into an equivalent denary result. [2]
(iii) Explain the validity, or otherwise, of your result. [2]

Compiled By Kapondeni T. Page 25 | 25

You might also like