0% found this document useful (0 votes)
42 views128 pages

1.data Representation A Level

Uploaded by

Tanatswa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views128 pages

1.data Representation A Level

Uploaded by

Tanatswa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 128

DATA

REPRESENTATION
CONTENT TO BE COVERED IN FORM 5
ONLY.
TOPICS TO BE COVERED DURING THE
COURSE:
I.  NUMBER BASES
II. FLOATING POINT ARITHMETIC
III. COMPUTER ARITHMETIC
ERRORS
IV. DATA REPRESENTATION
I. NUMBER BASES
- Binary
- Octal
- Denary
- Hexadecimal
- 2s Compliment
BITS AND BINARY
Computers use binary - the digits 0 and 1 - to store
data. A binary digit, or bit, is the smallest unit of data
in computing. It is represented by a 0 or a 1. Binary
numbers are made up of binary digits (bits), e.g. the
binary number 1001.
The circuits in a computer's processor are made up of
billions of transistors. A transistor is a tiny switch that
is activated by the electronic signals it receives. The
digits 1 and 0 used in binary reflect the on and off
states of a transistor.
ENCODING
Everything on a computer is represented as streams
of binary numbers.
Audio, images and characters all look like binary
numbers in machine code. These numbers are
encoded in different data formats to give them
meaning, e.g. the 8-bit pattern 01000001 could be the
number 65, the character 'A', or a color in an image.
Encoding formats have been standardized to help
compatibility across different platforms. For example:
 audio is encoded as audio file formats, e.g. mp3, WAV,
AAC
 video is encoded as video file formats, e.g. MPEG4,
H264
 text is encoded in character sets, e.g. ASCII, Unicode
 images are encoded as file formats, e.g. BMP, JPEG,
PNG
BINARY AND DENARY
The binary system on computers uses combinations of 0s and 1s.
In everyday life, we use numbers based on combinations of the digits
between 0 and 9. This counting system is known as decimal, denary or
base 10.
A number base indicates how many digits are available within a
numerical system. Denary is known as base 10 because there are ten
choices of digits between 0 and 9. For binary numbers there are only
two possible digits available: 0 or 1. The binary system is also known
as base 2.
All denary numbers have a binary equivalent and it is possible to convert
between denary and binary.
1.PLACE VALUES DENARY PLACE
VALUES
Using the denary system, 6432 reads as six
thousand, four hundred and thirty two. One
way to break it down is as:
 six thousands
 four hundreds
 three tens
 two ones/units
Each number has a place value which could be put into columns. Each
column is a power of ten in the base 10 system:

Or think of it as:
(6 x 1000) + (4 x 100) + (3 x 10) + (2 x 1) = 6432
BINARY PLACE VALUES
You can also break a binary number down into place-value
columns, but each column is a power of two instead of a
power of ten.
For example, take a binary number like 1001. The
columns are arranged in multiples of 2 with the binary
number written below:
By looking at the place values, we can calculate the equivalent
denary number.
That is: (1 x 23) + (0 x 22) + (0 x21) + (1x20) = 8+0+0+1
(1 x 8) + (0 x 4) + (0 x 2) + (1 x 1) = 8 + 1 = 9
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)
(1) 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.
The answer is therefore 101002
(2) Binary to decimal conversion
Raise each bit to its binary power equivalent, from right going to the left,
starting at 20.
To calculate a large binary number like 10101000 we need more place values of
multiples of 2.
· 27 = 128
· 26 = 64
· 25 = 32
· 24 = 16
· 23 = 8
· 22 = 4
· 21 = 2
· 20 = 1
In denary the sum is calculated as:
(1x2 ) + (0 x 2 ) + (1 x 2 ) + (0 x 2 ) + (1 x
7 6 5 4

2 ) + (0 x 2 ) + (0 x2 ) + (0x2 ) = 168
3 2 1 0

(1 x 128) + (0 x 64) + (1 x 32) + (0 x 16) +


