0% found this document useful (0 votes)
7 views

Data Representation and Numbering System@chapter (2)

The document covers the fundamentals of basic computer programming, focusing on data representation, number systems, and coding methods. It explains how data is stored in binary format, the different character encoding systems like ASCII and Unicode, and the representation of sound and images in digital formats. Additionally, it details various number systems, including decimal, binary, octal, and hexadecimal, along with their applications in computing.

Uploaded by

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

Data Representation and Numbering System@chapter (2)

The document covers the fundamentals of basic computer programming, focusing on data representation, number systems, and coding methods. It explains how data is stored in binary format, the different character encoding systems like ASCII and Unicode, and the representation of sound and images in digital formats. Additionally, it details various number systems, including decimal, binary, octal, and hexadecimal, along with their applications in computing.

Uploaded by

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

BASIC COMPUTER PROGRAMMING

MARCH, 2024

BASIC COMPUTER PROGRAMMING 1


Contents
Data Representation

Concepts of number systems

Conversion from one Base to Another

Binary Arithmetic

Coding methods

Representation of negative numbers and arithmetic

BASIC COMPUTER PROGRAMMING 2


Data representation
Data representation in computers refers to the way data is stored and processed within a
computer system. It refers to the various ways in which data is stored and organized in a
computer system.

Computers use binary digits (bits) to represent data, where each bit can have a value of
0 or 1. These binary digits are then grouped into larger units to represent different types
of data.

A computer uses a fixed number of bits to represent a piece of data which could be a
number, a character, image, sound, video, etc.
BASIC COMPUTER PROGRAMMING 3
Cont…
•Binary numbers are used to represent data in computers because computers are
electronic devices that operate using two voltage levels, typically represented as 0 and 1.

•This binary system aligns well with the on/off nature of electronic switches within a
computer's circuitry. By using binary numbers, computers can efficiently store, process,
and transmit data in a way that is compatible with their electronic components. This
simplifies the design and operation of computer systems, making them more reliable and
efficient for handling complex tasks.

BASIC COMPUTER PROGRAMMING 4


Data Representation: Characters
Characters are the smallest readable unit in text, including alphabets, numbers, spaces,
symbols etc.
In computer systems, each character is represented by a unique binary code.
The system by which characters are converted to binary code is known as a character
set. Let’s see some of them

BASIC COMPUTER PROGRAMMING 5


ASCII
It stands American Standard Code for Information Interchange.

It is the scheme used to represent characters.

Each character is represented using 7-bit binary code.

If 8-bits are used, the first bit is always set to 0

Provides for upper- and lowercase English letters, numeric characters,

punctuation, special characters


BASIC COMPUTER PROGRAMMING 6
Cont…
Accommodates 128 (27) different characters

Represents each character in a standard character set as a single byte binary

code.

The standard code form that most PCs use to allow for communication between

systems.

BASIC COMPUTER PROGRAMMING 7


Cont…
•Since there are exactly 128 unique combinations of 7 bits, this 7-bit code can represent
only128 characters.

•Another version is ASCII-8, also called extended ASCII, which uses 8 bits for each
character, can represent 256 different characters.

•For example, the letter A is represented by 01000001, B by 01000010 and so on.


ASCII code is enough to represent all of the standard keyboard characters.

BASIC COMPUTER PROGRAMMING 8


EBCDIC

It stands for Extended Binary Coded Decimal Interchange Code. This is similar to ASCII
and is an 8-bit code used in computers manufactured by International Business Machines
(IBM).

It is capable of encoding 256 characters. If ASCII-coded data is to be used in a computer


that uses EBCDIC representation, it is necessary to transform ASCII code to EBCDIC code.

Similarly, if EBCDIC coded data is to be used in an ASCII computer, EBCDIC code has to
be transformed to ASCII.

BASIC COMPUTER PROGRAMMING 9


ISCH
ISCII stands for Indian Standard Code for Information Interchange or Indian
Script Code for Information Interchange. It is an encoding scheme for
representing various writing systems of India. ISCII uses 8-bits for data
representation.

It was evolved by a standardization committee under the Department of


Electronics during 1986-88 and adopted by the Bureau of Indian Standards
(BIS). Nowadays ISCII has been replaced by Unicode.
BASIC COMPUTER PROGRAMMING 10
Unicode
•Using 8-bit ASCII we can represent only 256 characters. This cannot represent all
characters of written languages of the world and other symbols. Unicode is developed to
resolve this problem. It aims to provide a standard character encoding scheme, which is
universal and efficient.

