You are on page 1of 108

CSE‐101/102‐ PROBLEM SOLVING 

USING COMPUTERS (PSUC)
Introduction to computers
Algorithms &Flowcharts
C++ Fundamentals
Operators and Expressions
Flow of control
Derived Data types
Functions
Structures
Pointers
File Manipulation
Text Books:
 E. Balaguruswamy, “Computing Fundamentals & C Programming”,
Tata McGraw Hill, 2008.
 E. Balaguruswamy, “Object Oriented Programming with C++”, Tata
McGraw Hill, 2nd Edition 2007.
References:
 Kernighan and Ritchie,” ANSI C Language”, Prentice Hall of India, New
Delhi, 1992.
 Yashavant Kanetkar, “Let Us C”, 10th Edition, BPB Publications, 2010.
 Byron S. Gottfried, “Programming with C”, Schaum's Outline Series,
2nd Edition 1996.
 Robert Lafore, “Object Oriented Programming with Turbo C++”,
Gagotia Publications, 2002.
 Ellis Horowitz, Sartaj Sahni, Sanguthevar Rajasekaran, “Computer
Algorithms”, Galgotia Publications, 2001.
 Slides and books of Infosys Campus Connect – Foundation Program.
7/23/2012 Department of CSE 2
Mode of evaluation – Theory 

 Internal
• Sum of two Sessional tests
20 +20  = 40 marks
• Sum of best two Assignments/quizzes/open book tests
5 + 5  = 10 marks
Sub Total  = 50 marks
 External
• Semester Exam = 50 marks
Total  = 100 marks

7/23/2012 Department of CSE 3


Mode of evaluation – Laboratory 
• Continuous evaluation  = 60marks
[Biweekly evaluation of 10 marks
(Observation Book =5 marks,
Execution =3 marks,
viva =2 marks)
for 6 evaluations=10*6=60 marks]

• End Semester Exam  = 40 marks
(Write‐up =15 marks, 
Execution =25 marks)

Total  = 100 marks
If a student is absent on a particular week, he/she will loose 50% of
execution marks.

7/23/2012 Department of CSE 4


Introduction to Computers

What is a computer?

• A computer is an electronic device, operating under the


control of instructions (software) stored in its own memory
unit, that can accept data (input), manipulate data (process),
and produce information (output) from the processing.

• Generally, the term is used to describe a collection of devices


that function together as a system.

7/23/2012 Department of CSE 5


Devices that comprise a 
Computer system
Monitor Speaker
(output) (output) System unit
(processor,
memory…)
Printer
(output)

Storage devices
(CD-RW, Floppy,
Hard disk, zip,…)
Mouse
(input)
Scanner Keyboard
(input) (input)

7/23/2012 Department of CSE 6


What Does A Computer Do?
• Computers can perform four general operations, which
comprise the information processing cycle.

• Input

• Process/Computation

• Storage

• Output

7/23/2012 Department of CSE 7


Data and Information

• All computer processing requires data, which is a collection of


raw facts, figures and symbols, such as numbers, words,
images, video and sound, given to the computer during the
input phase.
• Computers manipulate/processes data to create information.
Information is data that is organized, meaningful, and useful.
• During the output Phase, the information that has been created
is put into some form, such as a printed report.
• The information can also be put in computer storage for future
use.

7/23/2012 Department of CSE 8


Why Is A Computer So Powerful?

• The ability to perform the information processing cycle with


amazing speed.

• Reliability (low failure rate).

• Accuracy.

• Ability to store huge amount of data and information.

• Ability to communicate with other computers.

7/23/2012 Department of CSE 9


How Does a Computer Know
what to do?
• It must be given a detailed list of instructions, called a computer
program or software, that tells it exactly what to do.

• Before processing a specific job, the computer program


corresponding to that job must be stored in memory.

• Once the program is stored in memory the computer can start the
operation by executing the program instructions one after the
other.

7/23/2012 Department of CSE 10


Block Diagram of Computer

