Sunday, June 3, 2018

Number Systems and Conversions


Chapter: Fundamentals
Lesson: Data Representation in Computers
Lecture: Number systems and conversions


Data Representation in Computers


qIn this section we are going to learn about how things stored in a computer. As we know computer can store things we call “Data” in storage devices such as hard drives and in memory.

qAs we know a computer can store so many different type of data such as images, videos, text documents, data bases, etc. But there is one fundamental thing we should understand is that a computer can understand on zeros and ones (0 and 1).

qSo we need to understand how a computer store these all types of data using only 1 and 0. To understand this we need to study about number systems.

qHere we are going to learn about decimal, binary, octal and hexadecimal number systems.

Number Systems

qA number system is a way of representing numerical values. It’s a standard and the most common number system is decimal and we all are using the decimal number system for our day to day works.  Other number systems are Binary, Octal and Hexadecimal.

qA number system has a BASE (also called RADIX) and there are symbols to represent a digit. The number of digits in a number system is equal to the value (size) of the base.

qOur human mind is trained to use decimal number system. Computer uses binary number system. We use octal and hexadecimal number systems to represent small values or large and complex numerical values such as a very large amount.

Number System
Abbreviation
Base
Symbols
Decimal
Dec
10
0,1,2,3,4,5,6,7,8,9
Binary
Bin
2
0,1
Octal
Oct
8
0,1,2,3,4,5,6,7
Hexadecimal
Hex
16
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F


qDecimal (Dec) base value is 10 and there are 10 digits from 0-9
qBinary (Bin) base value is 2 and there are only two digits 0 and 1
qOctal (Oct)  base value is 8 and there are only 8 digits 0-7
qHexadecimal (Hex) base value is 16 and there are 16 digits 0-9 and A-F


Binary Number System


The binary number system has only two digits 0 and 1. The base of the binary number system is 2. Like decimal number system, the value of the digit in a binary number is depend on the position within the number. Let’s consider following binary number and convert in to decimal to get it’s decimal value.

Example:
101101 is a binary number.
Now the base of these digits are 2. So, let’s multiply digit from its radix and decimal value of it.
1x25 + 0x24 + 1x23 + 1x22 + 0x21 + 1x20  = 32 + 0 + 8 + 4 + 0 + 1
= 45

Octal Number System

Octal number system (short term Oct) has eight digits (0 – 7) and the base is 8. Since Oct numbers can contain digits from zero to seven, number 98 is not an Oct number and 478 is an Oct number because the base of the number is indicated as 8. Now let’s get the decimal value of 478 we do the same method used above.


Example:
4x81 + 7x80 = 32 + 7
= 3910

According to the above example 478 in Oct equal to 3910 in Dec.
Oct number system mainly use in computing. We will discuss in a later chapter when and where Oct system is used and some applications of it.

Hexadecimal Number System

Called Hex for short, this number system has 16 digits and mainly used to present large numbers. This contains digits from 0 – 9 and A – F for additional numbers. E1016 is a hexadecimal number. We can get the decimal value of this number using the same method we used previously.

The decimal value of English letters presented in Hex number system;

A = 10, B = 11, C =12, D = 13, E = 14, F = 15
Altogether (0 – F) there are 16 digits (including zero).

Example:
E1016 = 14x162 + 1x161 + 0x160
= 3584 + 16 + 0
= 360010

Number System Conversions

Decimal to Binary, Oct and Hex

qDecimal numbers may have an integer part and a fractional part. In number 454.67 integer part is 456 and fractional part is .67
qTo convert decimal number in to any other number system, we need to take these integer part and fractional part separately.

Converting integer part
§Divide the quotient repeatedly by the target base while recording remainders  until the quotient become zero or the quotient is less than the base value.
§Write the remainders from bottom to top order.


Example: 367.2310 to Bin

Convert fractional part

Repeatedly multiply the fractional part from the target base value while recording the integer value while the fraction becomes zero or until the required number of fractional positions met.

Write the integer values from top to bottom.

367.2310 = 101101111.00112

Bin, Oct, Hex to Decimal
To convert any Bin, Oct or Hex number to Dec is easy.  All you have to do is multiply base value of the position of the digit with digit value and get the sum of all values.