•It provides a unique number for every character, no matter what the language and platform
be. Unicode originally used 16 bits which can represent up to 65,536 characters. It is
maintained by a non-profit organization called the Unicode Consortium.


BASIC COMPUTER PROGRAMMING 11
Data Representation: Storing Sound
Sound is a form of energy that we perceive through our ears.
In computing, sound is represented digitally as binary data.
Sound can be stored in several formats, with the most common ones being
.wav, .mp3, .ogg, etc.
Analogue and Digital Sound
Analogue sound is how humans hear it in nature, continuous and fluid.
Computers, however, represent sound by collecting many small samples and
stitching them together, this is called digital sound.
BASIC COMPUTER PROGRAMMING 12
Cont…
Digital representation is done by a process known as sampling. It involves
measuring the sound wave’s intensity at fixed intervals.

The rate at which this measurement is done is called the sampling rate.

Higher sampling rate results in better sound quality but also requires more
space to store the data.

BASIC COMPUTER PROGRAMMING 13


Storing Sound
•Once the sound data is sampled, it is quantized and then encoded into binary to create
a digital sound file.
•In a sound file, each sampled sound is stored as a binary number representing the
amplitude of the sound wave at that moment of sampling.
•Larger binary numbers yield a better representation of the original sound, but also take up
more storage space.
•This space-saving versus quality trade-off is often managed by sound file compression.
Bit depth is the number of bits used for each audio sample.
Greater bit depth allows for a wider dynamic range and better sound quality.

BASIC COMPUTER PROGRAMMING 14


Data Representation: Storing Images
An image consists of a collection of pixels, each with their own coordinates
and color.

Each pixel in an image is represented by a binary code, which will determine


the color and intensity of that pixel.

The more pixels an image has, the higher the resolution – a high-resolution
image conveys more visual information than low-resolution images, but at a
higher data cost.
BASIC COMPUTER PROGRAMMING 15
Cont…
Image can be represented by a list of pixels.
Imagine all the rows of pixels on the screen laid out end to end in one long
row.
This gives the pixel list, and a pixel's location in the list corresponds to its
position on the screen.
A pixel's color is represented by a binary code, and consists of a certain
number of bits.
In a monochrome (black and white) image, only 1 bit is needed per pixel: 0
for black, 1 for white, for example. A 16 color image requires 4 bits per
pixel.
BASIC COMPUTER PROGRAMMING 16
Cont…
Video Representation: Videos combine image and audio data to create
moving pictures. Video formats like AVI, MP4, and MOV encapsulate both
video and audio streams, along with additional information such as
timestamps and metadata.

BASIC COMPUTER PROGRAMMING 17


Data Representation: Units
•Data units are terms we use to measure digital information, expressing the quantity of data
storage or transmission.

•Data is represented in binary form, with the bit being the basic unit of data. Each bit can
hold a value of either 0 or 1.

•Bit: A bit is the smallest unit of data in computing and digital communications.

•The term is a portmanteau of ‘binary’ and ‘digit’. Bit can have a value of either 0 or 1,
corresponding to the binary system of numbers.
BASIC COMPUTER PROGRAMMING 18
Cont…
oByte: A byte is a unit of digital information that consists of 8 bits. It is the most common
unit used to represent character data (for example, a letter, number, or punctuation mark).

With 8 bits, a byte can represent any of 256 possible values.

Kilobytes, Megabytes, Gigabytes, and Terabytes

Data is typically represented in larger units for ease of interpretation in our world of
increasing data sizes.

BASIC COMPUTER PROGRAMMING 19


Cont…
A kilobyte (KB) denotes approximately one thousand bytes (or exactly 1,024 bytes).

A megabyte (MB) denotes approximately one million bytes (or exactly 1,048,576
bytes).

A gigabyte (GB) denotes approximately one billion bytes, and a terabyte (TB), one
trillion bytes.

BASIC COMPUTER PROGRAMMING 20


Cont…
Larger Data Units

For very large data storage or data transfer rates, we use petabyte (PB), exabyte (EB), zettabyte
(ZB), and yottabyte (YB). Each of these is 1,000 or 1,024 times larger than the previous, depending
on whether decimal or binary units are being used.

Choosing the Appropriate Unit

The choice of unit to represent data often depends on the context.

For instance, the size of a small text file might be best expressed in kilobytes, while the capacity of a
hard drive may be given in terabytes. The average internet speed could be measured in megabytes per
second (Mbps), indicating the amount of data that can be transmitted each second.
BASIC COMPUTER PROGRAMMING 21
Number System
Number systems are the technique to represent numbers in the computer
system architecture, every value that you are saving or getting into/from computer memory
has a defined number system.