Central Processing
unit Output Device

Arithmetic Logic
Input Device Unit

Control unit
Auxiliary storage
Device

Primary storage

7/23/2012 Department of CSE 11


Peripherals

 input, output, or auxiliary storage devices attached to a


computer

 Input Devices include keyboard and mouse, scanners.

 Output Devices include printers, video display, LCD screens.

 Auxiliary Devices/Secondary Storage Devices include disk


drives, CD‐ROM and DVD‐ROM drives, modems, and digital
cameras.

7/23/2012 Department of CSE 12


Input Device

 An external device connected to the CPU, used to feed data


and instructions for solving the problem at hand

 The most commonly used input device is the Keyboard and


some of the other input devices are listed below
 Mouse
 Joystick
 Light pen
 Trackball
 Optical Scanner
 Voice input

7/23/2012 Department of CSE 13


Output Device

 It is used to display the results.


 The most commonly used output device is the
monitor. Some of the other output devices are
 printer
 plotter
 plasma display panels
 LCD displays
 voice output

7/23/2012 Department of CSE 14


System Unit
• Data and instructions received from the input device are
stored and processed in the System unit. The Central
Processing Unit and Memory Unit are together called as
System Unit.
Central Processing
unit Output Device
Arithmetic Logic
Input Device Unit

Control unit
Auxiliary storage
Device

Primary storage

7/23/2012 Department of CSE 15


Central Processing Unit
 Once the data and instructions received from the input
device, they are processed in this unit. This is generally called
CPU. Further it consists of two functional units.
 Control Unit (CU)
 Arithmetic and Logic Unit (ALU)

Central Processing
unit Output Device
Arithmetic Logic
Input Device Unit

Control unit
Auxiliary storage
Device

Primary storage

7/23/2012 Department of CSE 16


Arithmetic/Logic Unit
 Performs arithmetic and logical micro operations:
Example:
– arithmetic(+,‐) and
– logical (<,=) operations
Central Processing
unit Output Device
Arithmetic Logic
Input Device Unit

Control unit
Auxiliary storage
Device

Primary storage

7/23/2012 Department of CSE 17


Control Unit

• Controls the order in which your program instructions are


executed.
Functions of CU:
 Fetches data and instructions to main memory
 Interprets these instructions
 Controls the transfer of data and instructions to and from
main memory
 Controls input and output devices.
 Overall supervision of computer system

7/23/2012 Department of CSE 18


Memory Unit

 It is a storage device where the data and instructions fed by


the user are stored.
 It is an ordered sequence of storage cells, each capable of
holding a piece of information
 Each cell has its own unique address
 The information held can be input data, computed values,
or your program instructions.

7/23/2012 Department of CSE 19


Memory Unit

Representation:

7/23/2012 Department of CSE 20


Memory Unit

 The computer memory is measured in terms of bits, bytes


and words.

 A bit is a binary digit either 0 or 1.

 A byte is unit of memory and is defined as sequence of 8 bits.

 The word can be defined as a sequence of 16/32/64 bits or


2/4/8 bytes respectively depending on the machine
architecture.

7/23/2012 Department of CSE 21


Main/Primary Memory
 It is the place where the data and instructions, currently being
executed are stored.
 Located outside CPU
 High speed.
 This is a temporary memory because the data and
instructions stored here get erased when the power goes off.
 This is also referred as primary memory.
 It is a semiconductor memory and measured in terms of
megabytes and gigabytes.
 Eg. RAM and ROM

7/23/2012 Department of CSE 22


Secondary/Auxiliary Storage Devices

 Because most of main memory is volatile and limited, it is


essential that there should be other types of storage devices
where programs and data can be stored when they are no
longer being processed.

 Secondary storage devices can be installed within the


computer box at the factory or added later as needed.

7/23/2012 Department of CSE 23


Secondary/Auxiliary Storage Devices

 It is a non volatile memory and made up of magnetic material


