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

C++ Chapter 1

Introduction

Uploaded by

robhamsolo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C++ Chapter 1

Introduction

Uploaded by

robhamsolo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter one

Introduction to computer

What is computer?

A computer is an electronic machine, operating under the control of instructions stored


in its own memory that can accept data, manipulate the data according to specified rules,
produce results, and store the results for future use. Computers process data to create
information. Data is a collection of raw unprocessed facts, figures, and symbols.
Information is data that is organized, meaningful, and useful. To process data into
information, a computer uses hardware and software.

Hardware is the electric, electronic, and mechanical equipment that makes up a


computer.

Software is the series of instructions that tells the hardware how to perform tasks.

Characteristics of computer

The characteristics of computers that have made them so powerful and universally useful
are speed, accuracy, diligence, versatility and storage capacity. Let us discuss them
briefly.
Speed

Computers work at an incredible speed. A powerful computer is capable of performing


about 3-4 million simple instructions per second. Their speed is measured by the amount
of time it took to perform or carry out a basic operation. Computer speed measured in
terms of microsecond (10-6 one millionths), nanosecond (10-9 one billionths), and Pico
second (10-12 one trillionths).

Accuracy

Nowadays computers are being used for surgical purposes, which need almost a hundred
percent accuracy. From this we can understand that computers are accurate and
consistent. Unless there is an error in the input data or unreliable program the computer
processes with a very high accuracy.
Diligence

Unlike human beings, computers are highly consistent. They do not suffer from human
traits of boredom and tiredness resulting in lack of concentration. Computers, therefore,
are better than human beings in performing voluminous and repetitive jobs.

Versatility

Computers are versatile machines and are capable of performing any task as long as it
can be broken down into a series of logical steps. The presence of computers can be seen
in almost every sphere – Railway/Air reservation, Banks, Hotels, Weather forecasting
and many more.

Automatic

Once necessary information and program is fed, the computer performs processing
without human intervention.

Storage capacity

Today’s computers can store large volumes of data. A piece of information once recorded
(or stored) in the computer, can never be forgotten and can be retrieved almost
instantaneously. And the time it took to retrieve or process single information is not more
than a micro or nanoseconds.

In general, a computer has a capacity to store a very large amount of information in


organized manner so that accessing information is very fast.

1.5. Generations (Evolution) of Computers

Although computer professionals do not agree on exact dates or specifics, computer


developments are often categorized by generations. Actually there are four generations and major
characteristics that distinguish these generations are the following:

 Dominant type of electronic circuit elements used.


 Major secondary storage media used.
 Computer language used.
 Types or characteristic of operating system used.
 Memory access time (time to store or retrieve a word or data from memory).
Computer generations are usually categorized by dramatic improvement in the hardware,
typically refold or better increases in speed and reliability.

First generation (1950s) – Vacuum Tube Technology


o Used vacuum tubes as components for the electronic circuit.

o Punched cards were the main source of inputs, and


magnetic grams were used for internal storage.

o Operate in a speed of milliseconds (thousands of a


second) and could handle more than 10,000 additions
each second.
o Most applications were scientific calculations.

o They generate huge amount of heat

o They solve one problem at a time

o They are room-sized and weights up to 30 tones (3000KG).

o They need 200KW of power to operate, which contains 18,000 Vacuum Tubes

Second generations (early 1960s) – Transistor Technology


 Transistors were the main circuit components. (Transistors are a solid state device made from silicon
which is smaller, cheaper, faster, dissipate less energy and more reliable than vacuum tube but work in
the same way with the vacuum tube.)
 Invented by Bell Labs.
 Magnetic tapes (similar with home tape caste), used for main storage.
 Business applications become more commonplace, with large data files stored on magnetic tape and
disk. (Magnetic disk: is a circular platter constructed of metal or plastic materials coated with magnetic
substance.)
 High-level languages COBOL and FORTRAN were introduced during this period. Batch operating
systems are used that permitted rapid processing of magnetic tape files.

Operate in microseconds (millionths of a second) with more than 200,000 additions