(1 x 8) + (0 x 4) + (0 x 2) + (0 x 1) = 128 +
32 + 8 = 168
(3) 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
(4) Octal to decimal
Power 82 8 1 80
Equivalent to: 64 8 1
Octal Digits 1 1 6
= (64 x 1) + (8 x 1) + (1 x 6) = 78
Converting Binary To Octal
The relationship between binary and octal numbers
is that octal (8) is 23. 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.
Converting Binary to Octal
Step-1 Divide the binary number into groups of
three digits starting at the LSB. 111 101
Step-2 Write down the decimal equivalent for each
group of digits.
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
(5) 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.
(5) 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 1 11 12 13 1 15
0 4
HEXADECIMAL NUMBER 0 1 2 3 4 5 6 7 8 9 A B C D E F
(6) 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.
(7) 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
As on binary, take the number, divide it by 16 and take the remainders only,
e.g.
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
Hexadecimal to decimal
Power 161 160
Equivalent to: 16 1
Hexadecimal Digits 13D 1
= (16 x 13) + (1 x 1) = 209
ADDITION OF BINARY
The binary number system uses two digits 0 and
1. There are four basic operations for binary
addition which are:
0+0=0
0+1=1
1+0=1
1+1=10
Consider the following example:
11101
+11011
11100
1+1=10=0 with a carry of 1
1+0+1=10=0 with a carry of 1
1+1+0=10=0 with a carry of 1
1+1+1=11 we write it as it is.
ANOTHER EXAMPLE
75 + 14 in denary: -128 64 32 16 8 4 2 1
75 = 0 1 0 0 1 0 1 1
14 = 0 0 0 0 1 1 1 0
0 1 0 1 1 0 0 1 = 89
Carry 1 1 1
BINARY SUBTRACTION
The subtraction of binary digit depends upon 4
basic operations which are:
0-0=0
1-0=1
1-1=0
10-1=1
NB: the above three operations are same as
decimal subtraction so they are easy.
Subtraction of Binary numbers We can use 2s
complement when doing subtraction of binary
numbers, (because 75 - 14 is the same as 75 + (-
14)). When we do subtraction of binary numbers,
we never do any subtraction; its all addition.
For example, 75 - 14
-128 64 32 16 8 4 2 1
75 = 0 1 0 0 1 0 1 1
14 = 1 1 1 1 0 0 1 0
0 0 1 1 1 1 0 1 = 61
Carry 1 1
Division of Binary
Division

1001ten Quotient
Divisor 1000ten | 1001010ten Dividend
-1000
10
101
1010
-1000
10ten Remainder
At every step,
• shift divisor right and compare it with current dividend
• if divisor is larger, shift 0 as the next bit of the quotient
• if divisor is smaller, subtract to get new dividend and shift 1 40

as the next bit of the quotient


Division

1001ten Quotient
Divisor 1000ten | 1001010ten Dividend

0001001010 0001001010 0000001010 0000001010


100000000000  0001000000 00001000000000001000
Quo: 0 000001 0000010 000001001

At every step,
• shift divisor right and compare it with current dividend
• if divisor is larger, shift 0 as the next bit of the quotient
• if divisor is smaller, subtract to get new dividend and shift 1 41

as the next bit of the quotient


Divide Example
• Divide 7ten (0000 0111two) by 2ten (0010two)

Iter Step Quot Divisor Remainder


0 Initial values
1

42
Divide Example
• Divide 7ten (0000 0111two) by 2ten (0010two)
Iter Step Quot Divisor Remainder
0 Initial values 0000 0010 0000 0000 0111
1 Rem = Rem – Div 0000 0010 0000 1110 0111
Rem < 0  +Div, shift 0 into Q 0000 0010 0000 0000 0111
Shift Div right 0000 0001 0000 0000 0111
2 Same steps as 1 0000 0001 0000 1111 0111
0000 0001 0000 0000 0111
0000 0000 1000 0000 0111
3 Same steps as 1 0000 0000 0100 0000 0111
4 Rem = Rem – Div 0000 0000 0100 0000 0011
Rem >= 0  shift 1 into Q 0001 0000 0100 0000 0011
Shift Div right 0001 0000 0010 0000 0011
5 Same steps as 4 0011 0000 0001 0000 0001 43
Multiplication of Binary
Binary multiplication
- is actually much simpler to calculate than decimal multiplication
because in binary multiplication, we only need to remember the
following,
0x0=0
0x1=0
1x0=0
1x1=1
*Note that since binary operates in base 2, the multiplication rules
we need to remember are those that involve 0 and 1 only.
As an example of binary multiplication we have 101 times
11,
101
X11
First we multiply 101 by 1, which produces 101. Then we
put a 0 as a placeholder as we would in decimal
multiplication, and multiply 101 by 1, which produces 101.
101
x11
101
1010 <-- the 0 here is the placeholder
The next step, as with decimal multiplication, is to
add. The results from our previous step indicates
that we must add 101 and 1010, the sum of which is
1111.
101
x11
101
+1010
1111
Explain how the binary value of 78 can be used to write down
the equivalent octal value with a minimum amount of
calculation.
- 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
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
- 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
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.
Example: Convert denary number 6.1875 to binary
= 6.1875
0110.0011 = 6.1875
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 63 which is -63.