and stores large amount of information for long time.
 Low speed.
 Holds programs not currently being executed.
 Input, output, and auxiliary/secondary storage devices attached
to a computer are called peripherals.
 E.g. Magnetic tapes, magnetic disks, compact disks etc.

7/23/2012 Department of CSE 24


Auxiliary Storage Devices

 (Floppy) disks

 Hard disks

 CD‐ROM’s

 Flash drives

… and more

7/23/2012 Department of CSE 25


The Bus
• Computer components are connected by a bus.

• A bus is a group of parallel wires that carry control signals and


data between components.

Central Processing
unit Output Device
Arithmetic Logic
Input Device Unit

Control unit
Auxiliary storage
Device

Primary storage

7/23/2012 Department of CSE 26


Hardware vs. Software
 Hardware :

The physical elements of a computing system


(printer, circuit boards, wires, keyboard…).

 Software:

The programs that provide the instructions for a


computer to execute.

7/23/2012 Department of CSE 27


Classification of Software

Software

Application  System 
Software  Software

7/23/2012 Department of CSE 28


Classification of Software

• System software consists of programs that manages the


computer resources.
Divided into three classes
– Operating System-user interface, database access
– System support software-other operational services
like disk format programs etc
– System Development software - language
translators and debugging tools etc

7/23/2012 Department of CSE 29


Application Software
Application Software is directly responsible for helping users
solve their problems.
MS Excel
For example
• Word processing
• Electronic spreadsheet
• Database
• Presentation graphics
MS Word MS Powerpoint

MS Access
7/23/2012 Department of CSE 30
Computer Languages
• Machine Language‐ The only programming language available
in earlier days
– Consists of only 0’s and 1’s; e.g.:- 10101011
• Symbolic language or Assembly language‐
– symbols or mnemonics used to represent instructions
– hardware specific
e.g. ADD X,Y; Add the contents of y to x
• High‐level languages‐ English like language using which the
programmer can write programs to solve a problem.
– more concerned with the problem specification and not
oriented towards the details of computer.
– e.g.: C, C++, C#, Fortran, BASIC, Pascal etc.

7/23/2012 Department of CSE 31


Machine level vs High level languages

Machine level language High Level Language
1. Uses only 2 symbols, zero and one. Uses alphabets, digits, punctuations and some 
special symbols
2. Each instruction is a sequence of zero’s and  Each instruction is English like statement.
one’s.
3. Can be directly executed. Cannot be directly executed.
4. Machine dependent. Machine independent.
5. Difficult to understand, modify, write and  Easy to understand, write, modify and debug.
debug.
6. Efficient (Fast). Less Efficient (Slow).
7. Not Standardized. Standardized.
8. Called as First generation programming  Called as Third generation programming 
language. language.

7/23/2012 Department of CSE 32


Assembly language vs High level language

Assembly language High Level Language
1. Assembly language increases the efficiency  They are to be compiled or interpreted & are 
of  the MLP resulting from it. less efficient compared to ALP
2. Difficult and time consuming to write a  It is easy to write a program in this language as 
program. it uses English like statements.
3. Assembly language programmer must be  Only the syntax of this language is to be learnt 
an expert who knows all about the logical  to write programs in this language.
structure of the computer.
4. It is machine dependent.  They are designed independent of the structure
Program written for one model of   The compiler translates the high level language
computer can’t be executed on another  into machine language and the program written
model. In other words it is not portable  in high level language can be executed on
from one machine to another. different computers.
5. It has greater flexibility in writing programs  More device independent but syntax 
well matched to the computers. dependent program to be written. Just usage of 
the available syntax of structures within high 
level language.

7/23/2012 Department of CSE 33


Language Translators

 basically translator programs


 used to translate programs written in one programming
language to machine level language.
e.g.:‐ Interpreters, Compilers and Assemblers.

7/23/2012 Department of CSE 34


Language Translators

Compiler : Entire high level language program as input and


translates it into machine language at a time.
e.g.:‐ C, C++ compilers