A number is a mathematical object used to count, label, and measure.

A number system has a unique base, which depends upon the number of symbols. The
number of symbols used in a number system is called the base or radix of a number system.

BASIC COMPUTER PROGRAMMING 22


Cont…
A Number system or numeral system is defined as an elementary system to express

numbers and figures. It is the unique way of representing of numbers in arithmetic and

algebraic structure.

The numeral system(Number system) Represents a useful set of numbers, reflects the

arithmetic and algebraic structure of a number, and Provides standard representation.

The digits from 0 to 9 can be used to form all the numbers. With these digits, anyone

can create infinite numbers. For example, 156,3907, 3456, 1298, 784859 etc

BASIC COMPUTER PROGRAMMING 23


Number and Its Types

Numbers used in mathematics are mostly decimal number systems. In the decimal number system, digits

used are from 0 to 9 and base 10 is used. There are many types of numbers in decimal number system,

below are some of the types of numbers mentioned,

Numbers that are represented on the right side of the zero are termed Positive Numbers. The value of these

numbers increases on moving towards the right. Positive numbers are used for Addition between numbers.

Example: 1, 2, 3, 4.

Numbers that are represented on the left side of the zero are termed Negative Numbers. The value of these

numbers decreases on moving towards the left. Negative numbers are used for Subtraction between

numbers. Example: -1, -2, -3, -4.


BASIC COMPUTER PROGRAMMING 24
Cont…
Natural Numbers are the most basic type of Numbers that range from 1 to infinity. These numbers are also

called Positive Numbers or Counting Numbers. Natural Numbers are represented by the symbol N.

Whole Numbers are basically the Natural Numbers, but they also include ‘zero’. Whole numbers are

represented by the symbol W.

Integers are the collection of Whole Numbers plus the negative values of the Natural Numbers. Integers do

not include fraction numbers i.e. they can’t be written in a/b form. The range of Integers is from the Infinity

at the Negative end and Infinity at the Positive end, including zero. Integers are represented by the symbol Z.

BASIC COMPUTER PROGRAMMING 25


Cont…
Rational numbers are the numbers that can be represented in the fraction form i.e. a/b. Here, a and b

both are integers and b≠0. All the fractions are rational numbers but not all the rational numbers are

fractions.

Irrational numbers are the numbers that can’t be represented in the form of fractions i.e. they can not

be written as a/b.

Numbers that do not have any factors other than 1 and the number itself are termed as Prime

Numbers. All the numbers other than Prime Numbers are termed as Composite Numbers except 0.

Zero is neither prime nor a composite number.

BASIC COMPUTER PROGRAMMING 26


Types of Number Systems
Based on the base value and the number of allowed digits, number systems are of many
types. The four common types of Number System are:

 Decimal Number System

Number system with a base value of 10 is termed a Decimal number system. It uses 10
digits i.e. 0-9 for the creation of numbers. Here, each digit in the number is at a specific
place with place value a product of different powers of 10. Here, the place value is termed
from right to left as first place value called units, second to the left as Tens, so on Hundreds,
Thousands, etc. Here, units have the place value as 100, tens have the place value as 101,
hundreds as 102, thousands as 103, and so on.

BASIC COMPUTER PROGRAMMING 27


Cont…
For example, 10264 has place values as,
(1 × 104) + (0 × 103) + (2 × 102) + (6 × 101) + (4 × 100)
= 1 × 10000 + 0 × 1000 + 2 × 100 + 6 × 10 + 4 × 1
= 10000 + 0 + 200 + 60 + 4
= 10264

BASIC COMPUTER PROGRAMMING 28


Cont…
Positional Notation: It is a system of expressing numbers in which the digits are
arranged in succession and, the position of each digit has a place value and the
number is equal to the sum of the products of each digit by its place value

Example:
◦ Consider the number 954:

◦ 9 * 102 + 5 * 101 + 4 * 100 = 954

BASIC COMPUTER PROGRAMMING 29


Binary Number System

Number System with base value 2 is termed as Binary number system. It uses 2 digits i.e.

0 and 1 for the creation of numbers. The numbers formed using these two digits are

termed Binary Numbers. The binary number system is very useful in electronic devices

and computer systems because it can be easily performed using just two states ON and

OFF i.e. 0 and 1.

Decimal Numbers 0-9 are represented in binary as: 0, 1, 10, 11, 100, 101, 110, 111, 1000,

and 1001. For example, 14 can be written as 1110, 19 can be written as 10011, 50 can be