Lets convert 11101101.11012 to Decimal
1x27 +1x26+1x25+0x24+1x23+1x22+0x21+1x20.1x2-1+1x2-2+0x2-3+1x2-4

Note that 1x2-1 = 1 / 21 =0.5 and same method apply to others.
128+64+32+0+8+4+0+1   .   0.5+0.25+0+0.0625
= 237.812510 

Octal to Binary, Binary to Octal

qEach octal digit can be represented by equal three binary digits
qTo convert octal to binary, each octal digit should be replaced by binary digits
qTo convert binary to octal, binary number should separate in to three digit groups starting from the decimal place to both directions. Then replace by equal octal digit.

Convert 1000101102 to Octal
100 010 110
= 4268

Convert 3228 to binary
= 0110100102


Hex to Binary, Binary to Hex

qEach hex digit can be represented by equal four binary digits

qTo convert hex to binary, each hex digit should be replaced by binary digits

qTo convert binary to hex , binary number should separate in to four digit groups starting from the decimal place to both directions. Then replace by equal hex digit.


Binary Addition


Binary addition follows 4 simple rules as stated in below table.


Conclusions

q Number systems, conversion and addition are the basics of computer technology. You need to have a good knowledge in this filed to understand different operations in CPU and programming.


q This is also a very easy part to learn and takes little time and practice.

q There may be questions from number system conversions and binary addition in the exam. These are the definite questions that you should answer and can gain full marks.

q It takes very little time to answer questions from this area and you can save time for more difficult problems in the exam.

You can download the full slide here 
https://www.slideshare.net/susanthaherath/number-systems-and-conversions

Thursday, February 9, 2017

Lecture 20 - System Software



This is the final lecture note for Software Development subject.

Compilers and Interpreters
A compiler is a program translator used for translating source program of high level languages in to machine executable form. That is the compiler is a program that takes in the C/C++ file(s) as an input and outputs an executable file that can then be directly run on the host computer.

Compilation Process
Compilation process consists of three stages;

Lexical Analysis
The source program of the programming language is scanned by the lexical analyzer to identify the commands, operators, identifiers, and tokens. After the identification it builds the syntax tree to detect any syntax or semantic errors.

Code Generation
During this stage for each and every rapid statement in the program machine code instructions are generated to prior the object program. In addition code optimization techniques are used to improve the efficiency of the program.

Linking
In the final stage separately compiled object codes in the external library functions are integrated to produce the final executable program.



If compilers are one extreme to running programming languages then pure interpreters are the other extreme. Pure interpreters do not do any code translation as done by compilers. These interpreters take the source code (which is written in a high language) and start executing the statements on the host machine one by one.

Comparison of compilers and interpreters

Compilers:
  • Translated the whole program at once to produce the object code
  • Can run the program independently 
  • Repeated executions do not need re-translation of the code
  • Syntax and sementic errors are detected before execution of the program
  • Execution speed is much higher
  • Run time memory requirement is less
  • Source program code is hidden from the user
 Interpreters
  • Translator execute the program statement by statement (line by line)
  • Program is run under the control of the interpretor
  • Every execution need to re-translate the program
  • Errors are detected at the run time only
  • Execution speed is much slower
  • More memory is required at the run time
  • Most of the time program source code can not be hidden

Some of the commercial programming languages have been known to be interpreted (Basic, Java) and yet these languages do not behave quiet like the description given above. The reason is simple - none -of the popular modern programming languages are pure-interpreter based. They are either compiled (like C/C++) or adopt a Hybrid approach (like Java, Basic).


Assembler
Assembly language is the closest thing you come to machine code It's a low level language which means that isn't as intelligent as high level languages like C or Pascal. It's harder to code in than those programs, but it runs much faster and you have total control of what you are doing. you should write programs all in assembly but use it as inline assembly in high level languages where you want it to run faster. Simplifies the task of writing machine code programs.


Important Notice!

Dear students and friends. When you commenting please do not mention your email address. Because your email address will be publicly available and visible to all. Soon, it will start sending tons of spams because email crawlers can extract your email from feed text.

To contact me directly regarding any inquiry you may send an email to info@bcslectures.website and I will reply accordingly.