- 94 will give us the answer 10100010.


Advantages and Disadvantages of the Fixed Point
Binary System
The advantage of fixed point notation is simple arithmetic
(same as integer arithmetic) and therefore faster
processing.
However, a disadvantage is the limited range, as
increasing the number of bits after the binary point for
precision decreases the range and vice versa
– very large numbers or very small fractions cannot be
represented
FLOATING POINT ARITHMETIC
Previous fixed point notation (e.g. two's complement)
allows a range of positive and negative integers
centered 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.
Floating point binary is used to represent fractions.
The model for a whole number byte: 128 64 32 16 8 4 2
1
Where each sub heading will have either a one or a zero
under it depending on what the value of the number.
Effectively there is a decimal point at the end of this number.
(e.g. 139 = 139. = 139.0)
Also each heading is the heading before divided by two.
If we had a two byte representation of a binary number, with
the second byte being the decimal point it would look like:
128 64 32 16 8 4 2 1 || 0.5 0.25 0.125 
And so on.
Another format is needed for holding very large numbers
In decimal, we can show very large numbers in scientific notation.
For example
1 200 000 000 000 can be written as 0.12 x 10 13
Here 0.12 is called the mantissa (or coefficient) and 13 is called
the exponent. The mantissa holds the digits and the exponent
defines where to place the decimal point. In example above the
point is moved 13 places to the right
The same technique can be used for binary numbers. For example,
two bytes (16 bits) might be divided into 10 bits for the mantissa
and 6 bits for the exponent.
The rules for converting binary floating point number
to decimal
 Place the point between the sign bit and the first digit of
the mantissa.
 Convert the exponent to its equivalent decimal form
(positive or negative)
 Move the point right if the exponent is positive, or left
if the exponent is negative, the appropriate number of
places.
 Convert the resulting binary number to denary.
Example: convert the following floating point binary number to
decimal with 10 bit mantissa and 8 bit exponent:
0 100000000 11111110
• 0.100000000 11111110
• 0.1 exponent – 2
• 0.001
Also if the exponent is negative (indicated by a 1 in its leftmost bit) the
binary point is moved left instead of right. You need to fill in with extra 1’s
as you shift the binary point to the left.
Example: convert the following floating point binary number to decimal
with 10 bit mantissa and 8 bit exponent:
1 100000000 11111110
 1.100000000 11111110
 1.1 exponent – 2
 1.111
Normalisation
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.
The rules for normalisation of binary floating point number
 Place the point between the sign bit and the first digit of the
mantissa.
 Convert the exponent to its equivalent decimal form (positive
or negative)
 Shift the point to the right or to the left to achieve a normalised
state, add the number of places moved to the left to the exponent
or subtract if the point was moved to the right.
 Convert the back the exponent to binary maintain the number
of bits.
 Maintain number of bits of the mantissa by adding 0s to the
right.
Example: Normalise the floating point binary
number: 0 000110101 000010
0 000110101 000010
 0.000110101 exponent 2
 0.110101 exponent 2 - 3
 0.110101 exponent - 1

0 110101000 111111 Normalised


Example: Normalise the floating point binary number: 1
111100100 000011
1 111100100 000011
 1.111100100 exponent 3
 1.00100 exponent 3 - 4
 1.00100 exponent - 1

1.001000000 111111 Normalised


Exercise:
SOLUTIONS
I. 0 000000110 000111
0.000000110 exponent =7
0.110 exponent = 7-6
0.110 exponent =1

0 110000000 000001(Normalised)
II. 0 000010111 000110
0.000010111 exponent =6
0.10111 exponent =6-4
0.10111 exponent =2

0 101110000 000010 (Normalised)


III. 1 111110111 000000
1.111110111 exponent = 0
0.111 exponent =0-5
0.111 exponent = (-5)

0 111000000 111011 (Normalised)


IV. 1 111111010 000011
1.111111010 exponent =3
1.010 exponent = 3 -6
1.010 exponent = (-3)

1 010000000 111101 (Normalised )


Normalisation of decimal numbers
Example: Normalise the decimal number 3.1875 using 8
bit for the mantissa and 4 bit for the exponent.
Example: Normalise the decimal number -3.1875 using 8 bit for the mantissa and 4 bit
for the exponent.
ADVANTAGES OF NORMALISED NUMBERS
 maximum precision for a given number of bits
 only one representation for each number
 Ensures that a single representation of a number is maintained
(standardisation).
 Ensures maximum possible accuracy with a given number of bits 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.
 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,
COMPUTER ARITHMETIC ERRORS
What are Overflow and underflow
Overflow
Occurs when calculations produce results exceeding the capacity of the
result.
Example:
16 bit integers can hold numbers in the range -32768 …32767. So what
happens when you add 2000 to 2000
The sixteenth bit contains a ’1’ as a result of adding the two
numbers. Yet, numbers with a ’1’ in the leading position are
interpreted as negative numbers, so instead of ‘4000’ the result is
interpreted as ‘25536’.
Overflow can also occur in the exponent of a floating point
number, when the exponent has become too large to be
represented using the given representation for floating-point
numbers (for example 7 bits for 32-bit integers, or exponents
larger than 63).
Underflow
A calculation resulting in a number so small that the
negative number used for the exponent is beyond the
number of bits used for exponents is called underflow
(example 7 bits for 32-bit integers, or exponents smaller
than -64)
The term arithmetic underflow ( or “floating point
underflow”, or just “underflow”) is a condition in a
computer program where the resultof a calculation is a
number of smaller absolute value than the computer can
actually store in memory.
Overflow
A CPU with a capacity of 8bits has a capacity of up to
11111111 in binary. If one more bit was added there
would be an overflow error
Example: 8-bit overflow
An example of an 8-bit overflow occurs in the binary sum
11111111 + 1 (denary 255 + 1)
Add -63 and -94 into 2’s complement
11000001
10100010
1 01100011
Throw away 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.
The total is a number bigger than 8 digits, and when this happens the CPU drops
overflow digit because the computer cannot store it anywhere, and the computer
thinks 255 + 1 = 0
Signed and Unsigned Numbers
A binary number may be positive or negative, and we use “+” and
“-” to represent positive and negative numbers. 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.
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 (-2 7 to
+27-1).
The magnitude of a number is its natural value, regardless
of the sign. Thus the magnitude of -25 and +25 is 25 (not
considering the sign in this situation). In binary form, 25 =
11001. Thus using Sign and Magnitude representation:
+25 = 0 11001
-25 = 1 11001
What only differ is the sign bit, not the magnitude.
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 2 n-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.
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.
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 Complement the number. 00000100
Step-2 Add one add prefix a minus sign. -0 0 0 0 0 1 0 1
Step-3 Convert binary to decimal. - 5
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).
Advantages of 2s compliment
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.
 DATA REPRESENTATION