written as 110010.
BASIC COMPUTER PROGRAMMING 30
Cont…

Example of 19 in the binary system


Here 19 can be written as 10011

BASIC COMPUTER PROGRAMMING 31


Octal Number System
Octal Number System is one in which the base value is 8. It uses 8 digits i.e. 0-7 for the
creation of Octal Numbers.

Octal Numbers can be converted to Decimal values by multiplying each digit with the place
value and then adding the result.

Octal Numbers are useful for the representation of UTF8 Numbers. Example,

(135)10 can be written as (207)8

BASIC COMPUTER PROGRAMMING 32


Hexadecimal Number System

Number System with base value 16 is termed as Hexadecimal Number System. It uses 16
digits for the creation of its numbers.

Digits from 0-9 are taken like the digits in the decimal number system but the digits from 10-
15 are represented as A-F i.e. 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and
15 as F. Hexadecimal Numbers are useful for handling memory address locations.

The hexadecimal number system provides a condensed way of representing large binary
numbers stored and processed. Examples,

BASIC COMPUTER PROGRAMMING 33


Cont…
(255)10 can be written as (FF)16
(1096)10 can be written as (448)16
(4090)10 can be written as (FFA)16

BASIC COMPUTER PROGRAMMING 34


Conversions Between Number Systems
Binary to Decimal Conversion
The binary number can be converted to a decimal number by expressing each digit as a
product of the given number 1 or 0 to the respective power of 2. Let us understand this
using an example.

We can convert 10101 to the decimal number form in the following way:

The binary number 10101 is expressed as (10101)2 = (1 × 24) + (0 × 23) + (1 × 22) + (0 ×


21) + (1 × 20) = (21)10. Thus, the binary number 10101 is expressed as (21)10.

BASIC COMPUTER PROGRAMMING 35


Conversions Between Number Systems
Any Base in to Decimal
Base B ⇒ Base 10 Conversion

Converting any base into decimal


 Multiply each bit in the base with the weight (or position)
 Add up all the results of the multiplication performed
 The desired decimal number is the total of the multiplication results performed

Example: 1F216= ?10

Number to be converted: N16=1F2 ; target base B=10

1F216 = 1 · 162+15 · 161+2 · 160

= 2 + 240 + 256

=498

BASIC COMPUTER PROGRAMMING 36


Octal to decimal
Example: 1628 = ?10

Number to be converted: N8=162 ; target base B=10

1628 = 1 · 82+6 · 81+2 · 80

= 2 + 48 + 64

=114

BASIC COMPUTER PROGRAMMING 37


Decimal to Binary Conversion
The decimal number can be converted to a binary number by dividing the given
number by 2 until we obtain the quotient as 1. The are written from downwards to
upwards. Let us numbers understand this using an example. We can convert 30 to a

binary number form in the following way: The decimal number 30 is expressed

as (30)10 = (11110)2.

BASIC COMPUTER PROGRAMMING 38


Decimal to Hexadecimal

 Decimal to hexadecimal is a system of conversion that is often used in


computers and digital systems. The decimal number system has a base of 10.
It has only 10 notations, i.e., 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
 Whereas the hexadecimal system operates with a base of 16 because there are
a total of 16 notations in it: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and A, B, C, D, E, F. To
denote the double digits 10, 11, 12, 13, 14, 15 in the hexadecimal number
system, we use characters A, B, C, D, E, F respectively.
BASIC COMPUTER PROGRAMMING 39
Decimal to Hexadecimal Conversion?
Decimal to hexadecimal conversion is the process of converting a decimal number with a
base of 10 to a hexadecimal number with a base of 16.

While converting a number from the decimal number system to the hexadecimal number
system, we need to carefully observe the base of the number.

The number needs to be divided by 16 until the quotient is zero. Observe the following
table which shows the representation of decimal numbers and hexadecimal numbers.

BASIC COMPUTER PROGRAMMING 40


How to Convert Decimal to Hexadecimal?

Step 1: Divide the given decimal number system value by 16 and note
the remainder.
Step 2: Divide the quotient by 16. Repeat this until you get a quotient equal
to zero.
Step 3: Use the characters A, B, C, D, E, F in place of 10, 11, 12, 13, 14, 15
in the remainders respectively, wherever needed.
Step 4: Follow the reverse order pattern to arrange all the values of the
remainder.
Step 5: The obtained number is the required hexadecimal number.

BASIC COMPUTER PROGRAMMING 41


Cont…

The Decimal to hexadecimal conversion formula of given numbers can