Interpreter : Program which translates one statement of a high


level language program into machine language at a time and
executes it.
e.g.:‐ Basic Interpreters, Java Interpreters.

Assembler : is a program which translates an assembly language


program into machine language.
e.g.:‐ TASM(Turbo ASseMbler), MASM(Macro ASseMbler).

7/23/2012 Department of CSE 35


Compiler vs Interpreter

Compiler Interpreter
1. Takes entire high level language Takes one statement of a high level
program as input and translate language program as input &
into machine language translates into machine language &
executes.
2. Errors that occur in the program Errors that occur only in the
are listed & displayed. statement being taken for translation
are displayed.
3. Debugging is faster. Debugging is slower.
4. Requires more memory. Requires less memory.
5. Costlier Cheaper.

7/23/2012 Department of CSE 36


More on Computer Memories

The computer memory is classified into


• Main memory‐Primary storage
• Secondary memory‐Auxiliary storage
• Cache Memory

The Main/Primary memory is a temporary memory, so


the data and instructions stored here erased when the
power goes off.
It consists of RAM and ROM

7/23/2012 Department of CSE 37


Main/Primary Memory
RAM stands for Random Access Memory
 It is the read and write memory.
 The information typed by the user are stored in this
memory and any memory location can be accessed
directly without scanning it sequentially.
 Hence it is called as random access memory.
 During power failure the information stored in it will be
erased.
 It is also called as volatile memory.
ROM stands for Read Only Memory
Permanent memory and non volatile.
The contents in locations in ROM can not be changed
It stores mainly stored program and basic input output
systems programs.
7/23/2012 Department of CSE 38
Main/Primary Memory
PROM :Programmable Read Only Memory
 It is a variation of ROM.
 The contents of this memory are decided by the user
 These contents can not be erased once they are written to it.
EPROM : Erasable Programmable Read Only Memory
 It is a modification to PROM.
 Contents stored can be erased by exposing it to Ultra Violet
Light source.
EEPROM: Electrically Erasable Programmable Read Only
Memory
 The contents stored in this can be erased electrically.
[ Flash, micro processors etc]

7/23/2012 Department of CSE 39


Cache memory

 It is a High speed memory and placed between the CPU and


the main memory.
 Users can not access this memory.
 It stores the data and instructions currently to be executed.
 More costlier than main memory.
 Less Capacity than main memory

7/23/2012 Department of CSE 40


Computer Memory Hierarchy

7/23/2012 Department of CSE 41


Operating System basics
 Operating System is an integrated collection of programs which make the
computer operational and help in executing user programs.

 It acts as interface between the man and machine.

 It manages the system resources like memory, processors, input‐output


devices and files.

 Types:
 Single user [DOS] and Multi user OS [Windows XP, Unix],
 Real time OS [Windows CE, QNX],
 Single‐tasking [DOS] and Multi‐tasking [Unix],
 Distributed OS [Amoeba],
 Embedded OS [Windows CE, Symbian OS] etc.

7/23/2012 Department of CSE 42


Operating System as an Interface

7/23/2012 Department of CSE 43


So … an Operating System

• A program that runs on the “raw” hardware and supports
– Resource Sharing
• Abstracts and standardizes the interface to the user across 
different types of hardware
– OS hides the messy details which must be performed
• Manages the hardware resources
– Each program gets time with the resource
– Each program gets space on the resource
• Manages different goals:
– Use hardware efficiently and give maximum performance to each user.

7/23/2012 Department of CSE 44


Questions
 What is a computer?
 What do you mean by stored program concept? Who 
suggested this idea?
 What is CPU? What it consists of?
 What are the functions of control unit?
 Differentiate between 
 RAM and ROM
 PROM and EPROM
 Main memory and secondary memory
 High level language and m/c level language
 Give an example which can be used as both i/p and o/p device 
 What is an O.S?
7/23/2012 Department of CSE 45
Number Systems