Data type
A data type is a method of interpreting and representing data held in a
computer. Categories of data types include Intrinsic/built-in data types and
user defined datatypes.
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
Boolean
Real
Single
Char
Short
Long
etc
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 bell short and long integers
(more bytes are used to store long integers).
 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.
NB: The representation of integers in the computer is
as follows:
- Unique bits are used to store the binary representation of
the integer
- Leading zeros are used to complete the required number
of bits
- Standard number of bits are used irrespective of size of
integer
- Two’s complement used to represent negative integers
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.
 In the computer, Boolean data is stored in one byte – True being stored as
11111111 and False as 00000000.
String
 A string(or text) is a series of alphanumeric characters usually enclosed in
quotation marks.
 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
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.
Benefits of 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
DATA REPRESENTATION
The form of data representation is in its character set. All
the characters that a system can recognize, which often
equates to characters on the keyboard, is called its
character set. Character set (or data representation) can
be as follows:
Each character is represented using a unique set of bits
which are equivalent to 1 or 2 bytes. Character set of a
computer is represented as binary codes, ASCII,
UNICODE and EBCDIC using 7/8 bits.
1.American Standard Code for Information Interchange
(ASCII)
It is a set of codes that a computer understands and is represented in a
single byte of 7 or 8 bits per character, which allows communication
between systems. 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. 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.
 Codes 0 to 31 represent control characters (non-
printable),
 Codes 48 to 57 stand for numeric 0-9.
 Codes 65 to 90 stand for uppercase letters A-Z.
 Codes 97 to 122 stand for lowercase letters a-z.
 Codes 128-255 are the extended ASCII codes.
CONVERTING CHARACTERS TO BINARY:
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.
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. 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.
- 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 in BDC if
added will not produce the correct answer
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.

You might also like