possible each second.
Third generation (late 1960s, early 1970s) – IC Technology

Characterized by solid-state logic and integrated circuit (IC). (A


single, self-contained transistor is called discrete component. In
early 1960 electronic equipment composed of discrete
components transistors, capacitors, resistors, they are:

manufactured separately

Packed in their own containers and soldered (wired


together) on a circuit board. So the entire manufacturing
process was cumbersome and expensive. Do to these and

other problems in 1958 the achievement that revolutionized electronics started


the era of microelectronics: the invention of integrated circuit.

Computer storage switched from magnetic cores to integrated circuit boards that
provide modularity (expandable storage) and compatibility (interchangeable
equipment
New input/output methods such as optical scanning and plotters.

Software become more important with sophisticated operating systems, improved


programming languages,
Keyboards and Monitors introduced at this time.
Fourth generation (late 1970s,early 1989s)-LSI Technology

Greatly expanded storage capabilities and improved circuitry.

Has Large-Scale Integrated Circuit (LSI) which has several hundred


gf thousands of transistors placed on one tiny silicon chip.

Computer memory operates at speeds of nano seconds


(billionths of a second) with large computers capable of adding
15 million numbers per second.

The fifth generation computer


This computer generation is still in progress. An architecture, which makes use of the changes in
technology and allows a simple and natural methodology for solving problems, is being sought.
These computers will have intelligent processors i.e., processors which can draw inferences.
Users will also be able to interact with them in natural languages such as English, German etc.
Japans are working intensively on the project for developing the 5th generation. This generation
is also called the future generation.

Summary of generation of computers

Generation 1st 2nd 3rd 4th

SSD Punched card Magnetic Tape Magnetic disk Mass storage device

Circuit element Vacuum tube Transistor IC LSI

Language Machine& assembly Fortran, COBOL etc. Structured language Application oriented

Operating system Operator control Batch system Application oriented Time sharing

Mem. Access time 1ms 10μs 10ns 1ns

Approx. date 1946-57 1958-64 1965-71 From 1971 above

ENIAC, UNIVAC,
Examples UDVAC IBM7090, 7094 IBM system Late IBM product
Introduction to computer & programming

Types of Computers

Computers can be classified into different categories based on different characteristics.

Based on type of data they process


Based on the type of data they process computers can be classified as:

Analog

Analog computers operate by measuring physical properties. They deal with continuous
variables; they don’t compute directly with numbers, rather, they operate by measuring physical
magnitude such as pressure, temperature, voltage, current etc.

Examples: Thermometer, Voltmeter, Speedometer

Digital

Digital computers deal with discrete variables; they operate by counting rather than measuring.
They operate directly up on numbers (or digits) that represent numbers, letters, or other special
symbols.

Examples: Abacus, Desk & pocket calculators, general purpose computers

Hybrid

Hybrid computers inherit the best features of both analog and digital computers. Usually the
Input is continuous data (analog). Since Digital Processing is more accurate, processing takes
place digitally. The processed information – the output – could be either digital or analog,
depending on the user preference or the type of application.

Examples: digital camera, health monitoring machines in some hospitals,

Page 6
Introduction to computer & programming

Based on Size, Capacity and price


Size and capacity are also the other characteristics of computers that can be used to categorize
computers. Based on these characteristics computers can be classified as:

Super computer

The term supercomputer has been coined to describe a category of extremely powerful computer
designed for high-speed processing. A supercomputer is generally characterized as being the
fastest, most powerful, and most expensive computer.

Generally, Supercomputers are:

 The largest and the most efficient computers


 Very expensive
 very fast and
 Supports hundreds of users at different locations

Mainframe computer

Mainframe computers are large, powerful computers that are physically larger than micros and
minis and usually have processors with faster instruction processing speeds. For example, they
may be able to process from 10 to 200 million instructions per second (MIPS). Mainframe
computers also support multiple users and are expensive.

Mini computer

Minicomputers are midrange computers that are larger and more powerful than most
microcomputers but are smaller and less powerful than mainframe computer systems.
Minicomputers are being used for a large number of business and scientific applications. They
are popularly used in scientific laboratories, research centers, universities and colleges,
engineering firms, industrial process monitoring and control, etc.

Micro computers

The smallest computers ever produced in the history of computers are microcomputers. Since
they are designed to be used by a single user, they have the least capacity as compared to the
other types of computers. They are also the least expensive of all types. There two different types
of microcomputers are desktop computers and portable computers (laptops, notebook computers
and palmtops)
Page 7
Introduction to computer & programming

Classification by purpose of application

Computers can be applied or used for different purposes. Based upon their application, they are
classified as special purpose or general purpose computers.

Special purpose computers


They are designed to solve a single type of problem, that is their components and their functions
are uniquely adapted to a specific situation involving specific application.
Example:
 The public telephone box
 Traffic control system
 Ticket machines (used in grocery, super market etc.)
 Pocket-calculators etc.
 Counters
Most analog computers are special purpose computers.

General purpose computers


They are designed to solve variety of problems through the use of “store program concept”. A
program or set of instructions designed to solve a problem is read and stored into the memory
and then executed by the computer one by one. The same computer can be applied to solve
another set of problem using different program. General computers are more flexible and
versatile.
Examples
 Micro computers
 Mini computers
 Super computers etc.

Limitation of computers

Computers have the following limitations: -

1. Cannot decide how to be programmed


2. Does not provide its own inputs, unless people provide with the input
3. Interpretation of data and implementation of decision is left for human beings
4. Unlike human beings, computers are incapable to:
 Think
 Judge and
 Motivated

Page 8
Introduction to computer & programming

Components of computer system

Generally, a computer system is composed of two main components:

1. Computer hardware and


2. Computer software

Computer Hardware
Computer hardware is the physical part of the computer system that can be seen and felt. The
hardware part of a computer system is composed of a number of interacted physical parts. E.g.
keyboard, mouse, CPU

1.1 Types of Computer Hardware


The hardware part of a computer system is composed of a number of interacting physical parts
based on the need of the information flow. Information flows in the computer hardware. There
are several criteria by which computer hardware can be categorized.

Based on information processing, we can divide computer hardware into four:

i) Input Devises
ii) Storage Dévies
iii) Output Dévies
iv) Central Processing Unit (CPU)