Common Number Systems
Used by Used in
System Base Symbols humans? computers?
Decimal 10 0, 1, … 9 Yes No
Binary 2 0, 1 No Yes
Octal 8 0, 1, … 7 No No
Hexa- 16 0, 1, … 9, No YES/No
decimal A, B, … F

7/23/2012 Department of CSE 46


Quantities/Counting
Hexa- Hexa-
Decimal Binary Octal decimal Decimal Binary Octal decimal
0 0 0 0 8 1000 10 8
1 1 1 1 9 1001 11 9
2 10 2 2 10 1010 12 A
3 11 3 3 11 1011 13 B
4 100 4 4 12 1100 14 C
5 101 5 5 13 1101 15 D
6 110 6 6 14 1110 16 E
7 111 7 7 15 1111 17 F

7/23/2012 Department of CSE 47


Decimal Number System

 The decimal number system is also known as base 10.

The values of the positions are calculated by taking 10

to some power.

 Why is the base 10 for decimal numbers?

 Because we use 10 digits, the digits 0 through 9.

7/23/2012 Department of CSE 48


Binary Number System

• The binary number system is also known as base 2.


The values of the positions are calculated by taking 2
to some power.

• Why is the base 2 for binary numbers?

o Because we use 2 digits, the digits 0 and 1.

7/23/2012 Department of CSE 49


Binary Number System
• The binary number system is also a positional
numbering system.

• Instead of using ten digits, 0 - 9, the binary system


uses only two digits, 0 and 1.

• Example of a binary number and the values of the


positions:

1 0 0 1 1 0 1

26 25 24 23 22 21 20

7/23/2012 Department of CSE 50


Hexadecimal Number System

• The hexadecimal number system is also known as base 16. The


values of the positions are calculated by taking 16 to some
power.

• Why is the base 16 for hexadecimal numbers ?

– Because we use 16 symbols, the digits 0 through 9 and the


letters A through F.

7/23/2012 Department of CSE 51


Hexadecimal Number System
Binary Decimal Hexadecimal Binary Decimal Hexadecimal 
0             0                  0                      1010         10                 A
1             1                  1                      1011         11                 B
10             2                  2                      1100         12                 C
11             3                  3                      1101         13                 D
100             4                  4                      1110         14                 E
101            5                  5                      1111         15                 F
110            6                  6
Example of a hexadecimal
111            7                  7
number and the values of the
1000            8                  8
positions:
1001            9                  9
3 C 8 B 0 5 1
166 165 164 163 162 161 160

7/23/2012 Department of CSE 52


Conversion Among Bases
• The possibilities:

Decimal Octal

Binary Hexadecimal

7/23/2012 Department of CSE 53


Example – Different forms

2510 = 110012 = 318 = 1916


Base

7/23/2012 Department of CSE 54


Decimal to Decimal (for understanding)

Weight

12510 => 5 x 100 = 5


2 x 101 = 20
1 x 102 = 100
125

Base

7/23/2012 Department of CSE 55


Binary to Decimal

• Technique

– Multiply each bit by 2n, where n is the “weight” of

the bit

• The weight is the position of the bit, starting from 0

on the right

– Add the results


7/23/2012 Department of CSE 56
Example‐ Binary to Decimal

Bit “0”

1010112 => 1 x 20 = 1
1 x 21 = 2
0 x 22 = 0
1 x 23 = 8
0 x 24 = 0
1 x 25 = 32

4310

7/23/2012 Department of CSE 57


Octal to Decimal
• Technique

– Multiply each bit by 8n, where n is the “weight” of


the bit
• The weight is the position of the bit, starting from 0
on the right

– Add the results

7/23/2012 Department of CSE 58


Example‐ Octal to Decimal

7248 => 4 x 80 = 4
2 x 81 = 16
7 x 82 = 448

46810

7/23/2012 Department of CSE 59


Hexadecimal to Decimal

• Technique
– Multiply each bit by 16n, where n is the “weight”
of the bit
• The weight is the position of the bit, starting from 0
on the right