be expressed as,
P10 = Q16
where P is a decimal number and Q is a hexadecimal number.
Let us understand how to convert a decimal number to a hexadecimal
number with the help of the following example.
Example: Convert 5386 to a hexadecimal number.

BASIC COMPUTER PROGRAMMING 42


Cont…

BASIC COMPUTER PROGRAMMING 43


Cont…
Example: 102010= ?16
Number to be converted: N10=1020 ; target base B=16
Step 1 1020 : 16= 63 Remainder: 12
Step 2 63 :16 = 3 Remainder:15
Step 3 3 : 16 = 0 Remainder:3
102010= 3FC

BASIC COMPUTER PROGRAMMING 44


Decimal to ocal
Example: 102010= ?8
Number to be converted: N10=1020 ; target base B=8
Step 1 1020 / 8 = 127 Remainder: 4
Step 2 127 / 8 = 15 Remainder:7
Step 3 15 / 8 = 1 Remainder:7
Step 4 1 / 8 = 0 Remainder:1
102010= 17748

BASIC COMPUTER PROGRAMMING 45


Binary, Octal, & Hexadecimal Conversions

• Octal (or Hexadecimal) to Binary:


– Express each octal (hexadecimal) digit as three (four) binary bits respectively
starting at the radix point and going both ways.
• Binary to Octal (or Hexadecimal):
– Group the binary bits into three (four) bit groups starting at the radix point and
going both ways, padding left hand zeros in the integer part and right hand
zeros in the fractional part as needed.
– Replace each group of three (four) bits with the equivalent octal (hexadecimal)
digit
• Octal Hexadecimal
– Go through binary as an intermediate step (use above)
e.g. Octal  Binary  Hexadecimal
BASIC COMPUTER PROGRAMMING 46
Examples: Octal (Hexadecimal) Binary
• Example: Hexadecimal to Binary
(F 9 C . B 7 D)16
= 1111 1001 1100. 1011 0111 1101
= (111110011100.101101111101)2
Always start at the radix point
• Example: Binary to Octal Working your way away from it
in both directions
(1011010110.0101111)2
= (1011010110.0101111)2
Appended 0’s
= (001011010110.010111100)2
= (1 3 2 6 . 2 7 4 )8
BASIC COMPUTER PROGRAMMING
Chapter 1 47
Example: Octal to Hexadecimal via Binary

1. Convert octal to binary


2. Use groups of four binary bits and express them as hexadecimal digits
• Example: Octal  Binary  Hexadecimal
(6 3 5 . 1 7 5)8
110 011 101. 001 111 101
000110011101. 001111101000
= (1 9 D . 3 E 8)16
Represent Octal in binary
Group into 3 bit groups for both the integer and fraction parts, starting at the radix point
Append leading 0’s to the left of integer part and trailing 0’s to the right of the fraction part as
needed.
Express each group of 4 bits in hex .
BASIC COMPUTER PROGRAMMING 48
Converting fraction from decimal to base b:

 Repeatedly multiply the integer by the new base b and save the integer part of the
result (will never exceed b) until 0 fraction or enough digits
 The digits for the new base are those integers with the MS digit being that computed
first
(0.6875)10 = (?)2
 radix point

Fraction in new base


Continue until
you get either:
 Zero fraction, or
 Enough # of
 Significant fraction
BASIC COMPUTER digits
PROGRAMMING
Chapter 1 49
Example: Convert 153.51310 to Base 8, rounding the
resulting fraction to 3 octal digits

1. Convert Integer Part: 153 to Base 8



2. Convert Fraction Part: 0.513 to Base 8:

Get 4 octal digits in the
Fraction for rounding to
3 significant digits

(0.513)10 = (0.4065)8  (0.407)8 after rounding, since 5 is >4 we add 1 to fraction


 BASIC COMPUTER PROGRAMMING
So: 153.51310 = Chapter
231.407 1 8 50
Exercise 1
1. Binary  decimal
a. 001100
b. 11100.011
2. Decimal  binary
a. 145
b. 34.75
3. Octal  hexadecimal
a. 56558
4. Decimal  hexadecimal
a. 156
b. 54.75
5. Decimal  octal
a. 145
b. 24.75
6. octal decimal and binary
a. 15
b. 34.75
7. hexadecimal binary and decimal
a. 145
b. 34.75

BASIC COMPUTER PROGRAMMING 51


Representation of integer and floating point numbers in
binary
Integer Representation
-1101.01012 = -13.312510
Computer storage & processing  do not have benefit of minus signs (-) and
periods.
 Need to represent the integer 

BASIC COMPUTER PROGRAMMING 52