Page 9
Introduction to computer & programming

i) Input Devices
Input devices are used to enter information into computer. They convert the data we give
them into the form that can be manipulated in the computer (electronic format).

Some examples of input devices are Keyboard, mouse, scanner, Bar Code Reader, mice

ii) Central Processing Unit


CPU executes instructions and performs the computer's processing activities. It is also known as
processor or microprocessor. It functions the same purpose as the human brain for human being.
It is called the brain of the computer.
One of the basic features of a computer that affects its entire performance is the CPU speed. CPU
speed is measured in Hertz (Hz). Hertz is the number of cycles per second. 1Hz=1cycle per
second. Larger units are KHz (Kilo Hertz), MHz (Mega Hertz), GHz (Giga Hertz), etc.
1 KHz = 1000 Hz

1 MHz = 1000 KHz

1 GHz = 1000MHZ

Current CPUs are as fast as 2-3GHz (2-3 billion cycles per second)

CPU has three sub-components:

 Control Unit (CU)


 Arithmetic Logic Unit (ALU)
 Registers

Control Unit

As human brain controls the body, control unit controls the computer hardware. Control
Unit does not execute instruction by itself, i.e. does not carry out instruction processing,
but it directs other processing elements to execute instructions.

Page 10
Introduction to computer & programming

Arithmetic Logic Unit (ALU)

The purpose of ALU is to execute instruction. It performs two operations:

 Arithmetic operation
 Logic Operation
Arithmetic operation: this includes mathematical operations like addition, subtraction,
multiplication, division, etc. If you give your computer the instruction 2+3, this will be
included in arithmetic operation and it is executed by Arithmetic Unit.