– Add the results

7/23/2012 Department of CSE 60


Example‐ Hexadecimal to Decimal

ABC16 => C x 160 = 12 x 1 = 12


B x 161 = 11 x 16 = 176
A x 162 = 10 x 256 = 2560

274810

7/23/2012 Department of CSE 61


Decimal to Binary

• Technique

– Divide by two, keep track of the remainder

– First remainder is bit 0 (LSB, least‐significant bit)

– Second remainder is bit 1

– So on.

7/23/2012 Department of CSE 62


Example‐ Decimal to Binary
12510 = ?2 2 125
2 62 1
2 31 0
2 15 1
2 7 1
2 3 1
2 1 1
0 1

12510 = 11111012

7/23/2012 Department of CSE 63


Octal to Binary

• Technique

– Convert each octal digit to a 3‐bit equivalent

binary representation

7/23/2012 Department of CSE 64


Example‐ Octal to Binary

7058 = ?2

7 0 5

111 000 101

7058 = 1110001012

7/23/2012 Department of CSE 65


Hexadecimal to Binary
• Technique
– Convert each hexadecimal digit to a 4‐bit 
equivalent binary representation

7/23/2012 Department of CSE 66


Example‐ Hexadecimal to Binary

10AF16 = ?2

1 0 A F

0001 0000 1010 1111

10AF16 = 00010000101011112

7/23/2012 Department of CSE 67


Decimal to Octal
• Technique
– Divide by 8

– Keep track of the remainder

7/23/2012 Department of CSE 68


Example‐ Decimal to Octal

123410 = ?8

8 1234
8 154 2
8 19 2
8 2 3
0 2

123410 = 23228

7/23/2012 Department of CSE 69


Decimal to Hexadecimal
• Technique
– Divide by 16

– Keep track of the remainder

7/23/2012 Department of CSE 70


Example‐ Decimal to Hexadecimal

123410 = ?16

16 1234
16 77 2
16 4 13 = D
0 4

123410 = 4D216

7/23/2012 Department of CSE 71


Binary to Octal
• Technique
– Group bits in threes, starting on right

– Convert to octal digits

7/23/2012 Department of CSE 72


Example‐ Binary to Octal

10110101112 = ?8

1 011 010 111

1 3 2 7

10110101112 = 13278

7/23/2012 Department of CSE 73


Binary to Hexadecimal
• Technique
– Group bits in fours, starting on right

– Convert to hexadecimal digits

7/23/2012 Department of CSE 74


Example‐ Binary to Hexadecimal

10101110112 = ?16

10 1011 1011

2 B B

10101110112 = 2BB16

7/23/2012 Department of CSE 75


Octal to Hexadecimal
• Technique
– Use binary as an intermediary

7/23/2012 Department of CSE 76


Example‐ Octal to Hexadecimal

10768 = ?16
1 0 7 6

001 000 111 110

2 3 E

10768 = 23E16

7/23/2012 Department of CSE 77


Hexadecimal to Octal
• Technique
– Use binary as an intermediary

7/23/2012 Department of CSE 78


Example‐ Hexadecimal to Octal

1F0C16 = ?8
1 F 0 C

0001 1111 0000 1100

1 7 4 1 4

1F0C16 = 174148

7/23/2012 Department of CSE 79


Exercise – Convert ...

Hexa-
Decimal Binary Octal decimal
33
1110101
703
1AF

Do not use a calculator!

7/23/2012 Department of CSE 80


Answer
Hexa-
Decimal Binary Octal decimal
33 100001 41 21
117 1110101 165 75
451 111000011 703 1C3
431 110101111 657 1AF

7/23/2012 Department of CSE 81


Common Powers
• Base 10
Power Preface Symbol Value
10-12 pico p .000000000001

10-9 nano n .000000001

10-6 micro  .000001

10-3 milli m .001

103 kilo k 1000

106 mega M 1000000

109 giga G 1000000000


1012 tera T 1000000000000