Signed Integer Representation

Signed integers are usually used by programmers

Unsigned integers are used for addressing purposes in the computer (especially for
assembly language programmers)

Three representations of signed integers:

1. Sign-and-Magnitude

2. Ones Complement

3. Twos Complement

BASIC COMPUTER PROGRAMMING 53


1. Sign-and-Magnitude

The easiest representation.

The leftmost bit in the binary number represents the sign of the number.
 0 if positive and

 1 if negative

The balance bits represent the magnitude of the number.

BASIC COMPUTER PROGRAMMING 54


Examples:

i) 8 bits binary number


__ __ __ __ __ __ __ __

Sign bit
7 bits for magnitude (value)
0 => +ve 1 => –ve

a) +7 = 0 0 0 0 0 1 1 1
(–7 = 100001112)
b) –10 = 1 0 0 0 1 0 1 0
(+10 = 000010102)

BASIC COMPUTER PROGRAMMING 55


2. Ones Complement
• In the ones complement representation, positive numbers are the same as that
of sign-and-magnitude

Example: +5 = 00000101 (8 bit)

• Sign-and-magnitude and ones complement use the same representation above


for +5 with 8 bits and all positive numbers.

• For negative numbers, their representation are obtained by changing bit 0 → 1


and 1 → 0 from their positive numbers

BASIC COMPUTER PROGRAMMING 56


 Convert –5 into ones complement representation (8 bit)

Solution:

 First, obtain +5 representation in 8 bits  00000101

 Change every bit in the number from 0 to 1 and vice-versa.

–510 in ones complement is 111110102

BASIC COMPUTER PROGRAMMING 57


Exercise:

Get the representation of ones


complement (6 bit) for the following
numbers:

i) +710 ii) –1010

Solution: Solution:

(+7) = 0001112 (+10)10 = 0010102

So,
(-10)10 = 1101012

BASIC COMPUTER PROGRAMMING 58


3. Twos complement

• Similar to ones complement, its positive number is same as sign-and-


magnitude

• Representation of its negative number is obtained by adding 1 to the ones


complement of the number.

BASIC COMPUTER PROGRAMMING 59


Example:

 Convert –5 into twos complement representation and give the answer in 8 bits.
Solution:
 First, obtain +5 representation in 8 bits  000001012
 Obtain ones complement for –5
 111110102
 Add 1 to the ones complement number:
 111110102 + 12 = 111110112
 –5 in twos complement is 111110112

BASIC COMPUTER PROGRAMMING 60


Exercise:

• Obtain representation of twos complement (6 bit) for the following


numbers
i) +710 ii)–1010
Solution:
Solution:
(+10) 10 = 0010102
(+7) = 0001112
(-10) 10 = 1101012 + 12
(same as sign-magnitude)
= 1101102
So, twos compliment
for –10 is 1101102

BASIC COMPUTER PROGRAMMING 61


Floating point representation
• A range of very large and very small numbers can be represented with only a few
digits by using scientific notation. For example:
– 976,000,000,000,000 = 9.76 * 1014
– 0.0000000000000976 = 9.76 * 10-14
• This representation saves space and reduces redundancy.
• This same approach can be used for binary numbers.
• A number represented by M*B±E can be stored in a binary word with three fields:
 Mantissa
 Exponent E, and
 B is base is implicit and need not be stored because it is the same for all numbers
(base-2).
BASIC COMPUTER PROGRAMMING 62
Floating Point

-15900000000000000
could be represented as

Mantissa Base

Sign - 159 * 1014 Exponent


- 15.9 * 1015
- 1.59 * 1016
BASIC COMPUTER PROGRAMMING 63
Example
• Convert the following number;37.75 into floating point
format to fit in 32 bit register.
 Convert the number from decimal into binary
100101.11
 Normalize all digits including the fraction to determine the
exponent.
1.0010111 x 25

0 0 0 0 0 0 1 0 1 1 0 0 1 0 1 1 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0

sign Significant
sign EXP
1bit 23bit
BASIC COMPUTER PROGRAMMING 64
IEEE 754
 Standard for floating point storage

 32 and 64 bit standards

 8 and 11 bit exponent respectively

 Extended formats (both mantissa and exponent) for intermediate results

BASIC COMPUTER PROGRAMMING 65


IEEE 754 Formats

BASIC COMPUTER PROGRAMMING 66


Typical 32-bit Floating Point Format

Exponent Mantissa
8 bits 24 bits

• First 8 bits contain the exponent


