1 Unit-Basics of Programming
1 Unit-Basics of Programming
Covered Topics
What is language?
What is mean by programming language?
What is the purpose of programming language?
Types of programming language
Low level language
High level language
What is C?
Popular applications developed by using C
o popular os
o popular db
o popular language
What is procedural oriented programming paradigm?
What is the advantage of C is having procedural oriented language?
What is mean by block structured programming?
What is the advantage of C is having block structured language?
What is program?
Translator?
o compiler
o interpreter
o assembler
What is the difference between compiler and interpreter?
Source code
Compile code
Input vs Output
Statement vs Program vs Software?
Types of software
What is bug/error in the program?
Types of errors
RAM vs HD vs CPU
CPU architecture
RAM architecture
How to program going to be executed in the memory?
Number System
Types of Number system
Number system conversion
1. Decimal to binary
2. Decimal to octal
3. Decimal to hexa-decimal
4. Binary to decimal
5. Octal to decimal
6. Hexa-decimal to decimal
7. Binary to octal
8. Octal to binary
9. Binary to hexa-decimal
10. Hexa decimal to binary
2|Page C Language Material by Girraj Sharma
Q: What is language?
Ans: Language is a common medium (understanding) by using which two person can communicate
with each other.
4|Page C Language Material by Girraj Sharma
In this diagram Person1 can‟t communicate to person2 using hindi because hindi is unknown to the
person2, similarly person2 can‟t communicate with person1 because English is unknown language to
the person1.
If two persons want to communicate with each other then there should be some common medium
between them in the form of language or communication can be happened with the help of translator.
After learning the English by the person1 then these persons can communication with each other
using English because English is common language for these persons.
There is another solution is that Person1 and Person2 should hire another person3 that are having a
knowledge of hindi and English both. In this communication person3 acts like a translator that can
translates the hindi instructions into English format, and English statement into hindi format.
Here hindi and English are human language it is not a programming language hence it can‟t be used
for programming purpose.
If any human person want to communicate with the computer then again some common medium is
required between the human person and computer. But computer doesnt know the meaning of human
language such as hindi, english etc.
Inside any computer machine all instructions are executed by the processor. And processor is a fastest
electronic device which can interpret only binary language (low level language). But as a human
being binary language is too difficult for learning us.
In this diagram if we want to performing some work by the computer, then we need to sending our
requirement to the computer. If we are sending our requirement to the computer using hindi/English
the computer is not able to interpret that hindi or English instructions because computer doesn‟t
knowing the human language. Then what can we do, we are having four choices which is as given
5|Page C Language Material by Girraj Sharma
Choice1: As computer is knowing only the binary language if we are also learning the binary
language that we can send our requirement in binary format then computer can easily interpret our
binary instructions. But learning the binary language is too much difficult for us we can‟t learn easily
hence it is never recommended to learn the binary language (machine language).
Choice2: If computer will learn the human language (hindi, English) then also communication is
possible. But as we know computer is a machine computer is not a human that‟s what computer can‟t
learn anything.
Choice3: We should hire some another person as a translator that will know the human language as
well as binary language. So that using this person we can talk with the computer. But problem is that
there is no such person (translator) available in the market that can translate the human language into
the binary language.
Choice4: There some languages are available in the market that can be easily translated into binary
language by the translators, these languages are nothing but known as programming language ie. C,
C++, Java, .Net, Python etc. And these programming language can easily learn by us. If we will learn
any programming language then using these programming language we can talk (communicate) with
the computer.
After learning the programming language (C,C++) we can write our requirements (program) and these
programming language code will be converted into binary language by the translators(compiler) into
computer understandable code (binary language) and this code can be easily interpreted by the
processor.
Q: What is program?
Program is a group of related statements which will going to be provide the solution of real word
problem
Ex: To perform the addition, calculating simple interest, transferring the balance from one
account to another bank account, converting temperature Fahrenheit to Celsius and Celsius to
Fahrenheit.
Q: What is translator?
Any software that can translates the high level language code into low level language code known
as translator.
In order to running the program of any high level language by the computer, program should be
converted into computer (hardware) understandable format (machine code/binary code). For this
translation purpose we have required some software that is nothing but translators.
The main role of translator is
a. Check the syntaxes of the program, if any syntactical mistake is doing by the programmer
then translator will not compile the program.
b. If program is syntactically correct then translator will translates program into low level format
Interpreter:
Interpreter will perform the code translation statement by statement. It will translate only one
statement at a time. After translating the current statement then only next statement is going to be
translated by the interpreter.
Interpreter will checks the syntax and translate the code into low level format statement by
statement. After translating the current statement then interpreter will checks the syntax of next
statement and will translates into machine code format and same process is applied for the all
statements. Since compiler will translates only one statement at a time therefore it can show only
single error message at a time. After correcting the current error message then only we are able to
check the next error.
Assembler:
Assembler will be used to translate the assembly language code into the low level language.
Q: What is input?
During execution any data or values receiving/taken by the program from another
device(keyboard) known as input to the program
Ex: Entering password and withdrawal amount using ATM machine, entering username and
password in the gmail account, entering two numbers for addition etc.
In an addition program:-
Enter first number: 10
Enter second number: 20
In this example the number 10 and 20 are the inputs for the program
Q: What is output?
During execution any data/values send by the program to the another device(monitor) known as
output of the program.
9|Page C Language Material by Girraj Sharma
In an addition program:-
Enter first number: 10
Enter second number: 20
Result is 30
In this example 30 is the output of this program
Statement vs Program vs Software:
Statement: statement is separated by using semicolon, statement is a line in the program which ended
by the semicolon.
Ex:
Statement1;
Statement2;
…
Statement-n;
Any statement can be divided into many lines.
Ex:
int x = 10; is a statatment which can be written into multiple lines in given way
int
x
=
10;
In a single line we can place multiple statements
int x = 10; clrscr(); printf(“%d”,x);
In this line there are three statements
Program:
Program is a collection of related statements which is going to be provide a solution of single
problem.
Ex: program that will performs the addition of two numbers containing three statements. These
three statement will performs the addition of two numbers
int add(int x,int y)
{
int res;
res = x + y;
return res;
}
Software:
Software is a collection of related programs
In this example there are four programs, program1 for addition, program2 for subtraction,
program3 for subtraction, program4 for division and these all programs together forms a
calculator software. In this example each program contains three statements and these three
statements together performs a single arithmetic operations etc.
10 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Types of software:
There are two types of software system software and application software, C language is most popular
for developing system software. ie Compiler, Operating system, device drivers, Anti-virus
Q: What is error?
Error is bug in the program which may terminating the execution of program abnormally.
Any unknown (invalid) statement in the program is nothing but error.
Any statement in the program which is against the rule of compiler is nothing but error.
Abnormal behaviour of the program is nothing but error.
Whenever any program is crashed during the execution is nothing but error.
Whenever any program is giving the unexpected results is nothing but error.
Types of errors:
11 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Syntax error:
Any error message which is raised by the compiler during compilation known as syntax error or
compiletime error.
It is syntactical mistake in the program, Any statement is violating the rules of the C compiler is
known as syntax error. These errors are reported by the compiler at compiletime therefore it is also
known as compiletime error.
Compiletime errors can be traced by looking into the source code of the program. Syntax errors also
known as static errors.
If we are able to find out the error based on source code such type of error is known as static error or
compiletime error.
Runtime Error:
Any error message which is given by the operating system due to wrong input given by user is known
as runtime error.
The main cause of runtime error is wrong input provided by the user or required resource(file) not
available.
Runtime error might not be traced based on source code. Because its behaviour completely depends
upon runtime input
Ex: dividing any number by the zero, file not found, calculating the square root of negative numbers
etc.
Linker Error:
Any error raised by the linker at the time of linking phase known as linker error.
The main cause of linker errors are definition of any variable or function is not found at the time of
linking phase.
If variable and function declaration is available but definition is not found then in this case linker will
gives an error during linking phase of the program
Note: We will discussing linker errors later in details
Logical Error:
It is the logical mistakes done by the programmer in the code, in case of logical error our program will
compile and run successfully but not giving the expected results.
It is most dangerous in the program. We should take care about the logical error seriously.
HD is a physical device but we can divide a HD memory into any number of logical partitions in
the form of drives (disk volume).
All files and installed software (including OS) will store inside HD memory permanently.
The source file and executable file of C program also will be stored inside HD memory.
In the below diagram HD memory is logically divided into C,D,E …drives. If we need search an
image file the we can apply the search operation only on the E:\ dirve there is no need of
searching in the remaining drives.
RAM:
RAM is a physical device that can store the data temporary during execution of the program.
All running application will be available inside a RAM memory.
To run any application by the processor with the help of OS required applications will be loaded
from HD to RAM memory.
Processor always takes the required executable instructions directly from the RAM memory only,
Processor will not communicate with the HD memory directly. Because the read/write speed of
HD memory is to slow as compare to RAM. RAM memory will provides fast access of data to the
processor.
In order to improve the performance before running any application OS will loads this application
(program) from HD to RAM memory.
RAM is also physical device, it is also divided into multipal logical partitions to manage the data
category wise in the form of variables.
ie. Data area, heap area, method area, stack area.
13 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Note: We will discussing about logical partition of the RAM later in details.
All global variables and static variables will be created inside data area
All dynamic memory allocations are performed inside heap
Before execution the compile code of any program will be loaded from HD to RAM memory
inside method area.
All local variables (auto variables) will be created inside stack area
Number System
Number system is way representing any number in different-different types.
There are four types popular number systems are available that is supported by the computer
a. Binary number system or Base 2 number system
b. Octal number system or Base 8 number system
c. Decimal number system or Base 10 number system
d. Hexa-Decimal number system or Base 16 number system
Binary number system:
In the binary number system there two distinct digits are allowed 0 and 1 only. Therefore it is also
known as base2 number system
At memory level any information will be stored in the form of binary number system
Step4: After dividing the resultant quotient(1) by 2 again we get the reminder as 1
Step5: Collect all the reminder together in order of step4, step3, step2, step1
Ans: 1 1 0 1
(13)10 = (1101)2
Example2: (25)10 = ( ? )2
Example3: (93)10 = ( ? )2
(512)10 = ( __________________________________ )2
(1024)10 = ( __________________________________ )2
(275)10 = ( __________________________________ )2
(369)10 = ( __________________________________ )2
(32767)10 = ( __________________________________ )2
(32768)10 = ( __________________________________ )2
(65536)10 = ( __________________________________ )2
(65535)10 = ( __________________________________ )2
Note: Before solving these practice questions you should practice the previous solved example1,
example2, example3 for the reference purpose
Step3: Collect all the reminder together in reverse order bottom to top order
Ans: 1 1 3
Example2: (100)10 = ( ? )8
Example3: (165)10 = ( ? )8
(65536)10 = ( _______________ )8
(65535)10 = ( _______________ )8
Practice: Match the decimal number to its equivalent octal number.
Decimal no octal no
25 377
128 177
18 31
127 200
255 22
Practice: Match the decimal number to its equivalent octal number.
Decimal no Octal no
350 4572
325 4526
2004 505
2390 3724
2426 536
After dividing the decimal number by 16 if the reminder will be in the range of 10 to 15 then it is
replaced by the hexa-decimal digits A to F.
Mapping table
A - 10
B - 11
C - 12
D - 13
E - 14
F - 15
Reminder 10 would be replaced by A, 11 replaced by the B, 12 replaced by the C, 13 replaced by the
D, 14 replaced by the E and 15 replaced by the F.
After dividing the decimal number 30 by the hexa-decimal base 16 we get the reminder as 14 and
according to mapping 14 should be replaced by the E. In this example we need not to be divide the 14
again by 16 because reminder 14 is already less than of 16.
Step2: Multiply each binary digits from right to left in terms of 2p in the given way
Dn*2n-1 +…..D4*23+D3*22+D2*21+D1*20
Step3: And solve the final expression of step2 and you will get the decimal number which is
equivalent of given binary number.
Example1: (1101)2 = ( ? )10
Step1: 1 + 1 + 0 + 1
Step2: 1*23 + 1*22 + 0*21 + 1*20
Step3: 1*8 + 1*4 + 0*2 + 1*1
8 + 4 + 0 + 1 = 13
(1101)2 = ( 13 )10
Practice1: Convert the given binary numbers into the decimal and fill in the blanks
(1)2 = (________ )10
(10)2 = (________)10
(100)2 = (________)10
(1000)2 = (________)10
(10000)2 = (________)10
(100000)2 = (________)10
(1000000)2 = (________ )10
(10000000)2 = (________)10
(11)2 = (________)10
(111)2 = (________)10
(1111)2 = (________)10
(11111)2 = (________)10
(111111)2 = (________ )10
(1111111)2 = (________)10
(11111111)2 = (________)10
21 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Practice2: Convert the given binary numbers into the decimal and fill in the blanks
(1010)2 = (________)10
(1101)2 = (________)10
(1111)2 = (________)10
(101101)2 = (________)10
(10000)2 = (________)10
(11111)2 = (________)10
(1000000)2 = (________)10
(111111)2 = (________)10
(10000000)2 = (________)10
(1111111)2 = (________)10
(11111111)2 = (________)10
(100000000)2 = (________)10
(10101011)2 = (________)10
(1000000000000000)2 = (________ )10
(111111111111111)2 = (________)10
(1111111111111111)2 = (________)10
(250)2 = (________)10
(12345)2 = (________ )10
Step2: Multiply each octal digit from right to left in terms of 8p in the given way
Dn*8n-1 +…..D4*83+D3*82+D2*81+D1*80
Step3: And solve the final expression of step2 and you will get the decimal number which is
equivalent to the given octal number.
Example1: (253)8 = ( ? )10
Step1: 2 + 5 + 3
Step2: 2*82 +5*81 + 3*80
22 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Practice: Convert the given binary numbers into the decimal and fill in the blanks
(10)8 = ( _____________)10
(125)8 = ( _____________)10
(520)8 = ( _____________)10
(728)8 = ( _____________)10
(2409)8 = ( _____________)10
(1234)8 = ( _____________)10
(242)8 = ( _____________)10
(5000)8 = ( _____________)10
(5030)8 = ( _____________)10
(42322)8 = ( _____________)10
(23252)8 = ( _____________)10
(20238)8 = ( _____________)10
(9353)8 = ( _____________)10
(10)8 = ( _____________)10
(10)8 = ( _____________)10
(10)8 = ( _____________)10
Practice: Match the octal number to its equivalent decimal number.
Octal no Decimal no
423 275
6343 3299
346 3822
7356 230
3467 1847
As we know base of hexa decimal number is 16, and base of decimal number is 10.
Step1: Write all the hexa decimal digits in the form of summation, Alphabetical digits(ABCDEF)
should replaced by the corresponding digits positionally. Positional digits of A is 10, B is 11, C is
12, D is 13, E is 14 and F is 15.
Dn+…..D4+D3+D2+D1
Step2: Multiply each hex digit from right to left in terms of 16p in the given way
Dn*16n-1 +…..D4*163+D3*162+D2*161+D1*160
Step3: And solve the final expression of step2 and you will get the decimal number which is
equivalent to the given hexa decimal number.
Practice: Convert the given hexa decimal numbers into the decimal and fill in the blanks
(24)16 = (_______________)10
(48)16 = ( _______________)10
(89)16 = ( _______________)10
(4D)16 = ( _______________)10
24 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
(2AF)16 = ( _______________)10
(B15)16 = ( _______________)10
(3214)16 = ( _______________)10
(abcd)16 = ( _______________)10
(29E)16 = ( _______________)10
(5232)16 = ( _______________)10
(23ED)16 = ( _______________)10
(512)16 = ( _______________)10
(42GD)16 = ( _______________)10
(2F5G)16 = ( _______________)10
Practice: Match the hexa decimal number to its equivalent decimal number.
Hexa-Decimal Decimal
423 838
6343 1059
346 29526
7356 25411
3467 13415
Binary to octal
As we know base of binary number is 2, and the base of octal number is 8
We can convert the any binary number into the octal number by using two methods.
a. Indirect method (Binary to decimal, Decimal to octal)
b. Direct method (Binary to octal)
Binary to octal conversion using indirect method:
In this method two level conversion is required, first we should convert the binary into decimal
number, and then resultant decimal number should be converted into octal
Step1: Binary to Decimal
Step2: Decimal to Octal
Example1: (11010)2 = ( ? )8
Step1: Binary to decimal
(11010)2 = ( ? )10
1+1+0+1+0
1*24 + 1*23 + 0*22 + 1*21 + 0*20
1*16 + 1*8 + 0*4 + 1*2 + 0*1
16 + 8 + 0 + 2 + 0 = (26)10
Step2: Decimal to octal
( 26 )10 = ( ? )8
Note: This method is too lengthy process of binary to octal conversion, therefore recommended to use
direct method.
25 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Step2: Replace the each group of three digits binary number by the equivalent octal digit
respectively.
Step3: And collect all octal digits together which is the final octal number for the given binary
number.
(Bn……B8 B7 B6 B4 B3 B2 B1 B0) = (On…..O2 O1 O0)
Example1: (111010)2 = ( ? )8
Step1: Create three digits group of this binary number
111 010
Step2: Replace each group of three digit binary number by the equivalent octal digit according to
previous table.
Octal digit of 111 is 7, octal digit of 010 is 2
Example2: (10110111010)2 = ( ? )8
Sol: ( 10 110 111 010)
2 6 7 2
(10110111010)2 = (2 6 7 2)8
Example3: (11011101011110110111010)2 = ( ? )8
Solve: 011 011 101 011 110 110 111 010
3 3 5 3 6 6 7 2
26 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
(11011101011110110111010)2 = (3 3 5 3 6 6 7 2 )8
Practice: Convert the given binary numbers into the equivalent octal and fill in the blanks
(11111111)2 = (_________________________)8
(100000000)2 = (_________________________)8
(1010101011)2 = (_________________________)8
(110110101011)2 = (_________________________)8
(100000000101)2 = (_________________________)8
(1101101101101100)2 = (_________________________)8
(101111100011)2 = (_________________________)8
(1110000010111011)2 = (_________________________)8
(110000011101101)2 = (_________________________)8
(10000110101010111)2 = (_________________________)8
(10101011111)2 = (_________________________)8
(11000000111111)2 = (_________________________)8
(101010101)2 = (_________________________)8
(1111111111111111)2 = (_________________________)8
(1111000011110000)2 = (_________________________)8
(110011001100110011)2 = (_________________________)8
Note: Recommended to use method2
Example1: (4025)8 = ( ? )2
Sol: 4 0 2 5
Replace all the octal digits by the binary number, write the binary in three bits for each octal
digits.
In three bits binary number of octal digit 4 is 100, binary number of octal digit 0 is 000,
binary number of octal digit 2 is 010, binary number of octal digit 5 is 101
4 0 2 5
100 000 010 101
(4025)8 = (100000010101)2
27 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Example2: (543021)8 = ( ? )2
Sol: Replace each digit by three bit binary number
5 by 101, 4 by 100, 3 by 011, 0 by 000, 2 by 010, 1 by 001
5 4 3 0 2 1
101 100 011 000 010 001
(543021)8 = (101100011000010001)2
Example3: (703040501)8 = ( ? )2
Sol: Replace each digit by three bit binary number
7 0 3 0 4 0 5 0 1
111 000 011 000 100 000 101 000 001
(703040501)8 = (111000011000100000101000001)2
Example4: (2570)8 = ( ? )2
Sol: Replace each digit by three bit binary number
2 5 7 0
010 101 111 000
Note: Write the binary number after removing all the occurrences of starting 0 from the final
binary result.
(2570)8 = (10101111000)2
Example5: (4895)8 = ( ? )2
Sol: The number 4895 is an invalid octal number because digits 8 and 9 is not allowed in
octal representation the allowed digits are between 0 to 7 only.
Practice: Convert the given octal numbers into the equivalent binary and fill in the blanks
(352343)8 = (_______________________________________)2
(5000)8 = (_______________________________________)2
(250)8 = (_______________________________________)2
(324653)8 = (_______________________________________)2
(634323)8 = (_______________________________________)2
(2343646)8 = (_______________________________________)2
(777222)8 = (_______________________________________)2
(72727)8 = (_______________________________________)2
(57235)8 = (_______________________________________)2
(745424)8 = (_______________________________________)2
(177777)8 = (_______________________________________)2
(5342634)8 = (_______________________________________)2
(6524225)8 = (_______________________________________)2
(6342424)8 = (_______________________________________)2
(72225)8 = (_______________________________________)2
Binary to Hexa-Decimal
As we know base of binary number is 2, and the base of hexa decimal number is 16
We can convert the any binary number into the hexa decimal number by using two methods.
c. Indirect method (Binary to decimal, Decimal to Hexa-decimal)
d. Direct method (Binary to Hexa-decimal)
Binary to hexa-decimal conversion using indirect method:
In this method two level conversion is required, first we should convert the binary into decimal
number, and then resultant decimal number should be converted into hexa-decimal.
Step1: Binary to Decimal
Step2: Decimal to Hexa-decimal
Note: This method is too lengthy process of binary to hexa-decimal conversion, therefore
recommended to use direct method.
E 1110
F 1111
As we seen in this table any hexa decimal digits can be made by using four binary digits. So in
this process of binary to hexa decimal conversion follow the given steps. Any hexa-decimal digit
will takes maximum four binary digits therefore in this process any hexa-decimal digit will be
formed by using pair of four binary digits
Step1: Create the four digits (bits) group of given binary number from right to left order
Bn.......... B8 B7 B6 B4 B3 B2 B1 B0
Bn.......... B8 B7B6 B4 B3 B2 B1 B0
Step2: Replace the each group of four digits binary number by the equivalent hexa decimal digit
respectively.
Step3: And collect all hexa digits together which is the final hexa decimal number for the given
binary
number.
(Bn…..B8 B7 B6 B4 B3 B2 B1 B0) = (Hn….H1 H0)
(1110000010111011)2 = (_________________________)16
(110000011101101)2 = (_________________________)16
(10000110101010111)2 = (_________________________)16
(10101011111)2 = (_________________________)16
(11000000111111)2 = (_________________________)16
(101010101)2 = (_________________________)16
(1111111111111111)2 = (_________________________)16
(1111000011110000)2 = (_________________________)16
(110011001100110011)2 = (_________________________)16
Note: To solve above examples recommended to use method2
Practice: Match the binary number to its equivalent hexa decimal number.
Binary Hexa-Decimal
10101100110 786
11110000110 665
10001110110 566
11001100101 5C6
10111000110 476
Example1: (4025)16 = ( ? )2
Sol: 4 0 2 5
Replace all the hex digits by the binary number, write the binary in four bit representation for
each hex digits separately.
In four bits binary number of hex digit 4 is 0100, binary number of hex digit 0 is 0000, binary
number of hex digit 2 is 0010, binary number of hex digit 5 is 0101
4 0 2 5
0100 0000 0010 0101
(4025)16 = (0100000000100101)2
Example2: (6AF5)16 = ( ? )2
Sol: Replace each digit by four bit binary number
6 by 0110, A by 1010, F by 1111, 5 by 0101
6 A F 5
0110 1010 1111 0101
(6AF5)16 = (11010101111 0101)2
Note: Write the binary number after removing extra beginning 0.
Example3: (FACE)16 = ( ? )2
31 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Practice: Convert the given hexa decimal numbers into the equivalent binary and fill in the blanks
(424)16 = (_______________________________________)2
(6445)16 = (_______________________________________)2
(52A5)16 = (_______________________________________)2
(B53F)16 = (_______________________________________)2
(A10BC)16 = (_______________________________________)2
(5743)16 = (_______________________________________)2
(8914)16 = (_______________________________________)2
(35EE)16 = (_______________________________________)2
(F2C5)16 = (_______________________________________)2
(346)16 = (_______________________________________)2
(AB3356)16 = (_______________________________________)2
(3785)16 = (_______________________________________)2
(CD793)16 = (_______________________________________)2
(30)16 = (_______________________________________)2
(50C)16 = (_______________________________________)2
(63467)16 = (_______________________________________)2
Practice: Match the hexa decimal number to its equivalent binary number.
Hexa Binary
3145 110001101001011
5EF3 110001111110010
634B 11000101000101
7486 101111011110011
63F2 111010010000110
Octal to Hexa-Decimal
As we know base of octal number is 8, and the base of hexa decimal number is 16
We can convert the any octal number into the hexa decimal number by using two methods.
a. Method1 (octal to decimal, Decimal to Hexa-decimal)
(octal)8 = (decimal)10 = (hexa)16
b. Method2 (octal to binary, binary to Hexa-decimal)
(octal)8 = (binary)2 = (hexa)16
Note: Octal to hexa-decimal conversion directly is not possible. Because there are no direct methods
available for octal to hexa-decimal conversion.
Note: This method is too lengthy process of binary to hexa-decimal conversion, therefore
recommended to use method2.
Hexa : 7 7 A
Final result is (3572)8 = (11101111010)2 = ( 77A )16
Practice: Convert the given octal numbers into the equivalent hexa-decimal number and fill in the
blanks.
(575)8 = (___________)16
(3765)8 = (___________)16
(287)8 = (___________)16
(33553)8 = (___________)16
(5276)8 = (___________)16
(6246)8 = (___________)16
(12345)8 = (___________)16
(634)8 = (___________)16
(25)8 = (___________)16
(10)8 = (___________)16
Practice: Match the Octal number to its equivalent hexa decimal number.
Octal Hexa-Decimal
2546 786
3606 665
2166 566
3145 5C6
2706 476
Hexa-Decimal to octal
As we know base of octal number is 8, and the base of hexa decimal number is 16
We can convert the any hexa decimal number into the octal number by using two methods.
c. Method1 (hex to decimal, Decimal to octal)
(hexa)16 = (decimal)10 = (octal)8
d. Method2 (Hex to binary, binary to octal)
(hexa)16 = (binary)2 = (octal)8
Note: Hexa to octal direct conversion method is not available therefore hexa to octal direct conversion
is not possible.
Note: This method is too lengthy process of binary to hexa-decimal conversion, therefore
recommended to use method2.
Practice: Convert the given hexa decimal numbers into the equivalent octal number and fill in the
blanks.
(CABE)16 = (___________)8
(2524)16 = (___________)8
(93A)16 = (___________)8
(A1F9)16 = (___________)8
(7634)16 = (___________)8
(DEF0)16 = (___________)8
(63EE5)16 = (___________)8
(5246)16 = (___________)8
(FADE5)16 = (___________)8
(968D)16 = (___________)8
r ) 250 ( ?
…
Example1: Convert the given decimal number in the base 2, base3, base 4, base 5.
(28)10 = (?)2 = (?)3 = (?)4 = (?)5
Sol: (28)10 = ( ? )2 In this case r is 2
(28)10 = ( ? )3 In this case r is 3
36 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Note: Before solving these practice questions you should practice the previous solved example1
Example2: (2534)6 = ( ? )4
Sol:
Step1: (2534)6 = ( ? )10
2*63 + 5 *62 + 3*61 + 4*60
2*216 + 5*36 + 3*6 + 4*1
432 +180 + 18 + 4
(2534)6 = (634)10
Step2: (634)10 = (?)4
38 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Practice: Convert the given hexa decimal numbers into the equivalent octal number and fill in the
blanks.
(12102)3 = (___________)2 = (___________)4 = (___________)5
(2431)5 = (___________)2 = (___________)3 = (___________)4
(6352)7 = (___________)6 = (___________)5 = (___________)4
(42321)6 = (___________)2 = (___________)4 = (___________)5
First 16 decimal, binary, octal, hexa decimal number table
Decimal Binary Octal Hexa-decimal
0 0000 0 0
1 0001 1 1
2 0010 2 2
3 0011 3 3
4 0100 4 4
5 0101 5 5
6 0110 6 6
7 0111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
We can convert any binary number into the decimal value using this table by replacing the all
occurrences of the binary bit 1 by the corresponding decimal value positionally according to the
given series and calculate the sum
39 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
If position of any occurrence of 1 in the binary bit is 16 then replace the binary bit 1 by the
32768(215)
If position of any occurrence of 1 in the binary bit is 9 then replace the binary bit 1 by the 256(215)
If position of any occurrence of 1 in the binary bit is 5 then replace the binary bit 1 by the
16(25)…etc. Apply this procedure for all occurrences of the 1 in the binary number
Ex1: (11111)2 = (?)10
Sol: 1 1 1 1 1
16 + 8 + 4 + 2 + 1 = 31
(11111)2 = (31)10
Ex2: (11110)2 = (?)10
Sol: 1 1 1 1 0
16 + 8 + 4 + 2 + 0 = 30
(11110)2 = (30)10
Ex3: (11100)2 = (?)10
Sol: 1 1 1 0 0
16 + 8 + 4 + 0 + 0 = 28
(11100)2 = (28)10
Ex4: (11000)2 = (?)10
Sol: 1 1 0 0 0
16 + 8 + 0 + 0 + 0 = 24
(11000)2 = (24)10
Ex5: (10000)2 = (?)10
Sol: 1 0 0 0 0
16 + 0 + 0 + 0 + 0 = 16
(10000)2 = (16)10
Ex6: (10100)2 = (?)10
Sol: 1 0 1 0 0
16 + 0 + 4 + 0 + 0 = 20
(10100)2 = (20)10
Ex7: (10110)2 = (?)10
Sol: 1 + 0 + 1 + 1 + 0
16 + 0 + 4 + 2 + 0 = 22
(10110)2 = (22)10
Ex8: (10101010)2 = (?)10
Sol:
(10101010)2 = (170)10
Practice1: Convert the given binary numbers into the decimal and fill in the blanks
(1)2 = (________ )10
(10)2 = (________)10
(100)2 = (________)10
(1000)2 = (________)10
(10000)2 = (________)10
40 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
(100000)2 = (________)10
(1000000)2 = (________ )10
(10000000)2 = (________)10
(11)2 = (________)10
(111)2 = (________)10
(1111)2 = (________)10
(11111)2 = (________)10
(111111)2 = (________ )10
(1111111)2 = (________)10
(11111111)2 = (________)10
Practice2: Convert the given binary numbers into the decimal and fill in the blanks
(1010)2 = (________)10
(1101)2 = (________)10
(1111)2 = (________)10
(101101)2 = (________)10
(10000)2 = (________)10
(11111)2 = (________)10
(1000000)2 = (________)10
(111111)2 = (________)10
(10000000)2 = (________)10
(1111111)2 = (________)10
(11111111)2 = (________)10
(100000000)2 = (________)10
(10101011)2 = (________)10
(1000000000000000)2 = (________ )10
(111111111111111)2 = (________)10
(1111111111111111)2 = (________)10
By using this table we can convert any decimal number into binary number without division
method using the given steps.
Ex1: (5)10 + ( ? )2
Sol: 5 can be written as 4+1
(5)10 + ( 101 )2
41 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Ex: (150)10 = ( ? )2
Sol: 150 can be written 128+16+4+2
Put 1 for 128, 0 for 64, 0 for 32, 1 for 16, 0 for 8, 1 for 4, 1 for 2, 0 for 1.
So the binary is 10010110
Binary Addition:
Rule: 0+0 = 0
0+1 = 1
1+0 = 1
1+1 = 10
Addition of 1+1 is 2 but in binary number 2 is not allowed so in place of 2 we need to write the binary
of 2 as 10. In 10, 1 is the carry bit, and 0 is the addition
Ex: Perform the binary addition of given numbers
00001010
01010001
--------------------
01011011
Ex: Perform the binary addition of given numbers
(10) 0 0 0 0 1 0 1 0
(5) 0 0 0 0 0 1 0 1
--------------------
(15) 0 0 0 0 1 1 1 1