7/23/2012 Department of CSE 82


Common Powers
• Base 2 Power Preface Symbol Value
210 kilo kB 1024
220 mega MB 1048576
230 Giga GB 1073741824
240 tera TB 1099511627776
250 peta PB 1125899906842624
260 exa EB 1152921504606846976
270 zetta ZB 1180591620717411303424
280 yotta YB 1208925819614629174706176

• What is the value of “kB”, “MB”, and “GB” …?
• In computing, particularly w.r.t. memory, the base‐2
interpretation generally applies.
7/23/2012 Department of CSE 83
Example
In your laptop/lab machine…
1. Double click on My Computer
2. Right click on C:
3. Click on Properties

/ 230 =

7/23/2012 Department of CSE 84


Exercise – Free Space
• Determine the “free space” on all drives on a 
machine in the lab

Free space
Drive Bytes GB
A:
C:
D:
E:
etc.

7/23/2012 Department of CSE 85


Review – multiplying powers
• For common bases, add powers

ab  ac = ab+c

26  210 = 216 = 65,536


or…
26  210 = 64  210 = 64k

7/23/2012 Department of CSE 86


Binary Addition
• Two 1‐bit values

A B A+B
0 0 0
0 1 1
1 0 1
1 1 10
“two”

7/23/2012 Department of CSE 87


Binary Addition
• Two n‐bit values
– Add individual bits

– Propagate carries

– E.g.,
1 1 1
10101 29
+ 11001 + 25
101110 54

7/23/2012 Department of CSE 88


Multiplication
• Decimal (just for understanding)

35
x 105
175
000
35
3675

7/23/2012 Department of CSE 89


Multiplication
• Binary, two 1‐bit values

A B AB
0 0 0
0 1 0
1 0 0
1 1 1

7/23/2012 Department of CSE 90


Multiplication
• Binary, two n‐bit values
– As with decimal values

– E.g.,  1110
x 1011
1110
1110
0000
1110
10011010

7/23/2012 Department of CSE 91


Fractions
• Decimal to decimal (just for understanding)

3.14 => 4 x 10-2 = 0.04


1 x 10-1 = 0.1
3 x 100 = 3
3.14

7/23/2012 Department of CSE 92


Fractions
• Binary to decimal

10.1011 => 1 x 2-4 = 0.0625


1 x 2-3 = 0.125
0 x 2-2 = 0.0
1 x 2-1 = 0.5
0 x 20 = 0.0
1 x 21 = 2.0
2.6875

7/23/2012 Department of CSE 93


Fractions

• Decimal to binary x
.14579
2
3.14579 0.29158
x 2
0.58316
x 2
1.16632
x 2
0.33264
x 2
0.66528
x 2
1.33056
11.001001... etc.

7/23/2012 Department of CSE 94


Exercise – Convert ...

Hexa-
Decimal Binary Octal decimal
29.8
101.1101
3.07
C.82
Do not use a calculator!

7/23/2012 Department of CSE 95


Answer
Hexa-
Decimal Binary Octal decimal
29.8 11101.110011… 35.63… 1D.CC…
5.8125 101.1101 5.64 5.D
3.109375 11.000111 3.07 3.1C
12.5078125 1100.10000010 14.404 C.82

7/23/2012 Department of CSE 96


Representation of Number–
Basic Idea 

• Hardware can only deal with binary digits, 0 and 1.

• Must represent all numbers, integers or floating

point, positive or negative, by binary digits, called

bits.

7/23/2012 Department of CSE 97


Positive Integers
• Decimal system: made of 10 digits, {0,1,2, . . . , 9}
41 = 4×101 + 1×100
255 = 2×102 + 5×101 + 5×100
• Binary system: made of two digits, {0,1}
00101001 = 0×27 + 0×26 + 1×25 + 0×24
+1×23 + 0×22 + 0×21 + 1×20
= 32 + 8 +1 = 41
11111111 = 255, largest number with 8 
binary digits, 28‐1
7/23/2012 Department of CSE 98
Writing Numbers