• The remaining 24 bits contain the mantissa
it also called significand
• The more bits we use for the exponent, the larger the range of
numbers available, but at the expense of precision. We still only have
a total of 232 numbers that can be represented.

BASIC COMPUTER PROGRAMMING 67


Cont..
• How to represent the binary number
11010.11011011101
This has to be converted to the form S* M*B±E
. 1101011011011101 Mantissa

The point has been moved 5 placed so exponent +5

.1101011011011101 x 2 101

Only the mantissa and the exponent need to be stored to represent this number

Note: this assumes that all numbers are positive.

BASIC COMPUTER PROGRAMMING 68


Cont..
Converting a decimal number to a floating point number.
1.Take the integer part of the number and generate the binary equivalent.

2.Take the fractional part and generate a binary fraction

3.Then place the two parts together and normalise.

BASIC COMPUTER PROGRAMMING 69


IEEE Biased 127 Exponent
To generate a biased 127 exponent
• Take the value of the signed exponent and add 127.

Example.

216 then 2127+16 = 2143 and my value for the exponent would
be 143 = 100011112

BASIC COMPUTER PROGRAMMING 70


Cont.…
Example:
Convert 6.75 to 32 bit IEEE format.
1.The Mantissa. The Integer first.
6/2 =3r0
3/2 =1r1
1/2 =0r1
2. Fraction next.
.75 * 2 = 1.5
.5 * 2 = 1.0
3. put the two parts together… 110.11
Now normalise 1.1011 * 22

BASIC COMPUTER PROGRAMMING 71


Cont..
Now normalise 1.1011 * 22
Exponent is 2+127 =129 or 10000001 in binary.

NOTE: Mantissa always ends up with a value of ‘1’ before the


Dot. This is a waste of storage therefore it is implied but not
actually stored. 1.1000 is stored .1000

6.75 in 32 bit floating point IEEE representation:-

0 10000001 10110000000000000000000

sign(1) exponent(8) mantissa(23)

BASIC COMPUTER PROGRAMMING 72


Example (2)
1.which number does the following IEEE single precision
notation represent?
1 1000 0000 0100 0000 0000 0000 0000 000

• The sign bit is 1, hence it is a negative number.


• The exponent is 1000 0000 = 12810
• It is biased by 127, hence the real exponent is
128 –127 = 1.
• The mantissa: 0100 0000 0000 0000 0000 000.
• It is normalised, hence the true mantissa is
1.01 = 1.2510
• Finally, the number represented is: -1.25 x 21 = -2.50
BASIC COMPUTER PROGRAMMING 73
Binary Arithmetic
010111
1. Addition ( + ) 011110 +
0+0=0 110101
0+1=1
1+0=1
1 + 1 = 10
1 + 1 + 1 = (1 + 1) + 1 = 10 + 1 = 112

Example:
i. 0101112 + 0111102 = 1101012
ii. 1000112 + 0111002 = 1111112

BASIC COMPUTER PROGRAMMING 74


2. Multiplication ( x )
0 x 0 = 0
0 x 1 = 0
1 x 0 = 0
1 x 1 = 1

3. Subtraction ( – )
0 – 0 = 0
0 – 1 = 1 (borrow 1)
1 – 0 = 1
1 – 1 = 0

BASIC COMPUTER PROGRAMMING 75


Cont..
010111
4. Division ( / )
001110 -
0 / 1 = 0
001001
1 / 1 = 1

Example:
i. 0101112 - 0011102 = 0010012

ii. 1000112 - 0111002 = 0001112

BASIC COMPUTER PROGRAMMING 76


Cont..
Multiplication
1101
 1011
1101
11010
 1101000
10001111

BASIC COMPUTER PROGRAMMING 77


Cont..
Exercise:
i. 1000100 – 010010
ii. 1010100 + 1100
iii. 110100 – 1001
iv. 11001 x 11
v. 110111 + 001101
vi. 111000 + 1100110
vii. 110100 x 10
viii. 11001 - 1110

BASIC COMPUTER PROGRAMMING 78


Arithmetic Operations for Ones Complement, Twos
Complement, sign-and-magnitude and floating point
number

Addition and subtraction for signed integers

Reminder: All subtraction operations will be changed into


addition operations

Example: 8 – 5 = 8 + (–5)

–10 + 2 = (–10) + 2

6 – (–3) = 6 + 3

BASIC COMPUTER PROGRAMMING 79


Sign-and-Magnitude
Z=X+Y
There are a few possibilities:
i. If both numbers, X and Y are positive
 Just perform the addition operation

Example:
510 + 310 = 0001012 + 0000112
= 0010002
= 810

BASIC COMPUTER PROGRAMMING 80