Logical Operation: this is concerned with the comparison of data and it is called logical
operation. It includes operators like less than, greater than, equal to, less or equal to,
greater or equal to, different from, etc. e.g. if mark>80, grade is 'A'.

A processor register (CPU register) is one of a small set of data holding places that are part of the
computer processor. A register may hold an instruction, a storage address, or any kind of data (such as a
bit sequence or individual characters). Some instructions specify registers as part of the instruction.

iii) Output Devices


Output devices are used to get data out of a computer so that it can be examined, analyzed or
distributed to others. It converts information from machine-understandable form to a human
understandable form. The outputs are of two types: Softcopy: displayed on monitor, projector, or
similar devices and Hardcopy: printed on paper

Examples

 The Visual Display Unit (VDU) or monitor or screen


 Printers (dot matrix, daisy wheel, laser printers)
 Plotters
 Voice (audio) response unit
 Disk drives

Page 11
Introduction to computer & programming

iv) Storage Devices


One of the unique features of computers is storage. Data can be stored on different storage media
temporarily or permanently. Storage devices can be categorized into to as:

1. Primary storage device


2. Secondary storage device

1. Primary Memory / Main Memory

Primary memory, also called Main memory, refers to integrated circuit that stores program
instructions and data. The CPU closely works with the main memory to perform its activities.
Memory stores three things:

 Operation system software instructions


 Application software instruction
 Data that is being processed
Depending on the type of information they store and the technology used, the primary memory
can be categorized into three:

 RAM (Random Access Memory)


 ROM (Read Only Memory)
RAM is temporary storage i.e. the data is lost when the computer is off unlike secondary
storage. Because of this it is called volatile memory.

Why is it volatile? It uses electric power to store data. When you write anything on your
computer, first it is stored on RAM. When you save the file, it is transferred into secondary
storage.

RAM has differing capacity, the common ones being 128, 256, and 512.It is directly accessible
by CPU. It is called RAM because each memory location can be accessed randomly using
memory address. Each unit in RAM has memory address by which it can be easily
accessed/referenced.
Page 12
Introduction to computer & programming

ROM stores data and programs that are permanently required by the computer. They have
programs built into them at the factory and that program could not be changed or erased by the
user, but read. It is non-volatile, read-only (not changeable) memory. Read-only means data can't
be altered or erased but read.

2. Secondary Storage

Secondary storage (also called auxiliary storage) supplements the primary memory. It takes
many forms. It includes punched cards, punched paper tape, magnetic tape, magnetic disk and optical
disk. Based on information access, secondary storage devices are divided into two:

 Sequential Access: information is accessed sequentially. To access information on such


media, we start from the beginning and read through to the end. Jumping to some part is not
possible. E.g. tape recorder cassette
 Random Access: information can be accessed in any order. You can access the first or the
last part of information by jumping others. E.g. floppy disk

Computer Software
Computer hardware is directed by a set of instructions. Without these instructions, computers can
do nothing. These set of instructions are called software (also called programs). One or more
programs are termed as software. We use programming languages to write these instructions.
Examples of programming languages include C, C++, Visual Basic, Java, etc.

Software is categorized into two:

 System Software
 Application Software
 System Software
System software consists of programs that are related to controlling the actual operations
of the computer equipment/resource.

Page 13
Introduction to computer & programming

There are three types of system software:

 Operating System
 Utility Software
 Language translators
Operating system manage resources, provides a user interface, and run application software. It
organizes resources such as keyboard, mouse, printer, monitor, etc. It also presents GUI (Graphical User
Interface) to the user for easy use of computer. It makes complex hardware more users friendly i.e. it acts
between the user and hardware. Operating system coordinates the activity between the user and the
computer

Utilities software is programs that make computing easier. They perform specific tasks related
to managing computer resources or files. There are different utility programs:

i) Troubleshooting programs: enable us to recognize and correct computer problems