Most significant digit 2    5    5 Least significant digit

Most significant bit Least significant bit


(MSB) 0 0 1  0 1  0  0 1 (LSB)

7/23/2012 Department of CSE 99


1’s Complement Representation
• 1’s Complement Representation
– Fixed size representation

– Most significant bit is reserved as sign‐bit

– Positive numbers are represented using standard


binary notation

– Negative numbers are represented by inverting all


bits in the standard binary notation

7/23/2012 Department of CSE 100


1’s Complement ‐ Example
• Represent 1210 using 8‐bit 1’s Complement
– Converting to binary: 1210 = 11002
– 8‐bit 1’s complement representation: 000011002
• Sign bit is 0 to indicate positive value.
• Represent -1310 using 8‐bit SBN
– Converting to binary: 1310 = 11012
– 8‐bit representation: 000011012
– Since original decimal number was negative each bit in 8‐bit 
representation is inverted!
– 1s Complement representation = 11110010
• Note: Sign bit becomes 1 to indicate negative value

7/23/2012 Department of CSE 101


1’s Complement ‐ Example
• Convert 1’s complement 000011112 to decimal
– Sign bit is 0 to indicating positive value.
– Converting 000011112 to decimal we get 1510
– Final result: +1510
• Convert 1’s complement 111101012 to decimal
– Sign bit is 1 indicating negative value.
– First invert all the bits to get: 000010102
– Convert above binary to decimal to get 1010
– Final result: ‐1010

7/23/2012 Department of CSE 102


1’s Complement
• 2 different representations for 0
+0 (00000000) and ‐0 (11111111)

• However, A – A operations can be easily


represented
A – A = A + (‐A) = 11111111 (which is effectively 0 in
1’s complement)

7/23/2012 Department of CSE 103


2’s Complement
• Overcomes the limitations of 1’s complement representation!
– Fixed size representation
– Enables subtraction of numbers via addition!
– Also reserves a special sign bit (left most bit)
• 0 (sign bit) indicates positive numbers
• 1 (sign bit) indicates negative numbers

– Conversion to and from 2’s complement requires similar


steps
• Several of them are identical to 1’s complement

7/23/2012 Department of CSE 104


2’s Complement – Example
• Represent +2010 using 8‐bit 2’s Complement
• Since number is positive simply represent +2010 as a 8‐bit binary 
number!
• +2010 = 000101002

• Represent -1810 using 8‐bit 2’s complement
• Since number is negative we need to do 1’s complement and add 12
– Step 1: Convert 1810 to 8‐bit binary = 000100102
– Step 2: Invert bits = 111011012
– Step 3: Add 12 = 111011102
– Final result: ‐1810 = 1 1 1 0 1 1 1 0 2 in 8‐bit 2’s complement

7/23/2012 Department of CSE 105


2’s Complement – Conversion 

• Convert 8‐bit 2’s complement to decimal


– Case 1: 000010102
• Sign bit is 0 indicating positive number
• Simply convert binary to decimal to get 1010
– Case 2: 111110102
• Sign bit is 1 indicating negative number
• Step 1: Invert all bits to get 000001012
• Step 2: Add 12 to get 000001102
• Convert binary to decimal to get ‐610
– Note the negative sign on decimal number!

7/23/2012 Department of CSE 106


Converting 2’s Complement to Decimal
n-2
an-1an-2 . . . a1a0 = -2n-1an-1 + Σ 2i ai
i=0

8-bit conversion box


‐128   64    32   16     8     4     2      1

‐128   64    32   16     8     4     2      1


Example
1      1      1      1      1     1     0      1

-128+64+32+16+8+4+1 = -128 + 125 = -3

7/23/2012 Department of CSE 107


Summary
• Computer
• Software
• Hardware
• Software Classification
• Memory 
• Operating System
• Number System
• Number Conversions
• Number Representations

7/23/2012 Department of CSE 108

You might also like