ii. If both numbers are negative
o Add |X| and |Y| and set the sign bit = 1
to the result, Z
Example: –310 – 410 = (–3) + (–4)
= 1000112 + 1001002
Only add the magnitude, i.e.:
000112 + 001002 = 001112
Set the sign bit of the result
(Z) to 1 (–ve)
= 1001112
= –710

BASIC COMPUTER PROGRAMMING 81


iii. If signs of both number differ
 There will be 2 cases:

a) | +ve Number | > | –ve Number |


Example: (–2) + (+4), (+5) + (–3)
– Set the sign bit of the –ve
number to 0 (+ve), so that both
numbers become +ve.
– Subtract the number of smaller
magnitude from the number with a
bigger magnitude

BASIC COMPUTER PROGRAMMING 82


Sample solution:
Change the sign bit of the –ve number to
+ve
(–2) + (+4) = 1000102 + 0001002
= 0001002 – 0000102
= 0000102 = 210

b) | –ve Number | > | +ve Number |


– Subtract the +ve number from the –ve number
Example: (+310) + (–510)
= 0000112 + 1001012
= 1001012 – 0000112
= 1000102
= –210

BASIC COMPUTER PROGRAMMING 83


Ones complement

 In ones complement, it is easier than sign-and-magnitude

 Change the numbers to its representation and perform the


addition operation

 However a situation called Overflow might occur when addition


is performed on the following categories:

1. If both are negative numbers

2. If both are in difference sign and

|+ve Number| > | –ve Number|

BASIC COMPUTER PROGRAMMING 84


Overflow => the addition result exceeds the number of bits that
was fixed.

1. Both are –ve numbers


Example: –310 – 410 = (–310) + (–410)
Solution:
–Convert –310 and –410 into ones
complement representation
+310 = 000000112 (8 bits)
–310 = 111111002
+410 = 000001002 (8 bits)
–410 = 111110112

BASIC COMPUTER PROGRAMMING 85


• Perform the addition operation
(–310) => 11111100 (8 bit)
+(–410) => 11111011 (8 bit)
–710 111110111 (9 bit)

Overflow occurs. This value is called EAC and needs to be


added to the rightmost bit.

11110111
+ 1
111110002 = –710

the answer

BASIC COMPUTER PROGRAMMING 86


2. | +ve Number| > |–ve Number|
• This case will also cause an
overflow
Example: (–2) + 4 = (–2) + (+4)
Solution:
• Change both of the numbers above
into one’s complement
representation
–2 = 111111012 +4 = 000001002
• Add both of the numbers
(–210) => 11111101 (8 bit)
+ (+410) => 00000100 (8 bit)
+210 100000001 (9 bit)
There is an EAC
BASIC COMPUTER PROGRAMMING 87
• Add the EAC to the rightmost bit
00000001
+ 1

000000102 = +210
the answer

Note:
For cases other than 1 & 2 above, overflow does not occur
and there will be no EAC and the need to perform addition to
the rightmost bit does not arise

BASIC COMPUTER PROGRAMMING 88


Twos Complement

Addition operation in twos complement is same with that of


ones complement, i.e. overflow occurs if:

1. If both are negative numbers

2. If both are in difference and |+ve Number| > |–ve


Number|

BASIC COMPUTER PROGRAMMING 89


Both numbers are –ve
Example: –310 – 410 = (–310) + (–410)
Solution:
• Convert both numbers into twos
complement representation
+310 = 0000112 (6 bit)
–310 = 1111002 (one’s complement)
–310 = 1111012 (two’s complement)

–410 = 1110112 (one’s complement)


–410 = 1111002 (two’s complement)

BASIC COMPUTER PROGRAMMING 90


 Perform addition operation on both the numbers in twos
complement representation and ignore the EAC.
111101 (–310)
111100 (–410)
1111001
Ignore the The answer
EAC
= 1110012 (two’s complement)
= –710

Note:
In two’s complement, EAC is ignored (do not need to be
added to the leftmost bit, like that of one’s complement)
BASIC COMPUTER PROGRAMMING 91
2. | +ve Number| > |–ve Number|
Example: (–2) + 4 = (–2) + (+4)
Solution:
• Change both of the numbers above
into twos complement representation
–2 = 1111102 +4 = 0001002
• Perform addition operation on both
numbers
(–210) => 111110 (6 bit)
+ (+410) => 000100 (6 bit)
+210 1000010
Ignore the EAC

BASIC COMPUTER PROGRAMMING 92


BASIC COMPUTER PROGRAMMING 93

You might also like