before they become serious.
ii) Anti-virus programs: they protect your computer against viruses or other malicious
programs that damage computer. E.g MacAfee, Sophos, Norton, etc.
iii) File compression programs: are used to reduce the size of files or data so that it takes
less storage space or network band. E.g WinZip, WinRAR, etc.
iv) Uninstall programs: this software enables us to safely and completely remove unneeded
programs/software from your computer.
v) Back up software: with the help of this software, we can make copies of files to be used
in case of the original data is lost/damaged. This copy is called back up.
vi) Screen savers: helps to prevent your work from being seen by others if you leave your
computer idle for some time.
Language translators are used to convert the programming instruction written by users into
binary code that the computer can understand. They are written for specific programming
languages and computer system.

Page 14
Introduction to computer & programming

 Application Software
Application software performs useful work for the user. These useful works could be:

 Word processing-document creation


 Spreadsheet-electronic calculation
 Data base system
 Email/communicating-email sending and reading
Users use this software to perform different activities like calculation, video editing, word
processing, presentation, etc.

DATA REPRESENTATION IN COMPUTER SYSTEM

Data is a collection of raw unprocessed facts, figures, and symbols.

Information is data that is organized, meaningful, and useful. To process data into information,
a computer uses hardware and software.

Data Representation
Every computer stores numbers, letters, & other special characters in a coded form. But a
computer stores those data’s in a binary number system. There are two main code forms that
every number, letter and special character is represented in a computer system. Before discussing
those codes let have a look at binary number system.

The Binary Number System

Computers are electronic devices that use electrical patterns to represent numbers. Modern digital
computers recognize only two electrical states—ON and OFF— but their memories contain
millions of transistors that can be either on or off. Working with numbers in computers is like
making numbers out of a row of lights that can be switched on and off independently. Because
there are only two ways to represent a number, computers use the binary number system (base 2
or radix 2).

Page 15
Introduction to computer & programming

A relationship among Binary & other number system

Decimal Hexadecimal Octal Binary


0 0 0 0
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000
9 9 11 1001
10 A 12 1010
11 B 13 1011

Decimal Hexadecimal Octal Binary

12 C 14 1100
13 D 15 1101
14 E 16 1110
15 F 17 1111
16 10 20 0001 0000

1.1.1 Why Binary?


Why do we go for binary numbers instead of decimal numbers?’ The reasons are as follows:

1. The 1st reason is that the electronic & electrical components, by their very nature, operate in
a binary mode. Information is handled in the computer by electronic/electrical components such
as transistors, semiconductors, wires, etc all of which can only indicate 2 states or conditions –
on(1) or off(0).
Transistors are either conducting (1) or non conducting (0); a voltage is present (1) or absent (0)
in wire. The binary number system, which has only two digits (0&1), is most suitable for
expressing the two possible states

2. The second reason is that the computer circuits only have to handle two binary digits rather
than ten decimal digits. This greatly simplifies the internal circuit design of computers, resulting
in less expensive & more reliable circuits.

Page 16
Introduction to computer & programming
3. Finally, the binary system is used because everything that can be done in decimal number system
(addition, subtraction, division & multiplication) can also be done in binary number system.

1.2 Units of Data Representation

When data is stored, processed, or communicated within the computer system, it is “packed” in units.
Arranged from the smallest to the largest, the units are called bits, bytes, & words.

Bits, Bytes and Words

Bits

A bit is a single binary digit. A bit may have a value of 0 or 1. In a computer, a switch or transistor
that is off represents a 0, and a switch or transistor that is on represents a 1. A bit is represented by the
numbers 1, & 0, which correspond to the states on & off, true & false, or yes & no.

Bytes

Most computers work with groups of 8 bits, which is called a byte. To make it easier to read, the 8
binary digits in a byte are divided into two groups of four, called nibbles, when they are written.

One byte may hold binary numbers ranging in value from 0000 0000 (base 2) to 1111 1111 (base 2),
or from 0 (base 10) to 255 (base 10). Counting 0 as a value, one byte can contain 256 values. For
many computer variables, the maximum value is 255 because the computer wants to store the value in
a single byte, or you are limited to 256 choices.

Words

- Bytes are combined into groups of 1 to 8 bytes called words.


- Words refer to the number of bits that a computer process at once.
- Typically, word lengths are 8 bits, 16 bits, 32 bits & 64 bits.

Page 17
Introduction to computer & programming

Computer Number systems


A decimal integer is converted to any other base, by using the division operation.

Page 18
Introduction to computer & programming

Hexadecimal number system

 Computers use hexadecimal because it represents binary values in a compact form.

 Base 16 number system

 Hex means six, Deci means ten

 16 symbols are used - 0 thru 9, A, B, C, D, E, F

 0-9 = 0-9

 A = 10 D = 13

 B = 11 E = 14

 C = 12 F = 15

Conversion of decimal to hexadecimal

 Reverse the process

 59 is 3B in hexadecimal

 16 goes into 59 three times

 It will take “11” or “B” to get to 59 (59 = 3*16+11) =3 * B (11)

Example: Convert the decimal number 90 into hexadecimal number

Page 19
Introduction to computer & programming
Solution:

90 = (5 * 16) +10 this implies 5A because the number 10 is represented by A in hexadecimal.

90 dividing by 16 and collecting the remainder of the digit we can verify it.

 A binary number can be converted into octal or hexadecimal number using a shortcut
method. The shortcut method is based on the following information:-

 An octal digit from 0 to 7 can be represented as a combination of 3 bits, since 23 = 8.


 A hexadecimal digit from 0 to 15 can be represented as a combination of 4 bits, since 24
= 16.

1.3 Decimal to Other Base System

Steps

 Steps 1 - Divide the decimal number to be converted by the value of the new base.
 Step 2 - Get the remainder from Step 1 as the rightmost digit (least significant digit) of
new base number.
 Step 3 - Divide the quotient of the previous divide by the new base.
 Step 4 - Record the remainder from Step 3 as the next digit (to the left) of the new base
number.

Repeat Steps 3 and 4, getting remainders from right to left, until the quotient becomes zero in
Step 3.

The last remainder thus obtained will be the most significant digit (MSD) of the new base
number.

1.3.1 Example

Decimal Number: 2910

Calculating Binary Equivalent:

Step Operation Result Remainder

Step 1 29 / 2 14 1

Step 2 14 / 2 7 0

Page 20
Introduction to computer & programming

Step 3 7/2 3 1

Step 4 3/2 1 1

Step 5 1/2 0 1

As mentioned in Steps 2 and 4, the remainders have to be arranged in the reverse order so that
the first remainder becomes the least significant digit (LSD) and the last remainder becomes the
most significant digit (MSD).

Decimal Number: 2910 = Binary Number: 111012.

1.4 Other base system to Decimal System

Steps

 Steps 1 - Determine the column (positional) value of each digit (this depends on the
position of the digit and the base of the number system).
 Step 2 - Multiply the obtained column values (in Step 1) by the digits in the
corresponding columns.
 Step 3 - Sum the products calculated in Step 2. The total is the equivalent value in
decimal.

1.4.1 Example

Binary Number: 111012

Calculating Decimal Equivalent:

Step Binary Number Decimal Number

Step 1 111012 ((1 x 24) + (1 x 23) + (1 x 22) + (0 x 21) + (1 x 20))10

Step 2 111012 (16 + 8 + 4 + 0 + 1)10

Step 3 111012 2910

Binary Number: 111012 = Decimal Number: 2910

Page 21
Introduction to computer & programming

1.5 Other Base System to Non-Decimal System

Steps

 Steps 1 - Convert the original number to a decimal number (base 10).


 Step 2 - Convert the decimal number so obtained to the new base number.

1.5.1 Example

Octal Number: 258

Calculating Binary Equivalent:

1.5.2 Step 1: Convert to Decimal


Step Octal Number Decimal Number

Step 1 258 ((2 x 81) + (5 x 80))10

Step 2 258 (16 + 5 )10

Step 3 258 2110

Octal Number: 258 = Decimal Number: 2110

1.5.3 Step 2: Convert Decimal to Binary


Step Operation Result Remainder

Step 1 21 / 2 10 1

Step 2 10 / 2 5 0

Step 3 5 / 2 2 1

Step 4 2 / 2 1 0

Step 5 1 / 2 0 1

Decimal Number: 2110 = Binary Number: 101012

Octal Number: 258 = Binary Number: 101012

Page 22
Introduction to computer & programming

 Shortcut method - Binary to Octal

Steps

 Step 1 - Divide the binary digits into groups of three (starting from the right).
 Step 2 - Convert each group of three binary digits to one octal digit.

1.5.4 Example

Convert Binary Number: 101012 to Octal Or

Calculating Octal Equivalent:

Step Binary Number Octal Number

10
Step 1 010 101
1012

Step 2 101012 28 58

Step 3 101012 258

Binary Number: 101012 = Octal Number: 258

1.6 Binary to Octal

An easy way to convert from binary to octal is to group binary digits into sets of three, starting
with the least significant (rightmost) digits.

Binary: 11100101 = 11 100 101

011 100 101 Pad the most significant digits with zeros if
necessary to complete a group of three.

Then, look up each group in a table:


Binary: 000 001 010 011 100 101 110 111

Octal: 0 1 2 3 4 5 6 7

Page 23
Introduction to computer & programming

Binary = 011 100 101

Octal = 3 4 5 = 345 oct

 Shortcut method - Octal to Binary

Steps

 Steps 1 - Convert each octal digit to a 3-digit binary number (the octal digits may be
treated as decimal for this conversion).
 Step 2 - Combine all the resulting binary groups (of 3 digits each) into a single binary
number.

1.6.1 Example

Octal Number: 258

Calculating Binary Equivalent:

Step Octal Number Binary Number

Step 1 258 210 510

Step 2 258 0102 1012

Step 3 258 0101012

Octal Number: 258 = Binary Number: 101012

Converting from octal to binary is as easy as converting from binary to octal. Simply look up
each octal digit to obtain the equivalent group of three binary digits.

Octal: 0 1 2 3 4 5 6 7

Binary: 000 001 010 011 100 101 110 111

Page 24
Introduction to computer & programming

Octal = 3 4 5

Binary = 011 100 101 = 011100101 binary

 Shortcut method - Binary to Hexadecimal

Steps

 Step 1 - Divide the binary digits into groups of four (starting from the right).
 Step 2 - Convert each group of four binary digits to one hexadecimal symbol.

1.6.2 Example

Binary Number: 101012

Calculating hexadecimal Equivalent:

Step Binary Number Hexadecimal Number

Step 1 101012 0001 0101

Step 2 101012 110 510

Step 3 101012 1516

Binary Number: 101012 = Hexadecimal Number: 1516

1.7 Binary to Hexadecimal

An equally easy way to convert from binary to hexadecimal is to group binary digits into sets of
four, starting with the least significant (rightmost) digits.

Binary: 11100101 = 1110 0101

Then, look up each group in a table:


Binary: 0000 0001 0010 0011 0100 0101 0110 0111

Page 25
Introduction to computer & programming

Hexadecimal: 0 1 2 3 4 5 6 7

Binary: 1000 1001 1010 1011 1100 1101 1110 1111

Hexadecimal: 8 9 A B C D E F

Binary = 1110 0101

Hexadecimal = E 5 = E5 hex

Example

Convert the following binary numbers into decimal numbers.

 Conversion of binary number 1010101000010111to hexadecimal number is -


AA1716

1.8 Shortcut method - Hexadecimal to Binary

Steps

 Steps 1 - Convert each hexadecimal digit to a 4-digit binary number (the hexadecimal
digits may be treated as decimal for this conversion).
 Step 2 - Combine all the resulting binary groups (of 4 digits each) into a single binary
number.

1.8.1 Example

Hexadecimal Number: 1516

Calculating Binary Equivalent:

Step Hexadecimal Number Binary Number

Step 1 1516 110 510

Step 2 1516 00012 01012

Page 26
Introduction to computer & programming

Step 3 1516 000101012

Hexadecimal Number: 1516 = Binary Number: 101012

Converting from hexadecimal to binary is as easy as converting from binary to hexadecimal.


Simply look up each hexadecimal digit to obtain the equivalent group of four binary digits.

Hexadecimal: 0 1 2 3 4 5 6 7

Binary: 0000 0001 0010 0011 0100 0101 0110 0111

Hexadecimal: 8 9 A B C D E F

Binary: 1000 1001 1010 1011 1100 1101 1110 1111

Hexadecimal = A 2 D E

Binary = 1010 0010 1101 1110 = 1010001011011110 binary

1.9 Octal to Hexadecimal

When converting from octal to hexadecimal, it is often easier to first convert the octal number
into binary and then from binary into hexadecimal. For example, to convert 345 octal into hex:

(From the previous example)


Octal = 3 4 5

Binary = 011 100 101 = 011100101 binary

Drop any leading zeros or pad with leading zeros to get groups of four binary digits (bits):
Binary 011100101 = 1110 0101

Then, look up the groups in a table to convert to hexadecimal digits.

Page 27
Introduction to computer & programming

Binary: 0000 0001 0010 0011 0100 0101 0110 0111

Hexadecimal: 0 1 2 3 4 5 6 7

Binary: 1000 1001 1010 1011 1100 1101 1110 1111

Hexadecimal: 8 9 A B C D E F

Binary = 1110 0101

Hexadecimal = E 5 = E5 hex

Therefore, through a two-step conversion process, octal 345 equals binary 011100101 equals
hexadecimal E5.

1.10 Hexadecimal to Octal

When converting from hexadecimal to octal, it is often easier to first convert the hexadecimal
number into binary and then from binary into octal. For example, to convert A2DE hex into
octal:

(From the previous example)


Hexadecimal = A 2 D E

Binary = 1010 0010 1101 1110 = 1010001011011110 binary

Add leading zeros or remove leading zeros to group into sets of three binary digits.

Binary: 1010001011011110 = 001 010 001 011 011 110

Then, look up each group in a table:


Binary: 000 001 010 011 100 101 110 111

Octal: 0 1 2 3 4 5 6 7

Page 28
Introduction to computer & programming

Binary = 001 010 001 011 011 110

Octal = 1 2 1 3 3 6 = 121336 octal

Therefore, through a two-step conversion process, hexadecimal A2DE equals binary


1010001011011110 equals octal 121336.

1.11 Hexadecimal to Decimal

Converting hexadecimal to decimal can be performed in the conventional mathematical way, by


showing each digit place as an increasing power of 16. Of course, hexadecimal letter values need
to be converted to decimal values before performing the math.

Hexadecimal: 0 1 2 3 4 5 6 7

Decimal: 012 3 4 5 6 7

Hexadecimal: 8 9 A B C D E F

Decimal: 8 9 10 11 12 13 14 15

A2DE hexadecimal:
= ((A) * 163) + (2 * 162) + ((D) * 161) + ((E) * 160)
= (10 * 163) + (2 * 162) + (13 * 161) + (14 * 160)
= (10 * 4096) + (2 * 256) + (13 * 16) + (14 * 1)
= 40960 + 512 + 208 + 14
= 41694 decimal

1.12 Decimal to Hexadecimal

Here is an example of using repeated division to convert 1792 decimal to hexadecimal:

Hexadecimal
Decimal Number Operation Quotient Remainder
Result

1792 ÷ 16 = 112 0 0

112 ÷ 16 = 7 0 00

Page 29
Introduction to computer & programming

7 ÷ 16 = 0 7 700

0 Done.

The only addition to the algorithm when converting from decimal to hexadecimal is that a table
must be used to obtain the hexadecimal digit if the remainder is greater than decimal 9.

Decimal: 01234567

Hexadecimal: 0 1 2 3 4 5 6 7

Decimal: 8 9 10 11 12 13 14 15

Hexadecimal: 8 9 A B C D E F

Page 30

You might also like