85
1. Numbers a. Comparison b. Natural numbers c. Integers a. Number line b. –A + B can be written as +B-A d. Real numbers e. Powers: Square, Cube. a. Examples f. Roots (Difference from parts/fractions). Square, Cube and n th root. g. Complex Numbers 2. What is mathematics? It’s not just numbers but a mechanism to explain things using symbols. Symbols are used to express both the entities – to quantify them and to state their qualities, and to express relations between these entities. State examples, circles, apples and open mouth as spheres. Can an apple of a certain volume go into open mouth easily or will it have to be cut. 3. Number representation (integers and real numbers) a. Decimal b. Radix c. Binary (Bits and Bytes) d. Hexadecimal e. Octal f. Conversion a. Binary to Decimal and vice-versa b. Hexadecimal to binary and vice-versa g. Operations a. Binary Addition b. 1’s and 2’s complement c. Binary Subtraction d. Binary Multiplication e. Binary Division f. Truth Tables g. Logical OR, AND, NOT, XOR, NAND 4. Scientific Notation, mantissa, radix and exponent. 5. Fixed point and floating point numbers. 6. Precision. 7. Floating point number representation, single and double precision. 8. Element, Compound 9. Structure of atom 10. Molecules, Bonds, and Free Electrons, Voltage and Current. 11. Conductors, Insulators. 12. Doping and Semiconductors; why semiconductors. 13. Resistor, Capacitor, Diode, and Transistor. Page 1 of 85

Programming by zb

Embed Size (px)

DESCRIPTION

po4ier b y zb

Citation preview

Page 1: Programming by zb

1. Numbersa. Comparisonb. Natural numbersc. Integers

a.Number lineb.–A + B can be written as +B-A

d. Real numberse. Powers: Square, Cube.

a.Examplesf. Roots (Difference from parts/fractions). Square, Cube and nth root.g. Complex Numbers

2. What is mathematics? It’s not just numbers but a mechanism to explain things using symbols. Symbols are used to express both the entities – to quantify them and to state their qualities, and to express relations between these entities. State examples, circles, apples and open mouth as spheres. Can an apple of a certain volume go into open mouth easily or will it have to be cut.

3. Number representation (integers and real numbers)a. Decimalb. Radixc. Binary (Bits and Bytes)d. Hexadecimale. Octalf. Conversion

a. Binary to Decimal and vice-versab. Hexadecimal to binary and vice-versa

g. Operationsa. Binary Additionb. 1’s and 2’s complementc. Binary Subtractiond. Binary Multiplicatione. Binary Divisionf. Truth Tablesg. Logical OR, AND, NOT, XOR, NAND

4. Scientific Notation, mantissa, radix and exponent.5. Fixed point and floating point numbers.6. Precision.7. Floating point number representation, single and double precision.8. Element, Compound9. Structure of atom10. Molecules, Bonds, and Free Electrons, Voltage and Current.11. Conductors, Insulators.12. Doping and Semiconductors; why semiconductors.13. Resistor, Capacitor, Diode, and Transistor.14. How logic gates are constructed from transistors/diodes, and how adders are constructed from logic

gates.15. Integrated Circuits.16. Microprocessor (understands binary commands, a program is a list of commands).17. Microprocessor interfacing block diagram.

a. Busses: address, control, and data.

Page 1 of 66

Page 2: Programming by zb

b. Registers, RAM (Bit, Byte, Word), ROM, Secondary Memory, Keyboard, Mouse, Display, Network Card.

c. Micro controllers.d. Interrupt and clock.

18. Instructions (R2R, M2R), Program Counter, Stack Pointer, General Purpose Registers, Cache.19. Instructions fetch, decode (string matching), ALU, Processor Bit-ness related to RAM addresses.20. Multi-cores.21. Data: Integer, Float, Character (byte), Character sets (ASCII and UNICODE) signed and unsigned (Flags

register), word, double word, quad word, pointers. Instructions to specify data are not in instruction set!22. Custom data types as groupings of standard data types.23. Program as top down execution (logical flow) of instructions, Program Counter, entry-point and PC setup.24. Functions (Methods) as combinations of instructions.

a. Input parameters along with their type.b. Return value and its type.c. Function name is nothing but the location (address) of its starting code. It is a symbol.

25. Programs need data.26. Data positions: stack (Stack Pointer) and how it relates to functions, heap, global memory, static and not-

static memory (data). Exception frames on stack and vector exceptions.27. Where is program stored? Its format (PE/ELF [executable linking format] depending on starting data

stored – magic number), static data – initialized and uninitialized (bss – block started by symbol, which is to be 0 initialized.)

28. Batch processing of programs by processor, picking them from secondary memory (Loading a program).29. Why should the supervisor program not be stored in microprocessor?30. OS as supervisor. Interfacing between microprocessor and user program by providing predefined services

to disallow dangerous scenarios. Different Oss, Windows, UNIX, LINUX, Mac, Android, iOS and for different machines types PCs, cell phones, TVs etc.

31. BIOS and Booting.32. Multiprocessing by OS, swapping programs.33. Division of address space into User mode (User Program), and Protected mode (OS) – facility provided by

cpu to implement this.34. When to swap programs, timer-interrupt, interrupt vector (descriptor) table. Vector means indirection.

Where is IVT stored? IDTR register. 35. RAM is small, programs need large memory. Address space (virtual memory) and Physical (actual/RAM)

memory; use of either swap area or a page-file to get make RAM more that available. Division of address space into user mode and protected mode.

36. Multiprocessing by swapping in and out only portions of full/incomplete (library for example, to be explained later) program from RAM into/out of secondary memory. I/O takes time, why wait. Changing PC, SP and other register values (PCB). Pages, Page Tables, Page Directories. Page directory register. Keeping vital part of paging (page directory and table) always resident in RAM.

37. Multilevel (2 and 3-level) page tables. CR3 (Control Register 3).38. How a system call is made, system libraries etc.39. Threading (multiple logical flows of the program) and its need. Division of process so that different parts

are handled by different threads.40. Division of stack, changing SP, PC (TCB) on every threads change on timer interrupt or thread yielding

control. Now stack is limited. We can use either default size or ask of custom size at thread creation.41. Kernel mode threads and user mode threads (fibres/light-weight threads).42. IPC.43. Program constructs.

a. Algorithm.

Page 2 of 66

Page 3: Programming by zb

b. Statements.c. Functions and usage of stacks.d. Decisions based on conditions: If-Then, If-Then-Else.e. Loops: While, For; Breaking out of and continuing loop statements.f. Recursion.

44. Data structures: How custom data types are called data structures when they include functions that act on them.

45. Mutual exclusion of threads; semaphores etc.46. Various data structures

a. Arraysb. Linked-lists: Single, double, Circular.c. Stacks: Fixed stacks from arrays and dynamic stacks from linked-list.d. Queues: Fixed and dynamic queues.e. Trees: Node, Root, Parent, Children, Siblings, Leaf

a. Binaryb. Binary Search Treesc. N-ary trees.d. Traversals:

1. Breadth-first2. Depth-first: Pre-, In-, and Post-order traversals.

47. Sorting: Bubble and Big-O notation, Selection, Insertion, Shell, Merge, Heap, Quick, and radix sorts.48. Searching: Binary search, Binary tree searching, Balanced Trees.49. File system

a. Files, Directories as special files. b. Types of file systems: FAT, FAT32, NTFS, CDF, UDF, EXT. c. File and directory naming conventions.d. Adding extensions to files and purpose of extensions.e. Drivers and File system driver. Other possible drivers in an OS.

50. Raw overview of executable file (that can be run) as containing data regions, and executable code. When about to run, implicit addition of heap and if size is not explicitly specified addition of thread stack.

51. Division of program into libraries and main part.a. Purpose of libraries: sharing code and possibly data, load only when code needed. Also code could

be replaced if there is an error (bug) in it.b. Difference between executable files that can be run and libraries.c. By default, each process gets own copy of data.d. Raw overview of library (as executable) as containing data regions and executable.

52. Overview of Language: keywords, syntax, grammar, and Compiler.53. How libraries can be built:

a. Static libraries and linking (address fixing). Big program. Sharing of code only at compile time.b. Dynamic Libraries: Small program, and sharing of code when the executable is run (run-time).

a.What do we mean by them and loading.b.How to write them. Writing position independent code or fixing their positions in address

space.54. Securing objects (kernel, and use objects):

a. User identification and group identification.b. Process token.c. Access Control List and Access Control Entries. Traditional *nix security.d. Denying or allowing object access.

55. Initial C++98 programming.

Page 3 of 66

Page 4: Programming by zb

a. Console programming and Windows (event-driven) programming differences.b. Program structures: headers, main files, standard library, main function as entry point.c. Classes and structures.d. Objects.e. Access specifiers. f. Inheritance: Single and Multiple.g. Function overloading.h. Function overriding – virtual functions.i. Streams: Input and Output streams and cin and cout objects. File streams.j. Templates.k. Standard string library.l. Implementation of List, Queue, Stack, Binary search tree.m. Implementation of sorting algorithms.n. Implementation of binary search and binary-search trees.

56. Multithreading conflicts and data structures.57. .NET overview: why and how.58. C# Programming.59. DBMS.

a. Sets, Functions, Relations.b. ACID.c. Relation as Tables.d. Primary and Foreign keys.e. Relational Laws.f. Relational Operators.g. SQL

60. C# ADO.NET Programming.61. Networking concepts.62. Software Engineering63. Project.

Page 4 of 66

Page 5: Programming by zb

1. Numbers

1.1. Comparison as CountingBefore man learnt counting he (or she. From now I will stick to he, but it should be read as he/she) still was able to sell his sheep. How could Joseph ask Shmichael that he wants to buy some of Joseph’s sheep for two sacks of wheat that he would give in return?

And how would Joseph decide how much sheep he should sell for those two sacks of wheat? Remember, they don’t know how to count! Schmichael would simply ask for sheep for ‘this much’ of sacks, and Joseph would know that he has to use ‘this much’ of sack to sell ‘some’ of his sheep.

Joseph could have decided to sell “a” sheep per sack. Now this is counting too, isn’t it? Yes it is, but he won’t use counting; instead, he would use comparison. He would simply place each sack near a sheep and he would know how many sheep he would need to sell. In our case, since we know how to count, it will be two; but for him it would still be this much.

1.2 Natural NumbersMan had finally learnt how to count. He knew how much one was, how many two were, how many three were, and so on. He also learnt to use symbols, that we now call letters, for these numbers. Most used letters come from the Indo-Arabic numeral system, 1, 2, 3, 4, and so on. These are called natural numbers because counting of one, two, three, four, and so on is intuitive and should come naturally to a man.

Man learnt that if he possesses two apples and is given three more – the mathematical operation on numbers that we call addition – he would now possess five apples. That is, 2 + 3 = 5. ‘+’ is the symbol that we use

Page 5 of 66

Page 6: Programming by zb

to denote addition of two numbers and ‘=’ is the symbol to denote ‘will result in’ or ‘equals.’ And the numbers on which the operation is performed are called operands.

1.3 IntegersWhat if Raj possesses three apples and all three would be taken away (in mathematical terms the operation of subtraction denoted by symbol ‘-’) from him; how much would he have now? The answer would be ‘nothing’, or ‘none.’ How can we represent none? We define this with a number ‘zero’, written as 0 in Indo-Arabic numeral system. 3 – 3 = 0.

Some of us may be intuitive enough to think that zero should be considered as a natural number; and indeed yes, some people do consider it as natural number. We will not.

What if Raj owed 4 apples to Sita and had only 3 in his possession. Sita then took away all apples Raj had, how many apples would Raj owe to Sita. You could write 4(owed) – 3(taken) = 1 apple owed. But what if, Raj had five apples in his possession and Sita took away all five! How much would Raj owe? Obviously, Raj doesn’t own anything to Sita. In fact, he is one apple less and it is Sita who would owe an apple to Raj. How would we write this? We write 4(owed) - 5(taken) = -1. That is we give a negative sign (denoted by the same symbol we use to denote subtraction) to denote opposite of what we are concerned in the problem. We were concerned with owing of apple. So opposite of owing an apple would be ‘deficiency’ of an apple. Thus, 1, 2, 3, and so on are actually +1, +2, +3, and so on, denoting the positive natural numbers. Whereas, -1, -2, -3, read as negative (or minus) 1, negative 2, negative 3, and so on give opposite quantities. Where the symbols + and – to the left of these numbers are often called as their signs. But how did we calculate +4 – +5 = -1? We will answer it shortly.

What is negative of zero? We write negative of nothing is nothing. Thus, 0, +0, and -0 are the same.

Natural numbers, their negatives, and zero together form Integers.

Back to calculating +4 – +5! We know that +4 can be written as 4 and +5 can be written as 5, we can write +4 - +5 as 4 – 5. The trick is to know that a negative sign only denotes opposite, and opposite of opposite is the initial number itself. Thus, 4 – 5 is “OppositeOf ‘OppositeOf 4 – 5’”; which is ‘opposite of 5 – 4.’ What is ‘opposite of 5 – 4?’ ‘Opposite of 5 – 4’ is opposite of 1; and we know opposite of 1 is -1. Using less English and more symbols, and using parentheses ‘(‘ and ‘)’ to club an operation, we write:

4 – 5 = - ( -(4 – 5) ) = - (5 – 4) = - (1) = -1

We always operate inside the inner-most parentheses (indicated above in a bold-face) first. Thus, 5 taken away from 4, is opposite of 4 taken away from 5. And, opposite of 1 is -1.

(1) is simply 1 because we had used parentheses to club an operation and 1 is not an operation but a number, so we ignore the final parentheses.

We could draw integers in a form called a number line and then intuitively perform operations of them.

A number on a number line is always smaller in value than the numbers to its right . Thus, negative 1 is less than 0, i.e., -1 < 0. Whereas, positive 1 is greater than 0, i.e., 1 > 0. Also note that, even though positive 3 is

Page 6 of 66

Page 7: Programming by zb

greater than 2, negative 3 is less than 2. That is, 3 > 2, but -3 < -2. Also the space between adjacent numbers is considered a value of 1.

What is – 4 – 5? It is -4 - +5. That is, we owe 4 apples and someone wants to take away 5 more apples. Thus, we actually owe them additional five apples, since we have none to give right now. In total we owe them a total of 9 apples. Thus answer is negative nine, i.e. -9.

Also if A is a positive number then –A is a negative number. Also ‘A – B’ can be written as ‘–B + A’, ‘-B + A’ can be written as ‘A – B’, ‘A + B’ can be written as ‘B + A’, ‘B + A’ can be written as ‘A + B’, ‘-A – B’ can be written as ‘–B – A’, and ‘-B – A’ can be written as ‘-A – B.’ Basically, we can switch positions of A and B for operation of addition and subtraction.

What is 2 + 0? Well, we have two apples and we are given ‘nothing’ or ‘none’ more apples. We obviously have the same number of apples we already had, i.e., 2. Thus, the answer is 2.

What is 2 – 0? In this case, we again have two apples, and ‘none’ of the apples are taken away from us, resulting in the same number of apples as were, i.e., 2. The answer thus is 2.

So, if A is an integer then A + 0 = A, and A – 0 = A.

We have seen how opposites behave with positive numbers in the operation of addition; they tend to cancel each other. For example, 5 – 4 is 1, i.e., four values are cancelled from 5 to result in a single value (i.e., value 1). But how do they behave in the operation of multiplication?

The operation of multiplication is denoted by symbol ‘x’ or by symbol ‘’. Multiplication can be broken down into an operation of addition. How?

Let us take an example of 3 x 2.

3 x 2 can be written as 3 added to self. Thus, 3 x 2 = 3 + 3 = 6. Note that we have written 3 twice in the previous addition operation because the right-hand operand in the multiplication was 2. If we take another example of, 2 x 5, we would have to write 2 five-times in the addition. Thus, 2 x 5 = 2 + 2 + 2 + 2 + 2 = 10.

But what is 3 x -2? Now this is a pickle. Let us take the analogy of having apples again. We have three apples, and someone wants us to do the opposite (because the sign on 2 is negative), i.e., borrow them; also that someone wants us to do this twice (because the right hand operand is negative 2). Thus, borrowing three apples twice would result in total borrowing of 6 apples. Thus the result will be -6.

The above problem could have been solved in an easier way. The way we can switch places of operands during addition and subtraction, we can with multiplication too. Thus, 3 x -2 can be written as -2 x 3. Now, analogy of having apples becomes simpler. We had borrowed 2 apples because we have negative 2, and someone wants us to do this 3 times. This means borrowing 2 apples three times, which results in borrowing of six apples. Thus, the result is negative six, i.e., -6.

It is important to note that if A is an integer then A x -1 = -A. That is, if we have some apples, and someone wants us to do opposite of this once (i.e., -1), we will have borrowed those number of apples.

For example, 3 x -1 = -3. That is, we just change the signs. So, we can go reverse to, i.e., -A = A x -1.

Also, if –A is an integer then –A x -1 = +A, or simply A. We just changed the sign of –A. That is, if we had borrowed some apples, we do opposite of this once (i.e., -1); which results in us having those number of apples.

Page 7 of 66

Page 8: Programming by zb

How much is -3 x -2?! There is no -1 for our help here; how do we do this. We can use the same reverse going trick as above to solve this. -2 can be written as 2 x -1. Thus,

-3 x -2 = -3 x 2 x -1 = -6 x -1 = +6 = 6.

It is important to note from above that multiplication of two oppositely signed numbers (i.e., one positive and other negative) results in a negative number. Whereas, multiplication of two same signed numbers (i.e., either both of them positive or both of them negative) results in a positively signed number.

What is 3 x 0? If we have 3 apples, someone wants to have then zero times, we will have zero apples. We can also reverse 3 x 0 to 0 x 3 for it to be intuitive. Now, we have zero apples or nothing, and someone wants to do this thrice; that is, having nothing thrice. This results in nothing. Thus, the answer is 0.

If A is an integer (whether positive or negative), then A x 0 = 0.

If A is an integer (whether positive or negative), the A x 1 = A.

1.4 Rational NumbersAll was well, us having apples and all. But one fine day a gentleman arrived at our barn with a knife and an intelligent brain of his. He saw us having an apple. Says he, ‘I will make four apples from your one and you shall you give me one of those?’ We were ready, yay! After all, four apples were better than one; and giving one of those to him wouldn’t have been such a big deal. The deal was struck.

He took out his knife, cut apple in four, counted one, two, three, and four apples. He then ate one of the portions and vanished. We were still hungry after having three apples! Wait, what just happened? We counted, didn’t we?

The problem was our representation of numbers wasn’t yet complete. We forgot to accommodate parts or portions of a number. A portion in mathematics is called a fraction.

The operation of cutting of an apple must have a name; we write it as division, because we divide apple in parts. One apple divided into 4 parts is written as ¼, and read as ‘1 divide into 4’ or simple ‘1 by 4’.

So, a number of the form A/B is called a rational number; where, A and B can be either positive or negative integers, and they may or may not have the same sign. But B cannot be 0; because we can’t divide an apple into zero parts.

A/1 means dividing A (here, say an apple) into 1 part, resulting in the same A (apple). Hence, A/1 = A.

If A is an integer (whether positive or negative), 1/A is called reciprocal of A.

In A/B, A is called the numerator and B is called the divisor.

Also, -A/-B = A/B. That is, if both the numerator and divisor are negative, the signs get cancelled.

But how do we calculate the division? As multiplication can be obtained by addition, division can be obtained by subtraction but the process is a bit longer.

Let us assume that numerator is greater than the divisor. Then we subtract divisor from numerator and note down the result. If the result is less than the divisor we finish, if not we then assume the result to be our numerator now and keep on subtracting. We keep on doing this until the result of subtraction is less that the divisor.

Page 8 of 66

Page 9: Programming by zb

We note down how many times we had performed subtraction during this entire operation and call this noted value as the quotient. We also note down the final result and call this value as remainder. If the remainder is 0 we stop.

Let us take an example of 10/2, and note down how many times we will perform subtraction.

10 – 2 = 8; 8 – 2 = 6; 6 – 2 = 4; 4 – 2 = 2; 2 – 2 = 0; remainder 0 < 2, so we stop. Since remainder is zero we don’t need to perform operation on remainder x 10. So, our answer is our quotient, which is the number of times we have performed subtraction. The answer is thus 5. So, 10/2 = 5.

When the remainder is not 0, what we get is an integer plus some fraction of 1.

Above process to calculate the result of division when the remainder is not 0, or when the numerator is less than the denominator is a bit longer and is called ‘long-division.’ You must have leant long division in your school. It involves remembering multiplication tables of numbers.

If either numerator or denominator is negative, but not both, then we can ignore the negative sign and simple put it in front of our result. If both of them are negative in sign, then the final answer is positive and their signs can be ignored while performing division.

1.5 IndicesCan we make 2 x 2 x 2 x 2 x 2 x 2 a little terse (i.e., shorthand)? Or do we have to write the same long operation again and again, whenever it occurs in our calculation? We can; we simply invent an operation for our convenience. We can call this operation as an exponentiation of a number. The operator of this can be called raised-to, to-the-power, or with-exponent operator etc.

So, we can invent 2 x 2 x 2 x 2 x 2 x 2 to be written as 2 raised-to 6. Why, because 2 occurs in our problem 6 times.

Thus, 2 raised-to 6 = 2 x 2 x 2 x 2 x 2 x 2 = 64. Yay! But, we are still using the term ‘raised-to’; can we give it a symbol? Yes we can, but we will be a bit intelligent than that. We will write 2 raised-to 6 as 2 6, that is we write 6 a little to the right of 2 but on its head. 6 here is called an index or power or exponent, whereas, 2 is called the base of exponentiation.

Similarly, 33 = 3 x 3 x 3 = 27.

Note that this is different from 3 x 3, which is 3 x 3 = 9.

So, if A and B are integers, and B is a positive integer greater than 0, then:

If 23 = 2 x 2 x 2, what is 20? This is totally unintuitive. How many times should we write 2? Zero? Should the result be 0? We (as others have), for our convenience, will take it to be 1 and not 0. Thus, any number with an exponent of 0 will result in 1.

A0 = 1.

What if the exponent is a negative integer, i.e., what about 2 -3? What should be the result? This is unintuitive too. But we will, for our invention, consider it to be something for our convenience. And we shall assume:

Page 9 of 66

Page 10: Programming by zb

2-3 be equal to 1/23. That is, we take the reciprocal of the problem and change the exponent sign to positive. Intuitively, 23 = ½-3.

Thus,

A-B = 1/AB, and

AB = 1/A-B.

We can write 2 x 2 x 2 x 2 x 2 x 2 x 2 , i.e., 27 as,

2 x 2 x 2 x 2 x 2 x 2 x 2 = 23 x 24

In Reverse, 23 x 24 = 27. So, the final exponent is addition of two individual exponents.

Thus, ‘with the same base B’ as an integer (either positive or negative), and ‘m’ and ‘n’ being integers (again, either positive or negative, and not necessarily both having the same sign) as indices:

Bm X Bn = B(m+n)

Similarly, What should be 24/22? Let us evaluate.

24/22 =

=

= 2 x 2 = 22

Thus 24/22 = 24-2 = 22. In general,

Bm/Bn = B(m-n)

There is a special term for power of 2 of a number (base), it is called a square of that number. Similarly, the power of 3 of a number is called cube of that number.

Suppose we have two predetermined numbers A and m, then in

xm = A.

The values x, i.e. a base, can take to result in answer A for certain index m, are called roots of A for this equation. And we write the root x as

X = A(1/m).

That is,

if xm = A, then x = A(1/m)

If m is 2, then the roots are called square-roots of A, and if m is 3 then the roots are called cube-roots of. Other values of m have no special names but we call them m-roots of A. That is, if m is 5, we call the roots as 5-roots of A.

For example, what should be the value of x in the following?

x2 = 9

Page 10 of 66

Page 11: Programming by zb

Obviously x can be 3 as 32 = 3 x 3 is 9. But what you may not have considered is that x can also be -3, because (-3)2 = -3 x -3 is also 9. Thus, square roots of 9 are 3 and -3. We write square root of 9 as either 9 1/2 or by using a special symbol to write 9.

What are square roots of 1? They are +1 and -1 because +1 x +1 or -1 x -1, both result in 1.

The trick to calculate a square root is to take the term which occurs twice inside the square root, outside of it. For example 9 = (3 x 3 x 1) or (-3 x -3 x 1) = 31 or -31 = 3(1x1) or 3(-1 x -1) or -3(1 x 1) or -3(-1 x -1) = 3 or -3 or -3 or 3 = 3 or -3.

What is the cube root of 8? That is what value of x satisfy the following

x3 = 8, or x = 81/3 or x = ∛8

The answer is 2, because 2 x 2 x 2 = 8. There is no other number that satisfies the above equation. Thus cube root of 8 is 2; in mathematical terms

∛8 = 2.

We have already devised nth-root operator to find a root, which is (base)1/n; and we write for square-root operator, i.e, where n =2, ∛ for cube-root, where n =3, and ∜ for fourth-root where n = 4, and so on. We will now devise two more operators for our convenience.

The first operator will give us the index, given the final result and the base. We call this operator as logarithm or in short log operator.

Let base be b, index be i, and final result be R. Then we know from our previous relationship that

bi = R.

Then, we define ‘log for base b’, as

logb R = i.

We read it as, log-to-base-b of R is equal to i.

The meaning of this is, that the value of base b has to be raised to power of i, to get value of R. Thus,

log10100 = 2, because base 10 has to be raised by a power of 2 to get 100. That is, 102 = 100.

Similarly, log28 = 3, because base 2 has to be raised to the power of 3 to get value 8. That is, 23 = 8.

When the base is 10, we don’t need to write 10 at the feet of log operator. When the base is 2, we usually write log2 operator as ‘lg’.

The second operator is the reverse of log operator, and intuitively we call it anti-logarithm or antilog operator.

The funny thing to note about antilog is that we have already defined it. It is the Result when we raise base to a power. That is antilogbi = R. Hence,

logb R = i, and antilogb i = R.

Page 11 of 66

Page 12: Programming by zb

Our derivation of these operators is not in vain, because these operator comes to our help in various mathematical problems.

1.6 Irrational and Complex Numbers.Not all numbers that are written in the form of A/B can be calculated. For example we can’t divide 1 by 3 precisely. That is we will not be able to get to final value. What we will get is 0.3333333. There is a pattern of non-terminating 3s. But as we know it can be written as A/B, namely 1/3. But there are numbers that keep on repeating in fractional part without a pattern. These numbers are called irrational and can’t be expressed in the form A/B. Pi is an example of irrational number. Pi is Circumference/Diameter of a circle. Another example is 2.

What would be square roots of a negative number, say -4? That is, what should be -4

Suppose x be one of the square roots of -4, then

x = -4, or

x2 = -4.

Can we find any such rational or irrational number that multiplying it by itself produces a negative four? The answer is no. In fact, we can’t find any rational or irrational number which multiplying by self gives a negative number. Why? Consider that our answer is a positive number. In such a case, multiplying by itself, i.e., positive to positive results in a positive answer. Which is not what we want because we want a negative answer, so we can’t have a positive value as our root. What if our root were a negative number? In such a case, multiplying it by self would again result in a positive number. Again, we need a negative number as a result. Thus, our root can’t be a negative number too.

If the square root of a negative integer can’t be positive or negative, that means it is not any form of rational or irrational number. What is it? We devise another convenient notation for such numbers. Here is how.

-4 = (2 x 2 x -1) = 2 -1.

We denote -1 as ‘i’, denoting that i is an imaginary number because it doesn’t fit into our description of rational and irrational numbers. That is, it doesn’t fit into the description of numbers which are real to us, or are real numbers for us. A number which is not a real number is called a complex number and has some imaginary part associated with it; imaginary because we have to imagine it to express it as some form of number.

We say that a

complex number = number-with ( a real number part + an imaginary part)

If real number part is 0, then the complex number has only the imaginary part. Since, we know from above that imaginary part is some real number (say, n) multiplied by ‘i’ (i.e., -1), and if we consider ‘r’ to real number part above, then

complex number C = r + n -1 = r + n x i.

The convenience of formulation of complex numbers helps us to further our solutions rather than to get stuck in the middle solving the problem.

Page 12 of 66

Page 13: Programming by zb

2. What is Mathematics?

Mathematics is not just study of numbers. It is more of a ‘precise’ language to define everything that we see or feel in nature, and to relate them, using symbols. For example, how can we define a circle in mathematics?

First and foremost we must ponder on the basic characteristics of a circle. We know it is perfectly round. But this doesn’t solve our problem. We must ask ourselves what does perfectly round mean. Here is what it means: suppose we have a nail in the ground to which we have tied a string; and on the loose end of the string we tie a pencil. If we hold the pencil in such a way that the string is tight, and try to move from the position where we are to either up or down and keep moving, we will arrive at our initial position. But during this process we would have drawn a perfectly round shape on the ground, which we call a circle.

So, for a circle every point on the drawn shape is equidistant from the nail, which we call the centre of the circle. Now we are reaching somewhere with our definition. A circle is such a shape such that every point on it (its periphery, called the circumference) is equidistant from its centre.

How do we write this is in mathematics?

The above figure shows a circle with centre o and the point P drawn on its circumference. Every such point P on the circumference will be equidistant from the centre with distance r. This distance r is called the radius of the circle. Point P is denoted by (x,y), because it is at a distance x to the right of the centre and a distance y up from the centre. Every such point on the circumference can be written as (x,y), with x and y taking values of how far are they from the centre o.

Now, whenever we will talk of some circle we will interpret it with its radius, known at a priori. Are we ready to define the circle mathematically? Not yet, but we know that we should define it in terms of r, x, and y.

Looking at the figure above we note that r, x, and y take part in the formation of a right-triangle, with x as the base, y as the perpendicular side and r as its hypotenuse. We can use the Pythagoras theorem to find that r=(x2 + y2), but this will be a direct jump without understanding how we have arrived at the Pythagoras theorem, which states that

r2 = x2 + y2

That is, square of the hypotenuse, is the sum of squares of its other two sides. In fact, the circle that we want to define is nothing much, rearranging the above equation as:

x2 + y2 = r2

But we still have to derive how we have come to this conclusion. The following figure shows a rectangle (the left shape), a box shaped figure that we are quite accustomed to in our daily life.

Page 13 of 66

Page 14: Programming by zb

What should be the size of the area of the shaded region of this rectangle? If we consider that the width of this rectangle is x as shown, then if we keep on placing this length (the width) on another such length, continuing it y times, we will get the desired area. Thus the desired area is y times x, or X x Y, or xy. Area of a rectangle is product of its two sides.

We can draw a right triangle as half the area of this rectangle. Thus, the area of a right triangle should be xy/2.

How much should be (a+b)2? It is (a+b) x (a+b); which is a x (a +b) + b x (a + b); which is a 2 + ab + ba + b2; which is a2 + 2ab + b2.

We are now ready to prove the Pythagoras theorem. Consider the following figure.

The outer figure is a special type of rectangle. It is a square, that is, a rectangle with all its side equal. In the figure all of its side are of length (a+b). Intuitively, area of the outer figure is the area of the shaded region plus the area of the hollow region. Shaded regions are right-triangles, and the hollow region is a square with all sides of length c.

Area of outer figure is (a+b) times (a+b), i.e. (a+b)(a+b). Which we have shown above is equal to a2 + 2ab + b2.

Area of the hollow region is c times c, i.e. c2.

Area of the shaded rectangles are ab/2, ab/2, ab/2, and ab/2.

Thus, intuitively,

a2 + 2ab + b2 = (ab/2) + (ab/2) + (ab/2) + (ab/2) + c2, or

a2 + 2ab + b2 = 4 x (ab/2) + c2, or

a2 + 2ab + b2 = 2 x ab + c2, or

a2 + 2ab + b2 = 2ab + c2.

Since the terms on either side of the equality mean they are equal in value, if we subtract some definite value from each of these terms, the final values of these terms will again be same. That is, if 4 = 4, and if we subtract 1 from either 4, what we get will be 4-1 = 4-1, i.e., 3 = 3, which is true.

Page 14 of 66

Page 15: Programming by zb

Thus, if we subtract 2ab from either side, the equality will still remain. Which means,

a2 + 2ab + b2 – 2ab = 2ab + c2 – 2ab, or

a2 + b2 = c2, or c2 = a2 + b2.

Since, a, b and c are just symbols, we can rewrite them as x, y and r respectively. Thus, r2 = x2 + y2.

We have proved the Pythagoras theorem, and in turn, defined the circle mathematically. Thus, a circle is defined as all such points from the centre where the following equation holds.

x2 + y2 = r2

Page 15 of 66

Page 16: Programming by zb

3.0 Number Representation

Until now we have used symbols 0 to 9, to represent our numbers. But we haven’t gone past the number 9 (if we ignore the figure representing the number line). What comes after 9? What symbol should we use to represent the next number, which we call ten? We could either choose a new symbol for it and for other numbers that come after 10 or represent them using a patter formed from our already chose symbols, i.e., 0 to 9. If we use the former method we would run out of symbols pretty soon, isn’t it?

What we do know is something intelligent. To represent the next set of numbers we place one of the symbols to the left of it, say 0. Then we have 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 00, 01, 02, 03, 04, 05, 06, 07, 08, and 09. Is 0 different from 00? It seems unintuitive, so we say appending 0 to left of a number is meaningless because it denote the same number. Now instead of 0, let’s try appending 1 to the left of our numbers; which gives us 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19. Yes, now we are reaching somewhere because 0 is different (and looks different) from 10. What should come after 19? This time instead of appending 1 to our predefined symbols, i.e., 0 to 9, we append 2. So we get 20, 21, 22, 23, 24, 25, 26, 27, 28, and so on. This process could be repeated over and over by choosing next symbol from the list, which would be 3, then 4, then 5 and so on. So, we would get

00, 01, 02, 03, 04, 05, 06, 07, 08, 09

10, 11, 12, 13, 14, 15, 16, 17, 18, 19

20, 21, 22, 23, 24, 25, 26, 27, 28, 29

30, 31, 32, 33, 34, 35, 36, 37, 38, 39, and so on up to ...

90, 91, 92, 93, 94, 95, 96, 97, 98, 99.

What should come after 99? We have already appended 1 to 9 to our chosen list of symbols! Well we repeat the same process but we now say that the symbols that we have are from 0 to 99! Yes, we again append 1 to first 00, then 01, then 02, up to 99; after which, we append 2 to 00, then to 01, then to 03, up to 99 and keep this process of appending continue. When we are exhausted with the symbols we increase our symbol set saying that we now have symbols 001, 002, 003, ..., 009, ..., 100, 101, ..., 201, ..., 900, 901, ...998, 999, and continue the process of appending 1, then 2, then 3, and so on to these numbers.

The above process can continue forever producing even greater numbers. We could do the same with the fraction part but this time the 0 to the left of the numbers in the fraction would hold meaning, but the zeros to the extreme right will be meaningless and could be ignored. This process of construction gives us a number system which we use to represent numbers.

In elementary school we have learnt 5/2 is 2.5, where the symbol ‘.’ Is called a decimal point and the number to the right is called the fraction part of the number. But how do we know that 2.5 is 5/2, i.e., how do we reach to 5/2 from this representation?

First, let me tell you that what we are concerned here is a decimal number system (decimal for short), not because of a decimal-point, but because we have used ten symbols (if we exclude ‘.’ symbol) to represent our numbers and ‘deci’ comes from a Latin word decimus meaning tenth.

If we look carefully the value of a pattern of symbols, e.g., 99, 346, 239.076 etc, can be given by

... + S5 x 105 + S4 x 104 + S3 x 103 + S2 x 102 + S1 x 101 + S0 x 100 + S-1 x 10-1 + S-2 x 10-2 + S-3 x 10-3 + ...

Page 16 of 66

Page 17: Programming by zb

Where S5 is the sixth symbol to the right from the decimal point, or if we start our counting from 0 instead of 1, the fifth symbol to the right of the decimal point. Similarly S -1, which is less than S0 the first symbol to the left of the decimal point, is the first symbol to the right of the decimal point. We see a lot of powers of 10 because our numbers use only ten, i.e., 0 to 9, symbols. The number of basic symbols (excluding a decimal point) that we use for a number system is called a radix of that number system, and the decimal point is actually called the radix-point. So, radix point of a decimal number system is commonly called a decimal-point.

Now we are ready to calculate the value of 2.5; here, S0 = 2, and S-1 = 5. Thus, we have

2 x 100 + 5 x 10-1 = 2 x 1 + 5 /10 = 2 + 5/10 = 2 + ½. Thus, 2.5 is actually 2 and a half.

Similarly, the value of is pattern 239.076 is

2 x 102 + 3 x 101 + 9 x 100 + 0 x 10-1 + 7 x 10-2 + 6 x 10-3

Can we think of a number system where the symbols are not 0 to 9? Yes, we can. The number system that uses only symbols 0 and 1 for all its number representations is called a binary number system (bi- stands for two), the number system that uses only eight symbols 0 to 7 is call octal (from Latin word octo meaning eight) number system, and the number system that uses sixteen symbols, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F is called the hexadecimal (hex- is 6 and decimal is 10) number system.

In other words, the radix in binary number system (or binary for short) is 2, in octal number system (octal for short) is 8, and in hexadecimal number system (hexadecimal) for short is 16.

The value of binary representation 110.1101 is thus

1 x 22 + 1 x 21 + 0 x 20 + 1 x 2-1 + 1 x 2-2 + 0 x 2-3 + 1 x 2-4

= 4 + 2 + ½ + ¼ + 1/16 = 6 + (4+2)/8 + 1/16 = 6 + 6/8 + 1/16 = 6 + (96 + 8)/128 = 6 + 104/128 = 6 + 13/16

= (96 + 13)/16 = 109/16 = 6.8125

Is 110.1101 a binary or a decimal number? Well it could be both because we can represent it in both the number systems. To distinguish a number that can be represented in more than one number system we denote its radix along with it using a subscript. Thus 110.11012 is a binary number and 110.110110 is a decimal.

If you would have looked closely, we always denote the value of a number in any number system as a value in our usual decimal number system. Thus, 110.11012 is a decimal value 6.8125.

How should we denote the value of a decimal number in our usual decimal number system? Well, it’s the same value, isn’t it?!

3.1. Binary Number system

In a binary number system, each symbol position is called a bit. Thus, a bit can be either 0 or 1. In our example 110.1101 (we will ignore the subscript 2 because we are only dealing with binary numbers here) we see there are seven positions, three to the left of radix and four to the right of it. Thus, there are a total of seven positions here, which means our number has 7 bits. A total of 8 bits is called a byte in binary number system. Thus, 11011011 is one byte long number.

Page 17 of 66

Page 18: Programming by zb

But how can we convert a number value, i.e., a decimal value into it binary representation? To represent a decimal number (value) into its binary equivalent we have to consider the fractional part and the integer part of the number separately. Let’s use 386.8125 to convert to binary.

3.1.1. Converting the Integer part into binary

The integer part of our decimal number 386.8125 is 386. To convert it to binary we divide the number by 2, note the quotient and the remainder. Now we consider the quotient as our integer and repeat the process. We continue the process until we get a quotient less than 2.

Our binary number is then the list of remainders we noted down, writing them from right to left, and then adding the last quotient to the extreme left.

386/2 = quotient 193, remainder 0 (right-most bit)

193/2 = quotient 96, remainder 1

96/2 = quotient 48, remainder 0

48/2 = quotient 24, remainder 0

24/2 = quotient 12, remainder 0

12/2 = quotient 6, remainder 0

6/2 = quotient 3, remainder 0

3/2 = quotient 1, remainder 1 (left-most bit after quotient)

Quotient is now less than 2; we stop.

The decimal 386 is represented in binary as:

Last-quotient last-remainder remainder-previous-to-it remainder-previous-to-it .... That is,

1100000102

If we want to be sure we can check this out; the radix for binary is 2 so the value of above number in decimal is:

1 x 28 + 1 x 27 + 0 x 26 + 0 x 25 + 0 x 24 + 0 x 23 + 0 x 22 + 1 x 21 + 0 x 20 = 256 + 128 + 0 + 0 + 0 + 0 + 0 + 2 + 0

= 38610

Note that, decimal 0 in binary is 0, and decimal 1 in binary is 1. That is

010 = 02, and

110 = 12

3.1.2 Converting the fraction part into binary

The fraction part of our decimal number 386.8125 is 0.8125. To convert this fraction into binary we multiply it by to and note the integer part of the result as well as the fraction part. The resulting fraction part now

Page 18 of 66

Page 19: Programming by zb

becomes our number and we continue the process until we reach a situation where the resulting fraction part is 0.

The resulting binary representation of the fraction is then the list of noted integer parts, writing them down first to lest from left to right. Thus,

0.8125 x 2 = 1.625 (left-most bit)

0.625 x 2 = 1.25

0.25 x 2 = 0.5

0.5 x 2 = 1.0 (right-most bit)

1.0 has 0 as the fraction part, so we stop. Thus, 0.8125 in binary is 11012. Let’s check if our answer is correct.

Again, for binary the radix is 2, and since we are dealing with fractions we will start the index (power) from -1.

1 x 2-1 + 1 x 2-2 + 0 x 2-3 + 1 x 2-4 = 0.5 + 0.25 + 0 + 0.0625 = 0.81252

Thus 386.8125 in binary is 110000010.11012.

3.2 Hexadecimal Number system

As explained earlier hexadecimal number system uses digits 0 to 9 and letter A to F, for its 16 symbols. The use of hexadecimal numbers is to make remembering large binary representation easy, as we shall see.

The decimal value of each symbol is 010 = 016, 110 = 116, 210 = 216, 310 = 316, 410 = 416, 510 = 516, 610 = 616, 710 = 716, 810

= 816, 910 = 916, 1010 = A16, 1110 = B16, 1210 = C16, 1310 = D16, 1410 = E16, and 1510 = F16.

3.2.1 Binary to hexadecimal numbers

Converting binary numbers to hexadecimal numbers is very easy. Starting from the radix point we bunch the bits of integer part of our binary numbers in groups of four bits, adding leading zeros if the last bits are less than four in numbers. Similarly we bunch the fraction part bits into group of four bits, and add leading zeros (this time to the right of bits) if the last bits in the group are less than 4 in numbers. For example, with 1101001.111012 we make groups of four bits on either side of radix points, adding leading zeros where necessary. This results in,

0110 1001 . 1110 1000 in binary

where the zeros in boldface are added so that the grouping of four bits is complete. Now we calculate decimal value of each group; this would result in

6 9 . 14 8 in decimal

We now convert the decimal symbols into their hexadecimal counterparts. Thus, we get

6 9 . E 8 in hexadecimal

We typically group the two decimal digits together because two 4bits give us one byte. Thus, our result isPage 19 of 66

Page 20: Programming by zb

69 . E816.

3.2.2 Converting from hexadecimal numbers

The process is opposite to what was described above. We first separate the hexadecimal digits from groups of two to individual digits, i.e. 69 . E8 is written as 6 9 . E 8. Then we convert them into their individual decimal values; which gives us (6 9 . 14 8)10. We then convert each into its binary equivalent, thus giving us

110 1001 . 1110 100

And since zeros to the extreme right of fraction part hold no meaning, we ignore them to get the answer 1101001.111012.

3.3 Operations on Binary Numbers

3.3.1 Binary Addition

Binary addition is similar to decimal addition, but the carry forward in binary to the next digit can only be 1. The key to remember binary addition is

0 + 0 = 0

1 + 0 = 1

0 + 1 = 1

1 + 1 = 10, i.e., 0 with the carry of 1. The boldface digit is the carry. 102 = 210 as we know.

1 + 1 + 1 = 11, i.e., 1 with the carry of 1. The bold face digit is the carry. 112 = 310 as we know.

Let us take the example of adding 110.01 to 1.011. That is 110.01 + 1.011. The first thing we do is align the radix points of the two numbers as we do with decimal numbers. That is

1 1 0. 0 1

+ 1. 0 1 1

--------------

We now, add zeros to the missing places as we do with decimal additions.

1 1 0. 0 1 0

+0 0 1. 0 1 1

--------------

We now add the individual digits using the key given above. Which becomes

1 1 0. 0 1 0

+0 0 1. 0 1 1Page 20 of 66

Page 21: Programming by zb

--------------

1

1 1 1 0. 0 1 0

+0 0 1. 0 1 1

--------------

0 1

1 1 1 0. 0 1 0

+0 0 1. 0 1 1

--------------

0 0 1

1 1 1 0. 0 1 0

+0 0 1. 0 1 1

--------------

1 0 1

1 1 1 0. 0 1 0

+0 0 1. 0 1 1

--------------

1. 1 0 1

1 1 1 0. 0 1 0

+0 0 1. 0 1 1

--------------

1 1. 1 0 1

1 1 1 0. 0 1 0

+0 0 1. 0 1 1

--------------

Page 21 of 66

Page 22: Programming by zb

1 1 1. 1 0 1

3.3.2 One’s Complement of a Binary Number

One’s complement or 1’s complement of binary number is a number that we get by inverting each bit of the number. That is, if the bit is 1, we change it to 0; and if the bit is 0 we change it to 1. Thus, 1’s complement of binary number 1011.001 is 0100.110, i.e. 100.11 ignoring the leading zeros.

3.3.3 Two’s Complement of a Binary Number

Method 1: The first method of getting two’s complement or 2’s complement of a number is first to gets its 1’s complement of the integer part, then add 1 to this 1’s complement, and finally write the fraction part. The result is known as 2’s complement of the number. Thus, 2’s complement of 1011.001 is.

1. Get the 1’s complement of integer part of 1011.001; it is 00100, i.e., 100 ignoring the leading zeros.2. Now add 1 to 100 and write the fraction part after it. This gives us 101.001.

Thus, 2’s complement of 1011.001 is 101.001.

Method 2: Start from the right-most bit in the integer part and keep reading each bit to the left until we reach a 1. Now start inverting each bit after this 1. Thus, integer part 1011 becomes 0101 because the first bit itself it 1. The bits to be inverted are shown with an underline. The fraction part is written after it. Thus, the result is 1011.001.

3.3.4 Significance of 2’s Complement.

In computer science, we represent a number using fixed amounts of bits. Say, if we have to write every decimal number in binary, but we can use only 8 bits (and have to use all 8 bits) then the number 11 10 in binary is no longer 10112 but 000010112 because now we have to use all 8 bits to represent a number.

How will we now distinguish a negative binary number from a positive binary number? We use the convention that if the left-most bit, also called the most-significant bit (msb in short), is 1 then the number is of negative value; whereas, if the msb is 0 then the number is positive.

A binary number representation that can take both positive and negative values is called signed-representation of binary numbers; and the numbers are called signed-numbers. The msb of a signed number is called the signed-bit of that number.

Whereas, the binary number representation that can take only positive values are called unsigned representation, and the numbers in this representation are called unsigned numbers.

So, 000010112 is +11. How can we write -11 in binary? Is it 100010112, by simply changing msb from 0 to 1? The answer is, no. The negative of a binary number is its 2’s complement. This is the importance of 2’s complement in computer science. Thus 000010112 is +11 and it’s 2’s complement 111101012 is -11.

Page 22 of 66

Page 23: Programming by zb

To get back from a negative number to a positive number we again perform the 2’s complement on the negative number, because negative of negative is positive number itself.

3.3.4 Binary Subtraction.

Method 1: It is to note that subtraction of 7 – 5 is nothing but 7 + (-5). That is

A – B = A + (-B), where A and B can be positive or negative numbers.

To understand this, assume that Raju has 7 apples and Sita takes away 5 apples from him, leaving Raju with only 2. This is equivalent to saying that Raju has 7 apples, Sita has borrowed (loaned) 5 apples, i.e. -5, from someone, and Raju would like to fulfil her loan; after fulfilling Sita’s loan Raju will be left with 2 apples.

In other words, subtracting a number is adding the negative of that number. So, to perform binary subtraction, we first find the 2’s complement of the number we are subtracting to get its negative (opposite) and then add this to the number we are subtracting from. For example,

00001001 – 00001011 = 00001001 + (-00001011) = 00001001 + 11110101 = 11111110

Where the boldface binary number is 2’s complement of 00001011. We can check if our calculation is correct. The above subtraction in decimals is 9 – 11 = -2. 2 in binary is 00000010 and -2, i.e., its complement is 11111110.

Method 2: We can remember the following key to perform subtraction

0 − 0 = 00 − 1 = 1, borrow 11 − 0 = 11 − 1 = 0

The logic of 0 – 1 = 1 with borrow of 1 in binary is analogous to in decimal. In decimal whenever the first (upper) digit is smaller than the right (lower) one, we increase the first digits value by 10 (radix of decimal system) and then perform subtraction. But this value is increased because we were able to borrow it from the digit adjacent to (left of) the upper digit. If this adjacent digit is also less than its corresponding lower digit, its value is also increased by 10 via a borrow and the process continues. That is,

0 10 10 The upper digits increased by 10 if they want to borrow 0 9 9 10 The upper digits then decreased by 1 if they a borrowed from 1 0 0 0− 0 1 0 1----------

The Situation is similar with binary, but the upper number is increased by 2 because radix is 2 in binary. The rest of the process is same.

Thus,

1 1 0 1 1 1 0− 1 0 1 1 1----------------

Page 23 of 66

Page 24: Programming by zb

0 borrow of 1 makes second bit 0 from 1. 1 1 0 1 1 1 0− 0 0 1 0 1 1 1 filling the gaps with 0---------------- 1

0

0 2 1 1 0 1 1 1 0− 0 0 1 0 1 1 1---------------- 1 1

0 0 2

0 2 1 1 0 1 1 1 0− 0 0 1 0 1 1 1---------------- 1 1 1

0 0 2

0 2 1 1 0 1 1 1 0− 0 0 1 0 1 1 1---------------- 0 1 1 1

0 0 2

0 2 1 1 0 1 1 1 0− 0 0 1 0 1 1 1---------------- 1 0 1 1 1

0 0 2 0 2 0 2 1 1 0 1 1 1 0− 0 0 1 0 1 1 1---------------- 0 1 0 1 1 1

1 1 0 1 1 1 0− 0 0 1 0 1 1 1---------------- 1 0 1 0 1 1 1

Page 24 of 66

Page 25: Programming by zb

Similarly, the result of 10002 – 1012 = 11

1 0 0 0− 0 1 0 1-------------

2 2 2 0 1 1 1 0 0 0− 0 1 0 1------------- 0 0 1 1

3.3.5. Binary Multiplication

Binary multiplication is done assuming we are performing decimal multiplication. That is 1 x 0 = 0, 0 x 0 = 0, and 1 x 1 = 1. Thus,

1 0 1 1 × 1 0 1 0 ------------ 0 0 0 0 1 0 1 1 0 0 0 01 0 1 1---------------1 1 0 1 1 1 0

3.3.6. Binary Division

Binary division is done the same way as decimal division, but while performing subtraction the binary key given in method 2 of binary subtraction is followed. For example,

___________1 0 1 ) 1 1 0 1 1

Could be done as

1 ___________1 0 1 ) 1 1 0 1 1 − 1 0 1 -------- 0 1 1

1 0 1 ___________1 0 1 ) 1 1 0 1 1 − 1 0 1

Page 25 of 66

Page 26: Programming by zb

--------- 0 1 1 − 0 0 0 -------- 1 1 1 − 1 0 1 ----- 1 0

We have reached to a remainder (102) which is less than the divisor (1012), so we stop. Or, as we perform decimal division we can add a radix point in the quotient and append a 0 to the dividend (11011) to continue the division into the fraction part. That is, we will continue as

1 0 1.0 ___________1 0 1 ) 1 1 0 1 1 − 1 0 1 --------- 0 1 1 − 0 0 0 -------- 1 1 1 − 1 0 1 ----- 1 0 0

1 0 1.0 0 ___________1 0 1 ) 1 1 0 1 1 − 1 0 1 --------- 0 1 1 − 0 0 0 -------- 1 1 1 − 1 0 1 ----- 1 0 0 0

1 0 1.0 0 1 ___________

Page 26 of 66

Page 27: Programming by zb

1 0 1 ) 1 1 0 1 1 − 1 0 1 --------- 0 1 1 − 0 0 0 -------- 1 1 1 − 1 0 1 ---------- 1 0 0 0

− 1 0 1 ------------

1 1, and so on...

3.3.7. Binary Logical Operations

Logical operations result in either true or false depending on the inputs. The inputs must have values that can be either true, or false. Some of these common binary logical operations are given below. We can denote true as T or 1, and false as F or 0.

3.3.7.1. Logical NOT.Logical Not operation denoted by ¬, gives opposite of the input value, i.e., if the input value is true then the result of Logical Not operation is false. Whereas, if the input value is false then the result of this operator is true. Let’s say that our input is A, and it can have value true (T) or false (F), then the corresponding ~A values are:

A ¬AT FF T

A ¬A1 00 1

Note that Not operator is a unary operator, i.e., it is applied to only single operand (here, A).

3.3.7.2. Logical OR.Logical OR (also called inclusive-OR) operation is a binary operator (i.e., is applied to two operands) and the result of this operation is true if any of the two inputs have a value ‘true’, it is false otherwise. In other words, the output is true is any of the inputs is true. Logical OR is denoted by ∨. Let’s say our two inputs (operands) are A and B, then each of the input can be either T or F resulting in the following scenarios:

A is T and B is T; or A is T and B is F; or A if F and B is T, or both are F. The result of logical-or in each scenario is shown below.

A B (A ∨ B)T T TT F T

Page 27 of 66

Page 28: Programming by zb

F T TF F F

A B (A ∨ B)1 1 11 0 10 1 10 0 0

This table of scenarios for a logical operator and given inputs is called its truth table.

3.3.7.3. Logical ANDThis operation is also a binary operation and is represented by ∧. The result of this operation is true only if both the input values are true, it is false otherwise. Thus we have following truth table for this operator.

A B (A ∧ B)T T TT F FF T FF F F

A B (A ∧ B)1 1 11 0 00 1 00 0 0

3.3.7.4. Logical Exclusive-OR (logical-XOR) operator.This operator is also a binary operator and is represented by ⊕. The result of this operator is true if only one of the inputs is T, otherwise it is false.

A B (A ⊕ B)T T FT F TF T TF F F

3.3.8. Bitwise operators.

Bitwise operators are same as logical operators but instead of active on true or false values act on individual bits of a binary number.

3.3.8.1. Bitwise-NOT operator.Bitwise not operator is a unary operator and is denoted by a ~. Bitwise not of a binary number is its 1’s complement; in other words, is the logical-not of each bit. Thus

~10110101 = 01001010

Page 28 of 66

Page 29: Programming by zb

3.3.8.2. Bitwise-OR operatorBitwise-OR operator is a binary operator and is represented by |. The result of bitwise-or operator is performing logical-or operation on corresponding bits (at the same ordinal position) of the two numbers. Thus, 10110011 | 10010111 is

10110011| 10010111------------- 10110111

3.3.8.3. Bitwise-AND operatorBitwise-AND operator is a binary operator represented by &. The result of bitwise-and operator is performing logical-and operation on corresponding bits of the two numbers. Thus, 10110011 & 10010111 is

10110011& 10010111------------- 10010011

3.3.8.4. Bitwise-XOR operator.Bitwise exclusive-OR or bitwise-XOR operator is a binary operator denoted by ^, and the result of this operator is logical-XOR of corresponding bits of the two numbers. Thus,

10110011^ 10010111------------- 00100100

3.3.9. Left-Shift and Right-Shift Operators.

Logical left-shift operator is a binary operator usually denoted by <<. It recursively moves, specified number of times, the bits of number to the left. Suppose we have a binary number of 8 bits, i.e., 1 byte; say, 11011001. Then 11011101 << 3 would mean moving each bit to left three times, giving us 11011101_ _ _. Where the three underlines denote empty places and the scratched bits (110) are the ones that are in place 9, 10, and 11. Since we have only 8 places for bits these bits 110, are forgotten and the empty places that have crept up are filled with zeros. Thus, the result of 11011101 << 3, is 11101000.

Similar to logical-left shift operator is the logical-right shift operator denoted by >>. This time the bits are moved to the right and empty places that get crept up on the left are filled with zeros. Thus,

11011101 >> 3 = 00011011

Incidentally, shifting a binary number to left by 1, i.e. number << 1 is equivalent to multiplying it by 2, and shifting by 2, i.e. number <<2 is equivalent to multiplying by 4; actually number << N, is equivalent to multiplying the number by 2N. Why? Because the power of radix at a position for number representation is one-more than the power of the position right of it. Remember the following?

Page 29 of 66

Page 30: Programming by zb

... + S5 x 105 + S4 x 104 + S3 x 103 + S2 x 102 + S1 x 101 + S0 x 100 + S-1 x 10-1 + S-2 x 10-2 + S-3 x 10-3 + ...

Similarly, number >> N, is equivalent to dividing the number by 2N.

Left and Right-shift operators act on unsigned numbers.

3.3.10. Sign-extended Left-shift and Right-shift Operators

Signed-extended Left-shift is same as normal left-shift operator but represented by <<<. The sign-extended right-shift operator, denoted by >>>, is different from normal right-shift operator.

Before performing >>>, we remember the signed-bit of our original number. Now we perform normal right-shift as usual, but as a last step we replace the msb with our saved value of signed bit. Thus.

11011101 >> 3 = 10011011, because the signed-bit initially was 1.

Sign-extended shifts act on signed-binary numbers.

3.4. Binary Fractions

3.4.1. Scientific Notation and Fixed-Point Notation. Until now we have written binary fractions taking quite a big liberty, using radix point wherever necessary, e.g. 1110.1011. This is not how binary fractions are written in computer arithmetic. Recall that we have used 2’s complement to denote a negative number instead of just appending a minus sign in front of a binary number. Why is that? This is because, in computers, we can only use symbols 0, and 1 to represent any binary number, whether positive or negative. Hence, the symbol ‘–’ finds no place in binary representation. This meant we had to use our intelligence and only symbols 0 and 1 to find a representation of a negative of a binary number; 2’s complement was the outcome of this.

The same goes for binary numbers with fractions. Although it’s quite possible to write a binary fraction that includes a radix point (or a minus sign) by hand, we can’t do such in computers. How should we be able to do this?

If we consider the decimal numbers, we could represent any number in the form S x 10E, where s is called a significand (or mantissa) and E is called the exponent. For example, 2367.8965 can be written as 2.3678965 x 103 with 2.3678965 as significand and 3 as exponent. If you note, we have taken power of 10 to be multiplied to the significand; this is because 10 is the radix (or base) of decimal numbers and moving the radix-point to left or right of its original position is equivalent to multiplying the number with some power of 10. In the above example, while moving the radix point to left by three places we have actually divided the number by 1000. To make the number hold its original value we must multiply it by 1000, or 10 3. This is what we have actually done. This method to write numbers is called scientific notation.

In the same way we can write binary numbers, but now the base would be 2 because this is the radix of binary numbers; in other words 111011.10111 = 1.1101110111 x 25.

Thus, using the scientific notation we can move the position of radix point, or in other words, float it around. The representation of numbers using the scientific notation is called a floating-point representation, in

Page 30 of 66

Page 31: Programming by zb

contrast to the earlier representation binary fraction we were dealing with, called the fixed-point representation.

3.4.2. Precision and Rounding-Off the NumbersSuppose we have only 5 places (ignoring the radix point) to use while representing a fraction and we would want to write 234.5689, how would we write it? Since, we have 5 places to write 8 digits we would have to ignore the least important digits; which are the ones holding the least value. The least value is represented by the right-most 9, then by previous 8. This is so because if we ignore this 9, we would reduce the number by 0.0009, whereas, if we ignore 6 we would reduce the number by 0.06. Clearly, 0.0009 is much less than 0.06. So, if we ignore the 89 from the fraction part we would get 234.56. This is our solution.

The above solution is less by an amount of 0.0089, clearly; this reduction amounts to a certain calculation error (of –0.0089) on our part. Can we reduce this error and still able to represent the above given number with 5 digits? Yes we can, and this is done by rounding the number.

While rounding the number we increase the last digit of our answer by 1 is the next digit of the previous number was either 5 or more. In our case of 234.56, the next digit to 6 was 8 (in 234.5689) and we increase the value of 6 by 1, giving us 234.57 as our final result.

This result is an increase by a value 0.0011, or in other words, a calculation error of +0.0011. You would ask why we would want to increase the number if we would still get a calculation error. The answer to this is that we would always like in the reduction of error margin, whether positive or negative. That is, we would want to ignore the sign of error and see which reduction is less, clearly 0.0011 < 0.0089.

When we are made to reduce the digits of our number we lose the power to measure the number accurately; here places represented by 8 and 9 are lost. This power to write a number accurately is called precision. In the above example we have lost precision up to two decimal places in fraction part.

Precision is an important concept in computer arithmetic because we only have a fixed (constant) amount of bits to represent numbers.

3.4.3. Single and Double Representation of Binary Floating PointWe generally use 32 bits or 64 bits to represent a binary floating-point number; the earlier one is termed as single representation of a binary number and the latter the double representation. We arrive at single and double from the fact that we gain a double precision using 64 bits than using 34.

3.4.3.1. Single-Precision Floating-Point RepresentationWe will first look at the single representation. This representation has been standardised by IEEE (Institute of Electrical and Electronics Engineers) committee, an institution dedicated to further innovation and standardization in electrical engineering and electronics.

Single Representation

As shown above the 32 bits are divided into three regions. The first one from 0 – 22 holds the significand, next comes space to store exponent (23 – 30), and finally the last bit is used as the sign bit.

As always the sign bit is 0 for a positive number and 1 for a negative number.

Page 31 of 66

Page 32: Programming by zb

Since we have only 8 bits to store the exponent, and if 1 bit from that is used to store the sign bit, we would get the maximum positive value to be 01111111 = 64+32+16+8+4+2+1 = 127. Taking 2’s complement of 127 we would get -127 = 10000001 as the minimum negative value. But single precision format doesn’t use 2’s complement to store negative exponent because calculating 2’s complement just to write an exponent part of a binary fraction is too much of time than necessary.

Instead, we chose not to deal with a sign bit in exponent and to store an exponent we add a value of 127 to it. This addition of a certain number (here, 127) to get to a final value is called biasing. Thus -1 would be stored as -1 + 127 = 126, and -127 would be stored as -127+127 = 0.

In other words, if exponent is 00000000, we would assume it to be 0 – 127 = -127 after removing bias. But the single precision representation treats an exponent of 00000000 as a special case and we are asked not to assume exponent 00000000 to be -127 but a special case mentioned later.

Thus the minimum value of exponent is 00000001, i.e., 1 – 127 = -126 after removing bias; but what about the maximum value? You would assume that 11111111 would be the largest exponent value, giving us 255 – 127 = 128 after removing bias. The exponent 11111111 is also a special case of representation as explained later. Thus the minimum and maximum values of exponent a single can take are -126 and +127 respectively.

We don’t have to store any sign bit in the significand part because the sign of significand is stored as a separate bit, the 32nd bit. In addition we will assume that our binary number will always have only one digit before the radix point but it will never be 0, i.e., it will never be of form 0.1100011 x 2 5 but 1.100011 x 24. We will take the special case of 0 being the digit before the radix point later.

If 0 can’t be the leading (starting) digit of the significand, i.e. the digit before the radix point, then the only digit which can take this place is 1. This is a significant observation because now we don’t need to store this starting 1 (most significant bit) in our representation. Instead we will assume that there is always a leading 1. Thus, if the significand is 1.01110110110111011011111, a 24 bits number, we can ignore the most significant bit to get a 23 bit number .01110110110111011011111. We have essentially made a representation in which a 24-bit number could be stored in 23 bits, assuming that the most significant bit has to be appended to it to get the final number.

Single representation takes the advantage of this situation and assumes that the number always has a leading 1 in the significand and the radix-point is after this first digit, both of which we don’t store in the single’s significand’s representation. The significand stored in the 23 bits as shown in the above figure has its most significant bit removed.

Let’s take an example, what value does the single number 11000000101101000000000000000000 hold? To calculate this we first need to divide the above representation into its three parts, sign-bit, exponent, and significand, as 1 10000001 01101000000000000000000. As we see the sign-bit is 1, which means this is a negative number.

Now comes the exponent part, which is 10000001 = 1x27 + 1x20 = 128+1 = 129. If you remember we had added a bias of 127 to get to the final value of exponent, so we would need to remove this bias to get to its original value. Thus, the original exponent value is 129 – 127 = 2.

The next part is the significand. We have 23 bit significand 01101000000000000000000, but we have assumed that the significand always starts with a 1 and chose not to write this bit. Thus, to get to the original significand we will have to append a 1 as the most significand bit. Our original significand thus becomes 1.01101000000000000000000, which is just 1.01101 removing the trailing zeros.

Thus our number is -1.01101 x 22, which is – (1x20 + 0 x 2-1 + 1 x 2-2 + 1 x 2-3 + 0 x 2-4 + 1 x 2-5) x 22.

Page 32 of 66

Page 33: Programming by zb

= - (20 + 2-2 + 2-3 + 2-5) x 22 = - (20 x 22 + 2-2 x 22 + 2-3 x 22 + 2-5 x 22) = - (20+2 + 2-2+2 + 2-3+2 + 2-5+2)= - (22 + 20 + 2-1 + 2-3) = - (4 + 1 + ½ + 1/8) = -(32 + 8 + 4 + 1)/8 = -(45/8) = -5.625

Now let’s convert from a decimal number into a single. Say we have the decimal number 329.390625, how will we represent it in the single format?

First, we will have to convert it into fixed point binary number by taking integer and fraction parts separately and converting them to their binary equivalents. The integer part of our number is 329 and its binary equivalent is 101001001, which we can find by the process of continuous division by 2 as explained earlier. The fraction part of our number is 0.390625 and its binary equivalent is 011001, which we can find by the process of continuous multiplication by 2 as explained earlier.

Remember that we can arrive at a situation where the calculation of fraction part is never ending or when the bits in the calculation of binary part increase to more than 23. In that case we stop and use those 23 bits as our fraction part. Now we can combine the two to get the final binary number in fixed-point format as 101001001.011001

We will now convert the fixed-point representation into the floating point representation using scientific notation with base 2, and using only single digit before the radix point. This gives us, 1.01001001011001 x 2 8. We have an exponent of 8 because by moving the radix point to the left by 8 places we have in turn divided the original number by 28; hence we will have to multiply it by 28 to hold its original value. We are now ready to denote this binary number into its single representation.

The first thing to note is that the number is positive, hence, the sign bit in the representation will be 1. The second thing to note is that the exponent has the value 8. Hence, after biasing the exponent with 127 we arrive at the final value 8 + 127 = 135, which we know in binary is 10000111. The third part of the representation is the significand. Our significand is 1.01001001011001, but since we do not write the leading 1 and the radix point in the significand of the single representation, it will be stored as 01001001011001.

Since the number of bits used in the significand of a single are 23 and all of them have to be filled, we append trailing zeros to the fraction part of the significand to make it 23 bit. Thus, our significand is actually 01001001011001000000000.

The final representation is <sign-bit><exponent><significand>, which comes to:

1 10000111 01001001011001000000000 or 11000011101001001011001000000000, where the boldface exponent separates the other two parts.

If you remember, we have made two assumptions in the above representation, the first that the exponent can’t take values of -127 and +128, and the other that there is always a leading 1 in the significand. Now we will deal with the situations where we will give answers to why we have assumed above.

Before we start note that all the numbers that fit into our single representation above are called normalized numbers and all the numbers (described below) that don’t fit in this representation are called denormalized numbers.

The case when the exponent is -127, gives us the exponent representation of -127+127 = 0 = 00000000 after removing the bias (of 127). As we have we have written earlier, we don’t take this as an exponent of -127 because this is a special case for us. If the exponent is all zeros, i.e. 00000000, and the significand is not 00000000000000000000000, we assume that there is a leading 0 in the original significand with the exponent of -126. Thus, these numbers take the form of 0.significand x 2-126.

Page 33 of 66

Page 34: Programming by zb

The exponent of +128 gives us the exponent representation of 128+127 = 255 = 11111111 after removing the bias. This exponent also holds a special meaning. That is, we can’t have numbers with exponent of 128. When the exponent is 11111111 and the significand is 00000000000000000000000 the value of single number is assumed to be infinity (uncountable).

Also, when the exponent is 11111111 but significand is not 00000000000000000000000, it means that the value of this representation is not a number, written as NaN (but say some symbol).

Similarly, when the exponent is 00000000 and the significand is also zero, that is, 00000000000000000000000, then the single represents a value 0. That is 0.0 x 2-126 = 0.

3.4.3.2. Double-Precision Floating-Point RepresentationDouble representation is largely calculated the same we as single precision barring some differences. The first difference is the amount of bits used to represent exponent and significand as shown below.

As shown above we now have 11 bits to represent the exponent and 52 bits to represent the significand. Hence we have almost doubled the precision with which we can store our binary fractions.

The exponent bias in double representation is 1023 (instead of 127 in single), and the minimum and maximum values allowed for the exponent are thus – 1022 (1 less than the bias 1023) and 1023 respectively.

The special case of exponent being all 0’s and 1’s and significand being 0’s and 1’s are the same as in single representation.

Page 34 of 66

Page 35: Programming by zb

4.0. A Little Bit of Science

Almost all the substances that are obtained in nature, including us humans come in combinations of smaller individual material. These individual materials are called elements, and the substances that they combine and make are called compounds. For example, water is not really water as a whole but it is a compound of elements named hydrogen and oxygen. Similarly, iron is not found alone but as a compound consisting of elements iron and oxygen. Even we are made up of different compounds, majorly consisting of elements carbon, hydrogen, and oxygen.

Most of the substances, which we call matter, can be divided into majorly three categories; namely solids, liquids, and gases. The difference between the three is how they occupy the container they are in. Gases will expand their shape to take the shape of container no matter how large the container is. Similarly, liquid though doesn’t expand, takes the shape of the container. If however, the container is very large than the quantity of liquid present then the liquid will change its shape to occupy as much container as possible and thus will eventually sit on the bottom of the container.

Solid on the other hand doesn’t change its shape, and thus, if its size is larger than the container it won’t fit in it.

Compounds and elements can be in any of these three states when found in nature, which we call their normal state. Also, if proper pressure or temperature is applied to compounds they change their normal state.

4.1 Energy

The term energy is not alien to us. We say we are energetic when we are ready to do some work; and are not when we feel tired. What is the state of being tired? Tired means we don’t have enough energy currently to do some work. It’s not just living beings that are used to being energetic but non-living things too. This is because energy is a fundamental concept in physics to explain things and their behaviour. If a ball is suspended in air, we say that it has energy to fall down; falling down is equivalent to doing some work. Similarly if a hot iron rod is heated up we say that it has enough energy to (do work and) heat up a vessel of water.

In addition to that there are various forms of energy, e.g., heat energy is gained by an object when it is heated up, an object suspended in air has a potential (to fall) energy, a moving object has a kinetic energy (energy for motion), plus many more forms of energies are available.

In our universe, law of conservation of energy holds and is the most fundamental laws of physics. It states that the total energy of our universe is constant, i.e., can’t be changed; but energy can be converted from one form to another.

Hence, for an object to gain some energy it must be provide to it by an external body. Similarly, when an object loses some energy, it is gained by some other object. The two most important energies in computer science are electrical energy and magnetic energy which we will deal with later.

4.2. The Structure of Elements

We now know that an element usually isn’t found alone but attached to other elements in the form of a compound. That is a compound is composed of elements. Until not long ago people believed that the

Page 35 of 66

Page 36: Programming by zb

elements were not composed of anything else, but they were mistaken. Many experiments showed that what we see as solid objects have holes in it that are not visible through our naked eyes. An example would be the procedure of X-ray photography. During X-ray photography a part of our body is subjected to x-ray radiation and most of this radiation passes through our body because the radiation is much smaller than the holes (which aren’t visible through a naked eye) present in our body. The part of body which doesn’t allow the X-ray radiation to pass through it (usually the bones) is visible in the photograph.

So, what are elements made up of? Elements are made up of small ball like particles that we call atoms. In fact these atoms are also made up of smaller particles.

4.2.1. The Structure of an Atom

An atom consists of a thick ball like portion and many small particles moving near it. This thick portion of an atom is called its nucleus, and the small particles moving near the nucleus are called electrons.

The fact that electrons move around the nucleus in some fashion but not fall farther from it should make us think what is keeping them together with the nucleus. We have seen this feature at least once in our life; it’s called magnetism. Two bar magnets are attracted to each other or repelled depending on which end of one magnets is held near the other. The two opposite ends of a bar magnet are given names North and South poles; and when north pole of one magnet is brought near the south pole of the other, the magnets are attracted to each other. But when same type of poles, either both north or both south, of these two same magnets are brought near it makes the magnets repel each other.

The same is true for the case of an atom; the nucleus and total electron behave as one magnet with electrons making one magnetic pole and the nucleus the other. We can denote any of these as north or south, it doesn’t matter, in fact, we choose not to use north or south altogether. Instead, we denote the two as positive or negative poles. By convention an electron is taken as negative and the nucleus positive.

Thus, two electrons repel each other (there is a caveat but it is not necessary here) and every electron is attracted to the nucleus, but doesn’t normally get attached to it. This strength or ability of a particle to attract or repel some other particle is called its electrical charge. The charge on an electron is used as a unit (value 1) charge.

The sum of electrical charges of individual electron is equal to (but opposite in sign) the charge of the nucleus. That is, the ability of a nucleus to attract/repel is equal to the total ability (to attract/repel) of electrons but has an opposite sign.

Since the sum of charges on all electrons is balanced (cancelled) by the charge on the nucleus the atom is electrically neutral; that is has no charge of its own.

Until now we were able to reduce element to tiny particles called atoms, and atoms to electrons and nucleus. Can we reduce electrons and nucleus into even smaller particles? The answer is no of electrons, but yes for nucleus.

Atoms have some weight, but all electrons of an atom taken together make fraction of this weight. Almost all of the weight comes of an atom comes from its thick nucleus. Nucleus itself is composed of two types of particles. The first of them is much heavier than an electron but has the same (but opposite) charge of an electron; and their number in the nucleus is equal to the number of electrons in that atom. These particles are called protons. The other kind of particle in the nucleus is electrically neutral but slightly larger and heavier than the proton.

Page 36 of 66

Page 37: Programming by zb

This structure of atom is same irrespective to the element under consideration, but the number of protons (which equal to number or electrons) and number of neutrons in atoms of different elements varies. Below is a depiction of an atom.

In the middle of an atom is the heavy nucleus comprising tightly bound protons and neutrons, and electrons move near it, not necessarily in circles. The electrons that are near the nucleus have a strong attraction towards it, and hence, less energy necessary is need to keep them near the nucleus than the electrons that are father from the nucleus. The electrons that are enough farther from the nucleus that the force of attraction of nucleus is too weak possess very high energy, and hence, are capable of leaving the hold of nucleus is some energy stimulus is provided to them. There are billions of atoms in very small piece of material.

4.2.2 Atomic Bonds

As learnt above electrons move near the nucleus in regions. But there is one important point to remember in addition to this. It is that the electrons tend occupy a region closest to the nucleus, i.e. with lowest needed energy, but this region depending on its relative position to the nucleus can contain only a certain maximum number of electrons. After the region is filled with the maximum allowed electrons, any other electron would try to occupy the region which is next farther to nucleus that this region, i.e. a region at next energy-level. When this region is full with maximum allowed electrons, any new electron will fill a region that is at the next energy-level. This process would continue until all the electrons that an atom has occupy their relative positions with respect to the nucleus. These different regions of incremental energy where electrons can be are called orbitals, and come in varying shapes. Electrons present in an orbital with full electron capacity tend to be in a satisfied state, i.e., are controlled by the attraction of nucleus.

Most of the elements have atoms comprising orbitals which haven’t filled up their quota of electrons. And electrons in these orbitals are loosely bound by the attraction of nucleus. Two or more atoms in these elements come near each other so as to share their electrons in unfilled orbitals. These shared electrons tend to move in a region that is shared by these atoms. How many such atoms come together to share their electrons depends on the maximum allowed electrons in their unfilled orbitals. For example, an unfilled orbital in a hydrogen atom needs one more electron to complete its capacity. So, two hydrogen atoms come together to share their electrons. Now these two shared electrons move in a space that is near to each of the hydrogen nucleus.

Page 37 of 66

Page 38: Programming by zb

This coming together of atoms so as to satisfy the electrons in their unfilled orbitals is called a covalent bond. Following shows how a single Silicon (Si) atom shares its four electrons in its outermost orbital so that the orbital is complete with 8 electrons in it.

Some of the elements and compounds have atoms in which the electrons in outermost (from the nuclei of constituent atoms) orbitals of an atom move free to neighbouring atoms, whereas in some elements and compounds they are tightly bound together to their respective molecules. The electrons in the former case are called free electrons.

4.3. Electricity

If we were able to provide enough energy to excite the free electrons of an element or a compound to break them free from their orbitals and move in one direction for some time, we give rise to an electric current. Electric current causes many effects, like heating, magnetism, radiation etc, and is an essential source of life today.

This energy that we could provide to excite the electrons is called an electric potential energy (or simply, electrical energy), and if it is measured per unit charge, i.e. electric potential energy / (charge of an electron), is called electric potential or voltage. Electrical energy can be converted into other forms of energy and can be used to perform some work.

There are five ways in which we can get electric current, viz. magnetism, chemical change, heat, piezoelectric effect (pressure applied on certain crystals), and light.

A battery that we install in a TV remote control is an example of chemical change producing necessary voltage. After chemical reaction inside the battery electrons get accumulated at one end of the battery (thus, the negative end) from the other end which gets deprived of electrons (thus, the positive end). So if we connect a copper wire, which has enough free electrons, to the two ends of a battery (symbol –|l—, with the taller end denoting positive side and smaller the negative side) we would create an electric current. This is because the free electron in the wire would try to reach the positive side of the battery because of strong force of attraction.

Inside the battery the chemical change continues to produce electrons from positive end to negative until the chemical reaction keeps on happening, which for a battery typically lasts for a few days to months and even years. So, the electrons that get accumulated from the wire at the positive end of the battery get transmitted to the negative end inside the battery (due to chemical reaction), from where they again get transmitted to the wire. This looping of electrons keeps on producing electric current. This process is shown in the diagram below.

Page 38 of 66

Page 39: Programming by zb

The figure also shows the direction of electron flow, or the flow of electric current. Since electron is electrically negative we write that the direction of flow of electron is the direction of flow of negative electric current. In other words, the direction of flow of positive electric current is opposite to the flow of electrons. The positive electrical current is by convention what we call an electric current, and in our electrical diagram the direction of electric currents is shown opposite to the direction of electron movement.

Electrical conductivity (coming from word conduct meaning to direct) is the property of a substance to allow electrical current (movement of electrons) to pass through it. Substances like copper have very many free electrons and hence it has high electrical conductance. In other words, copper is a good electrical conductor. The opposite of conductors are insulators which under normal circumstances don’t allow electron flow easily through them because there are very less free electrons, i.e., even the electrons in the outermost orbitals are tightly bound to their respective molecules.

Different elements or compounds have different electrical conductivities because of the difference of how free are the free electrons in each of them.

Even while moving through the conductor due to presence of an electric potential across its ends, the free electrons collide with other atoms; some rebound and some are slightly deviated from their path. This collision of free electrons with other atoms gives rise to a slight resistance to electrical current, which normally is insignificant.

Due to various circumstances this electrical resistance of a material may be increased. For example, if we increase the length of the conductor the amount of collisions will increase because the free electrons will now have to travel more, or if we decrease the cross-sectional area of the conductor the passage for free electrons will not be reduced. If we heat the conductor, its molecules start to vibrate vigorously until the conductor loses heat naturally with passage of time; but during this passage of time due to vibrations of molecules the amount of collisions, and hence, resistance increases. Some elements or compounds have high density of molecules in the same measure of area as other elements or compounds, and hence have higher resistance. The strength of a material to provide resistance to electric current is called electrical resistivity of the material, and the material is called an electrical resistor ( or ). We say that insulators are ideal resistors because they don’t allow electrical current to flow through them in normal circumstances. An example of a resistor is the filament in the light bulb. Due to high resistance (lots of collisions of electrons) it produces heat, and hence, glows. Another example would be a portable water heater metal element.

One point to note is that in case of insulators which normally don’t allow electrons to move through them, increasing enough temperature causes the electrons in outermost orbitals of their molecules to gain enough energy and be free from their molecules. Thus, with increase in temperature the conductivity of insulators increases. But amount of energy required is considerably high.

Current, Voltage, and Resistance are related to each other as we have already seen. Mathematically they are related as: Voltage = Current x Resistance.

Page 39 of 66

Page 40: Programming by zb

If we denote voltage by v, resistance by r, and current by i, we have

V = I x R

There are some situations in which within an electric circuit (electrical devices connected via conductors, to do some useful work), we would like to store electric charge (electrons) momentarily, and then use this as a small battery to be used later. The electric component that fulfils this requirement is called a capacitor (–||–, similar to battery but both vertical lines are of same size). In other words, capacitor is a temporary storage for electric charge. A capacitor is made up of two flat conductors (conducting plates) with an insulator sandwiched between them. The following diagram shows how a capacitor is charged.

After the capacitor is charged it can store the charge in it for some time depending on its size, there are tiny capacitors used in electrical circuits and then there are capacitors as large as five feet to store charge. Whatever their size, charged capacitors should not be touched as they can give electric shock because the stored charge will travel through our body; yes, our body is a good conductor of electricity.

4.4. Semiconductors – The Building Blocks of Computers

Until now we have seen that materials can be either conductors or insulators; but in nature there exists some materials which are not so good conductors but also not insulators. Since, there are conductors, but not fully, these materials are called semiconductors.

There are many interesting properties that semiconductors exhibit, first they behave as insulators because increase in temperature causes them to conduct more electricity. Bringing them close to light causes increase in their electrical conductivity, and if there is a difference in temperature between any two points in these materials they act as battery. Also when two specific kinds of semiconductors are brought in contact to form a junction, the junction allows current to flow through only in one direction irrespective of the direction battery is connected to the junction; moreover this junction of two specific kinds of semiconductors tend to act as a battery when brought near a light source.

But the most important reasons of the use of semiconductors in computers are two.

First, as explained earlier we can increase the length of the conductor to provide more resistance, or even heat it. With semiconductors we can add comparatively minute amounts of specific material to make them

Page 40 of 66

Page 41: Programming by zb

better conductors of the strength we need, which is much less than that of typical conductors. So if we add less amounts of this material in one semiconductor that the other, the latter one would be a better resistor than the former. This way we can control the relative resistance of semiconductors. Since, we only add minute fraction of secondary element, it can be written that we are adding impurity to the otherwise pure material. This addition of impurity to semiconductor is called doping.

Second important reason is how electricity is conducted. We know we get electric current because of movement of electrons, and good conductors are rich in free electrons. But semiconductor when doped with specific element can conduct electricity by absence of some free electrons.

To look at the two points closely, let’s see how silicon, a semiconducting material, is laid out at atomic level. Following is the structure of silicon at the atomic level which we have already seen.

Every silicon atom shares its electrons in outermost orbital with four neighbouring atoms so that the needed 8 electrons in that orbital to make it full are acquired. Similarly, the four silicon atoms shown above still have 3 more electrons less than needed to fill their outermost orbital. They will combine with neighbouring 3 silicon atoms and the process continues. This gives the following structure of the silicon crystal.

Here each line stands for an electron shared between connected atoms. But this structure is very tightly held, and hence the amount of free electrons need to conduct electricity are absent. What if we were to add an impurity such that it was similar to silicon but have one electron more in its outermost orbital? Of course, it will no longer be pure silicon but some compound. Let’s say, we add an impurity of Arsenic (As), we will get

Page 41 of 66

Page 42: Programming by zb

As can be noticed every atom of arsenic combines with four neighbouring atoms of silicon by sharing its 4 electrons. But an important point to notice is that its fifth electron in outermost orbital leaves it because only eight electrons could be filled in that orbital. Thus, this electron is absolutely free from the clutches of nuclei of atoms. By this way we can make a semiconductor a good conductor of electricity but of certain needed value. This type of semiconductor where combining impurity results in free electrons is called an n-type semiconductor; n-type because the current is conducted by free electron which is negative. The point to remember is that the above figure is not repeated continuously, rather, arsenic atoms get placed at random positions in the final structure.

An important point to remember is that even after generation of a free electron the net charge of the material is zero. This is because we have not induced any extra charge in either silicon or in arsenic atoms, their electrons are just being shared in some way.

But what if we combine silicon material with an impurity that has less than 4 electrons in its outermost orbital? Let’s say we add Boron (B) to Silicon (Si) to make a new doped semiconducting material. The structure of this material would look like following.

As seen each boron atom shares its electrons in outermost orbitals with neighbouring four silicon atoms; and one of this neighbouring silicon atom will not get full share 8 electrons in its outermost orbital. The same is true for the boron atom. Thus, between boron atom and one such silicon atom we get an area which will be deprived of electron, and this deprivation of electron is called a hole. There will be many such holes in this

Page 42 of 66

Page 43: Programming by zb

material as the above figure could be a millionth part of whole amount of material. Electrons in the vicinity of a hole have a tendency to move and fill that region, leaving a hole in their previous positions. In a way, we can write that the holes move inside the materials. If an electron is considered a negative charge, the hole is considered a positive charge, and hence, the semiconductors which have holes are called p-type semiconductors – p stands for positive. Here too the net charge of the semiconductor material remains zero because of the same reasons.

Free electrons and holes are called charge carriers in semiconducting materials.

What will happen if we bring n-type semiconductor and p-type semiconductor in contact to form a junction?

A P-N junction is shown above in where. As soon as the two materials are brought into contact, the free electrons try to move into P region, because they are free to move. Also, the holes in the P region are filled by the electrons in the neighbouring N atoms. This movement is shown in the second bit of the above figure.

Every movement of a free electron from N to P region will add to a –e charge in P and leave a +e charge in N region. Furthermore, filling the holes in otherwise neural atoms in P by electrons from N atoms will make the filled P atom –e charged and the atom in N which loses of its electron (thus, forming a hole there) +e charged. This is shown as the third bit of the above figure. Charged atoms are called ions by convention.

Afterwards, the newly arrived free electrons in P will fill holes in nearby P atoms. Also, free electrons in N will fill holes created in N atoms. This further more creates a few P atoms with –e charge and a few N atoms with +e charge. This is shown as the fourth bit of the above figure.

This process continues until enough charge ions are created in both ends. This creates a layer which is deprived of free electrons and holes, and thus no electric current can pass through this region. That is the depletion region acts as an insulator. In addition, this region stops further interchange of free electrons from N and holes from P into opposite ends. Depletion region is thus neither P nor N region.

Let’s see what happens when we connect this PN junction to a battery. Let the positive side of battery be connected to the P end of this junction as shown.

Page 43 of 66

Page 44: Programming by zb

Before the PN junction is connected to a battery there is a depletion region between P and N sides. But as soon as enough voltage is applied at the two ends (via a battery), with the P end of junction connected to Positive terminal of the battery and the N end of the junction connected to Negative terminal of the battery, free electrons are pushed towards positive terminal of the battery because of attraction from this terminal (or repulsion from the negative terminal), to overcome the depletion region. This causes current to flow the through the circuit.

On the other hand, if the battery is connected the other way, something different happens, as shown in the figure below.

In this case, under normal conditions, the free electrons are pulled towards the positive terminal but don’t flow through the junction. Towards the P end the electrons near the battery are pushed slightly to the right, and this causes the concentration of holes to be more towards battery side, in other words electrons (not free electrons) move and try to fill the holes to the right. Similarly, in the N region due to the attraction of electrons to right, some electrons leave their orbital from left hand side to N to the right. What we get is even bigger depletion region. But under these normal conditions the electrons from P end don’t jump into N end.

Only if the applied voltage is high enough will (higher than the voltage required in the earlier diagram) will the electron from P region move towards the N region, thus forming the current. In this case the voltage required to cross the barrier is called the breakdown voltage for the junction.

The first diagram is called forward-bias connection for the PN junction, and the next one is called reverse-bias for the PN-junction. Moreover, any electrical component that allows current to pass through it only in one direction is called a diode. PN junction is thus, an example of a diode.

The symbol of diode is

The +ve and –ve signs are unnecessary because the base of the triangle is always considered positive (P-type).

Page 44 of 66

Page 45: Programming by zb

One example where diodes are useful is when we would want delicate circuitry (say, remote control) not to get damaged just because battery is connected improperly, i.e., where we should connect the positive terminal we mistakenly connect the negative instead.

As can seen from above is we insert a diode in the circuit just after the battery casing as shown, the current will flow only if the diode is forward biased. This will only happen if the battery is connected properly as indicated by the signs on the battery casing.

Diodes are very powerful components, because they allow current to pass through depending on the voltage polarity (sign) and are important components in any circuitry, let alone computers. But we have not yet discussed the most important of all the components in the digital circuits.

What should happen if we sandwich N-type semiconductor between two P-type semiconductors (PNP), or if we sandwich P-type semiconductor between two N-type semiconductors (NPN)? What we get is a (type of) transistor. Transistor gets the name from transfer resistor. It is essentially a register when supplied with an amount of voltage as input, produces even larger voltage as output, i.e., it gains energy. Of course, it doesn’t create energy, just acquires from its surroundings.

Let’s take the following circuit as an example; in this case we have taken an NPN transistor, where the P (electron deficient) region is very small compared to its surrounding N (with free electron) regions. In addition one N region is large compared to the other. We call the larger N region as the emitter, the other N region as collector, and the middle P region as the base of this transistor.

The above diagram shows a typical NPN transistor and its symbol. The symbol in NPN has the arrow in the emitter pointing outside and the key to remember is NPN could be Not Pointing iN. We have to show arrows

Page 45 of 66

Page 46: Programming by zb

to differentiate collector form emitter in the diagram. Also, whether NPN or PNP, the emitter region is always larger than the collector and the base region is always narrow.

An important thing to notice in the above picture is that the above transistor can be seen as two individual junction diodes if we ignore the third region. Thus, the above transistor can be seen as a diode with N side to Left, and another diode with N side to the right. So, if we give a forward bias voltage to the emitter –base diode, it conducts electricity as shown below.

The free electrons are pushed into the base, also initial (before voltage was applied) holes in the N region are filled with by some electron in the N region. These two contribute to current in emitter. Some free electrons that were pushed into the base come out of the base, also some try to fill in the hole, and some non-free electron also try to fill hole. All these contribute to current in the base. The direction of current is taken, by convention, as opposite of electron flow and is shown by directed arrows marked i. The applied voltage is shown as Veb, i.e., voltage between emitter and base. This diode has some inherent resistance, and thus the current flowing through it will be i=Veb/r, where r is the resistance of the diode. Suppose, we were able to push majority of the electrons now through the PN region (right side junction), we would get a voltage through it as it too has some resistance, say, r2. Then the voltage across the collector and base will be Vnew =imajority x rnew; and what is the resistance rnew is more than r?

How to push these electrons to the N region and not let them come out of base? The answer is simple, the P-N (where N is the collector end) is also a diode, we can reverse bias it to disallow a current through it, in other word resistance rnew will be infinite ( > r). Thus we can do the following.

Here base and collector diode is connected in reverse bias and this will disallow current through the bold line circuit. This means that resistance is very high.

What happens in the three regions is given below:

Page 46 of 66

Page 47: Programming by zb

As shown in the above figure, free electors in the emitter are pushed by applied voltage to fill up holes in base, give by arrow marked (a). Thus contributes to emitter current. Some of the free electrons in the emitter combine with holes now present in the emitter. This too, but negligibly, contributes to base current.

Electrons from emitter that combine with holes in the base, and free electrons (as shown in b) that are pushed into the base from emitter are pulled towards base. But these are very little in number and amount to base current.

Since, the base-collector junction is reverse-biased no free electrons from collector are pushed into the base.

In addition, since the base is very small, the force applied by voltage to free electrons in emitter, and by the second voltage at the base, is enough for them to go into base and then into the depletion region – shown by arrow (c).

Once, into the depletion region they are pulled by the positive ions into the depletion region and thrown into the collector. This is shown as (d). This amounts to collector current.

Electrons that are pulled into the collector are 99% and electrons that are pulled from base to battery are 1% of total moving electrons.

Thus toward emitter end we have Vin = iE x r, and towards collector we have Vout = iC x R. Thus, since R >> r, Vout

is greater than Vin. Thus, the input voltage is amplified (increased), and the transistor works as an amplifier. The circuit where we can check this new voltage is shown below as dashed line.

The above configuration is called the common-base configuration because base is connected in between the two batteries. This is an uncommon configuration for transistors because we generally don’t require that much of voltage gain as it will damage the circuit. Following is common-emitter configuration.

Page 47 of 66

Page 48: Programming by zb

The working of common-emitter is same as that of common-base, but common-emitter configuration is used as a digital switch. If instead of a battery some other circuit was responsible for the current through the base then the transistor could be used as a digit switch (not mechanical).

If the circuitry passed even a little current through the base then the above transistor will allow the passage of current from collector to emitter, i.e. transistor, just like a switch, would turn on; but if the circuitry didn’t pass enough current into the base, then the transistor would turn off like a switch and won’t allow current from collector to pass through emitter.

4.5. Transistors on a Chip

Normally transistors are a few millimetres wide just like diode. But, modern computers need more than a 50 million (to a billion) transistors to function. If these measurements of width and height are correct we would have to build a computer which will fill an apartment. Fortunately, a few decades ago we were able to build a full circuit, with millions (even more than a billion) of transistor, capacitors, resistors, diode, insulation, and their connections on a single small block of silicon (called silicon wafer or substrate). This single small block is called an integrated circuit, or in common language a chip.

Following show how the above mentioned components are made on a silicon wafer. We need to remember that each of these diagrams is actually smaller than the width of our hair; actually close to 130 x 10 -6 mm, i.e., 0.00013 mm. Compare it with width of our hair, which is 200 x 10 -3 mm = 0.2 mm. It’s closer to thousand times wider than the transistors on modern chip.

Each component on a chip is called a feature.

*

The insulation used in the above figures is an oxide of silicon (silicon dioxide SiO 2) that can be formed by heating silicon wafer using water vapour.

We will now list in short how the modern chips are produced.

Page 48 of 66

Page 49: Programming by zb

4.5.1. Chip Production

Building an integrated circuit like a computer chip is a very complex process. It is divided into two major parts, front end and back end. In the front end, you make the components of the circuit. In the back end, you add metal to connect the components and then you test and package the chip. Below is a simplified description of the steps.

Front End – Construction of the Components

1. Various designs of a chip need to be considered before one of them is selected for manufacture. To do this computer design and simulation is used.

2. After the simulation the circuit plan mask (stencil) is printed.

3. Under precisely monitored conditions, a pure silicon crystal is grown. Circuit manufacturing demands the use of crystals with an extremely high grade of perfection.

4. The silicon is sawed into thin wafers with a diamond saw. The wafers are then polished in a number of steps until their surface has a perfect mirror-like finish.

5. The silicon wafer is covered with a layer of insulating silicon oxide.

6. A covering film of protective material is put on top of the insulating silicon oxide. This material, a bit like the film in any ordinary camera, is sensitive to light.

7. Ultra violet (UV) light is shone through a mask and onto the chip. On the parts of the chip that are hit by light, the protective material breaks apart.

Page 49 of 66

Page 50: Programming by zb

8. The wafer is developed, rinsed and baked. The development process removes the parts of the remaining protective material exposed to light.

9. The wafer is treated with chemicals in a process called "etching." This removes the unprotected insulating material, creating a pattern of non-protected silicon wafer parts surrounded by areas protected by silicon oxide.

10. The wafer is run through a process that alters the electrical properties of the unprotected areas of the wafer. This process is called "doping." Steps 5-10 are repeated to build the integrated circuit, layer by layer. Other layers of conducting or isolating layers may also be added to make the components.

Back End – Adding the Connecting Wires

11. Finally, when all the components of the chip are ready, metal is added to connect the components to each other in a process called metalization. This is done in a way similar to the making of the components. First a conducting metal like copper is deposited over the chip.

12. On top of the metal a layer of UV-sensitive photo resist is added.

13. Next, a mask that describes the desired layout of the metal wires connecting the components of the chip is used. UV-light is shone through this mask. The light hits the photo resist that isn't protected by the mask.

14. In the next step, chemicals are used to remove the photo resist hit by UV-light.

15. Another step of etching removes the metal not protected by photo resist.

Page 50 of 66

Page 51: Programming by zb

16. This leaves a pattern of metal that is the same as the one described by the mask. Now, the chip has a layer of wires that connect its different components.

17. Today, most integrated circuits need more than one layer of wires. Advanced circuits may need up to five different layers of metal to form all the necessary connections. In the last picture we have added another layer of metal to our example. As you can see, a layer of insulating material is put between the two metal layers to prevent the wires from connecting in the wrong places. Of course, to add the second layer we had to go through the same steps as when adding the first layer of metal.

18. When the final layer of connecting metal wires have been added, the chips on the silicon wafer are tested to see if they perform as intended.

19. The chips on the wafer are separated with a diamond saw to form individual integrated circuits.

20. Finally, each chip is packed into the protective casing and subjected to another series of tests. The chip is now finished and ready to be shipped to manufacturers of digital devices around the world

4.5.2. MOSFET Transistors

We will now study how transistors are used as basic building blocks in the digital circuits (of computers). But before this, we will study a new kind of transistor which has replaced our good old junction transistors in practice. This is called a Field Effect Transistor or FET for short. There are different FETs depending on how they are made. The most common FET is a MOSFET (Metal Oxide Field Effect Transistor). A MOSFET is shown below.

Page 51 of 66

Page 52: Programming by zb

As shown below the P-type substrate is connected to negative side hence there is a depletion layer around the substrate and N regions.

The symbol is a variable-battery, i.e. a battery whose strength can be increased or decreased. When sufficient voltage, say greater than VGS which is usually 5V, is passed through this battery, then strong positive charge is deposited on the metal conductor and since substrate is at negative polarity, some of the holes are pushed down towards bottom as shown below. This is because the Metal and the P substrate sandwich an insulator in between and they together form a small capacitor. So, if N-MOSFET turns on after VGC voltage is applied, or else it is turned off. Even if the voltage at source and substrate is less than at gate, this transistor is turned on , but if it is not then this transistor is turned off .

Thus, we can use a MOSFET just like the bipolar junction transistors for a digital switch. Depending on the proper voltage there will be current between source and drain. P substrate sandwiched between two Ns in a MOSFET is known as N-MOSFET, and if N is sandwiched between two Ps, we get a P-MOSFET. In addition, in a P-MOSFET the voltages are reversed and the P-MOSFET will turn on when voltage applied is around 0V and it off when the applied. More precisely, if the voltage at gate is less than source and substrate then P-MOSFET transistor will be turned on , else it will be off .

Page 52 of 66

Page 53: Programming by zb

An N-MOSFET is preferred in transistor production than a P-MOSFET because moving free electrons is easier than moving holes.

4.5.3. Logic GateThe components that we have studied so far, including resistors, capacitors, diode, transistors etc are used to form small circuits each of which acts as a Boolean logical operator. These small circuits are called logic gates and are building blocks of computers. We will study them one by one.

We need to understand a few things before it. The first is that the electrical ground, as shown in the above figure is taken to be the most negative (or least) voltage in a circuit.

In addition, every component in the computer considers some voltage to be 1 (for on), and some voltage less than it to be 0 (for off); also these limits for 0 and 1 may differ in different computer components, but if two components are to interact they both need to settle for the same values of 0 and 1 voltages to communicate. However, inside each of the two components they can individually set different limits for 0 and 1.

If we don’t show whether a positive terminal of a battery is attached to a component or a negative terminal is attached to it, we will show the sign to the voltage applied to denote which end is attached to the component. Thus, +5V applied to a component would mean the 5V is applied to it and the positive terminal is attached to the component.

4.5.3.1. The NOT Gate or InverterNOT gate performs logical NOT operation on the input. That is, if input is A, it calculates ¬A. The not gate we will show will use a CMOS (Complementary Metal Oxide Semiconductor) structure. This structure has the term complementary because we will use two transistors in this structure, each of which is of opposite function. N-MOSFET and P-MOSFET are two complementary transistors as we know. The CMOS structure is shown below along with its symbol and working.

If we consider 0V as our 0 or off, and +2V as our 1 or on (actually, any voltage not 1, i.e. not +2V show be 0/ off for us but here we have taken 0 as off ), then when we input 0V (off) from Vin the gate of P-MOSFET (upper transistor) is at lower voltage than its source and this transistor conducts. Whereas, the gate of N-MOSFET will become at same voltage (not greater than) as of its source, this transistor will not conduct. Hence the current will flow as shown in the second bit of above figure and the output Vout will be +2V which is 1 (or on).

Page 53 of 66

Page 54: Programming by zb

On the other hand if our input voltage is +2V, i.e. 1 or on, then the above transistor will be turned off because its source will be at same voltage as its gate; but the lower transistor will turn on because now its gate will be at higher voltage than its source. The current will pass through as shown in the third bit of the above figure and the output voltage will be that of the ground (0V). Thus Vout will be 0V, i.e. 0 or off.

Hence the above circuitry is able to invert the input voltage at its output voltage. If input voltage is +2V, i.e. 1 or on, then the output will be 0V, i.e. 0 or off. On the other hand, if the input voltage is 0V, is 0 or off, then the output voltage will be +2V, i.e. 1 or on.

This circuitry is called the NOT logic gate or simply NOT gate, and its symbol is shown above. Essentially, NOT gate works as the logical-not operator.

Remember, we could have easy written that if input were off then upper (PMOSFET) transistor would be on , and the lower (NMOSFET) transistor would be off . And if the input were on then the upper (PMOSFET) transistor would be off and the lower (NMOSFET) transistor would be on . And this is how we will explain further, i.e., instead of writing that a transistor conducts or not we will write that it is on or off, and instead of writing +2V (or any positive value) we will write 1 or on and for 0V we will write 0 or off.

4.5.3.2. The OR GateOR gate calculates logical-OR of the input and it can be constructed either from diodes or from transistors as shown below.

As shown it is simple to create OR gate using diodes than transistors. Output will one when any of the diodes is forwards bias (voltage is positive i.e. 1). The logic drawn above using transistors is simple to calculate. The symbol of OR gate is also shown in the figure, and if you notice the input end is slightly curved.

There are too many transistors in the above figure and if you notice carefully the right part of the transistor figure consists of a NOT gate. Thus, we are performing a NOT operation on the left hand part of the figure to get an OR. That is,

¬ (left-part) = OR GATE

This left part is also a gate, and we will learn it later.

4.5.3.3 The AND GateAnd gate is constructed similar to the OR gate. AND gate performs binary logical AND operation. Construction of AND gate using MOSFETs and using diodes is shown below, along with its symbol.

Page 54 of 66

Page 55: Programming by zb

To explain the construction of AND using diodes, consider what happens when any of the diodes is forward biased, i.e. at 0V. In that case the current will go from the forward biased diode(s) and the voltage at output will not be +2V, and thus will be 0 or off. The only way that output is +2V or on is when both the diodes are reversed biased, i.e., at +2V (or on).

Just like the case of OR gate, the case of AND gate using MOSFETS involves a NOT gate as can be noticed. Thus,

¬ (left-part) = AND gate.

And just like the left-part in the OR gate, this left part in the AND gate is also another gate.

4.5.3.4. NOR gateNOR gate is the left-part of the OR gate we had seen earlier. Actually NOR gate is logical NOT of OR gate (i.e. Not-of OR). That is

OR Gate + NOT gate = NOR gate

Just like a NOR gate can be reached by NOT-ing an OR gate, and OR gate (as we have seen earlier) can be reached from NOR gate by NOT-ing the NOR gate. That is,

NOR Gate + NOT Gate = OR Gate

More precisely,

A NOR B = ¬ (A ∨ B)

NOR gate using MOSFETs is shown below along with its symbol. Notice the small round at the end of the NOR gate. This is the part of the NOT gate in it, and the rest of the part comes from the OR gate. NOR gate is a universal gate.

Page 55 of 66

Page 56: Programming by zb

4.5.3.5. NAND GateNAND gate is NOT-of AND Gate, and this is the left-part of the OR gate that we had earlier seen. We get to a NAND Gate by NOT-in an AND-gate as shown in the figure below; also shown is the construction of NAND gate from MOSFETs, along with symbol of NAND gate. Notice the small round at the end of the NAND gate. This is the part of the NOT gate in it, and the rest of the part comes from the AND gate. NAND gate is another universal gate.

The following equations hold for NAND gates

NAND = AND + NOT, i.e., A NAND B = ¬ (A∧B)

AND = NAND + NOT, i.e. A∧B = ¬ (A NAND B) ¬ (¬ (A∧B) ) = ¬¬(A∧B) = (A∧B)

4.5.3.6. XOR GateXOR gate is the logic gate that calculates the exclusive-OR (⊕) of the inputs. It is usually constructed from NAND gate as shown below.

Page 56 of 66

Page 57: Programming by zb

XOR gate has a few important functions in digital circuits. The first on is signal crossing, that is when we have to interchange the values of A and B without losing any value (of A or B) in between. The signal crossing circuit is shown below.

The second use it to perform addition of binary integers. Addition of binary numbers is done by continuously performing addition on two input bits and checking if the carry has resulted, if it has then adding it to the result of next two bit. The addition of bits to get output and a carry forms a half-adder component of digital circuitry and is shown below.

Let’s consider A and B to be the two bits to be added, then the only way we get a 1 in the output bit is if only one of the inputs is a 1. This is the function of XOR gate to give output bit as S. Moreover if both of them are 0s, or only any one of them is 1, then there will not be any carry (i.e., there will be a carry of 0). This is the function of AND gate in the above figure to produce a carry.

The only way there will be a carry of 1 will be when the inputs as 1 and 1. In this case XOR will yield 0 (the output bit) and AND will yield a 1, the carry bit.

Where does this carry bit go? This carry bit is added to the result of next addition of bits. That is, added to the result S of the next half adder to produce another carry. This modified component which adds the carry (from previous) operation to the operation of current half adder is called a full-adder, and is shown below.

Page 57 of 66

Page 58: Programming by zb

Here, A and B are current bits to be added, C in is the carry from previous addition of bits, and Cout is the carry produced from this addition.

The difference between half-adder and full-adder is that the half-adder doesn’t take previous addition’s carry into consideration but full-adder does.

Thus, for the addition of two integers of 8-bits, we will have 8 full-adders connected in series (one after other) with the Cout of the previous full-adder acting as Cin of the current full-adder. Following figure shows full-adders needed to add two 4-bit numbers. It is assumed that the carry C in from previous addition is 0, and that the last carry Cout may be used for further calculations.

Page 58 of 66

Page 59: Programming by zb

5.0. Overview of a Computer SystemFollowing block diagram shows internals of a typical computer system.

Page 59 of 66

Page 60: Programming by zb

Before we study the computer system we should always remember that a computer system doesn’t do anything on its own but has to be directed. This direction is given to the computer system as “a set of” commands ( instruction s) which computer executes one after other. This set of instructions, as we shall see, is called a computer program . An instruction is actually an operation and its accompanying operands.

The figure shows the microprocessor as a central processing unit (CPU), connected to other parts of the system. The CPU is a general purpose microprocessor in that it is used to perform general tasks like mathematical operations, interactions to known types of components, movement of data within the system etc. CPU contains many parts, each performing a specific task; but right now we are concerned with the three shown.

The first such part in the CPU is the Arithmetic and Logic Unit (ALU) which is used to perform binary arithmetic and logical operations.

The second is the cache memory (or instruction cache); a cache is a data buffer that can be accessible at very high speeds (very fast), and a memory is that part of a system which is used to store information. Whenever a CPU is asked to perform a certain instruction on some data, it will have to fetch the instruction from other parts of the computer system where these reside. This may take a fraction of second, but this small time period is enough for a CPU to perform millions of instructions present inside the CPU. To overcome this delay the CPU uses cache memory. At the start of execution of very first instruction the cache is empty, and the computer has to fetch the instruction from the part of system where it resides. The trick is that the CPU, if possible, fetches next few (more than one) instructions into the cache before executing. Now, the next instructions can be fetched from (is present in) the cache. Only if, the instruction is not in the cache the process of fetching the instruction is repeated. One more point to note is that during the execution of a computer program the system may be asked to execute an instruction (or some instructions) which it has already executed. If these earlier executed instructions are already present in the cache, the CPU need not fetch them from outside of it. Cache memory is of very small capacity (because larger caches are very expensive to produce), and a whole program cannot reside in it.

The third is a “limited” set of registers. A register is also a form of computer memory, but it is where the operands (of the instruction) are brought before the operation (of the instruction) is executed. CPU fetches the operands from the cache. The delay in bringing operands from cache to registers is negligible even for the might of CPU. Not all the registers are used to store operands; some of them have specific purpose which we shall detail later. The subset of registers used to store the operands is called general purpose registers.

Not shown here, within the CPU, is the floating-point unit. It is the part of the CPU that performs operations on binary floating-point numbers, and has its own set of registers. As we shall see a floating point number is not involved that often in a computer program, and if it is, it is limited only to a small portion of the program. Note that when we perform a division on integers using a CPU, what we always get is an integer and not a floating-point number, unless specifically asked.

Parts of computer system shown above are sub-circuits of the full circuit constructed on a plastic circuit board, called the motherboard. The CPU is a special (sub-) circuit as it is constructed on a single chip’s surface (constituting an integrated circuit). A CPU is not hard-wired (connected permanently) to the motherboard, instead, it is latched on to a hard-wired base which is connected to the motherboard. In this way, if a CPU gets damaged it may be replaced by another one by simply fitting (latching) it to the base.

Page 60 of 66

Page 61: Programming by zb

Parts of computer systems are connected via wirings (again, constructed on the motherboard), collectively called the system-bus. The system-bus has three sets of wirings each with specific purpose. These three sets are control bus, data bus, and address bus. These buses send signals to the connected subsystems as a series of 1’s and 0’s, i.e., series of high and low voltages. Connected sub-systems may recognize some predefined patterns of high and low voltages, within the series, to mean a control signal (used by control bus), data or instruction (used by data bus), or the location of instruction/data to be fetched (used by address bus).

Also shown in the figure is RAM (for Random Access ‘Memory’), which is a removable part of the motherboard. RAM circuit, also called RAM stick can be connected to the motherboard by placing it in designated connectors (latches) present on the motherboard. The size of total RAM can be increased by connecting either a larger “capacity (say, the number of integers that can be stored in it)” RAM stick, or/and connecting more RAM sticks to the motherboard if more than one RAM connectors are present on the motherboard. RAM is called the main memory because the CPU expects the instructions given to it (i.e., the computer program) to be present in the RAM. RAM stick contains a collection of small chips. RAM, i.e., the set of its chips, can be seen as a string of bits where data can be stored; usually each bit is a capacitor built on the chip. A particular CPU typically sections the RAM into units called words. A word is the fixed-number of bits used to represent any main memory address, i.e., location (of an instruction or data) by the CPU. Most computers today usually have 64-bit word, and near year 2005 had 32-bit word. This is why, in the above figure, the RAM is shown as a stack – words stacked on one another.

RAM is ten times slower than the cache and is obviously outside of the CPU. Hence the instructions are fetched from the RAM (as word-sized chunks) into the cache before the CPU acts on them.

One important point to remember is that some CPU instructions (operation and operands) can fit into a word, whereas others can take more than one word to fit. Another point to remember is that if we were only providing data (and not an instruction) to the CPU then it should be placed either at the start of a word (-chunk) or the next, i.e. at word boundary, but never somewhere in the middle of a word; the reason being the CPU fetches data into cache as word-size chunks and if data were not ‘ aligned’ to the word boundary it may have to fetch one additional word for whole data to be read into cache. The important point is that we can fit more than one data into some multiple of word-size chunk, but if the last data exceeds the last chunk then the CPU will have to make one more fetch to complete the reading of last data. This may induce misalignment of further data as can be imagined.

This alignment is also an issue with instructions, but is intelligently handled by a program called the compiler – about which we will learn later. So, what if our data doesn’t fill multiples of word size? In that case we add (pad) extra useless bytes to our data so that in total it becomes a multiple of word-size. We do introduce some useless space in RAM via padding, but it is ‘affordable’.

The phrase ‘random access’ in RAM is added because we can access information at any location (address) on this medium independently of other locations on it. Complementary to random access memories are sequential access memories like magnetic tapes (like audio cassette tapes) used in magnetic tape drives where we can’t access a location on these media unless we have already accessed previous locations.

CPU executes instructions it recognizes from the RAM one after another, but there are different types of RAMs available and CPU is not built to recognize any specific type. Instead, it takes help of the circuitry present on the motherboard to fetch instructions and data from the RAM. This circuitry

Page 61 of 66

Page 62: Programming by zb

is called a memory controller, and the main display (output) device (which usually is a monitor) is usually connected to it.

In the above figure a video adapter (we will learn about the meaning of adapter later) is connected to the memory controller and the monitor is connected to the video controller. Video adapter is another optional circuitry that can be latched on to the motherboard. Video adapter has its own small capacity RAM-like memory called the video-memory. Also present on the video adapter is a computer chip like the CPU but is usually small in size and limited in functioning; the functioning is limited because of single (particular) purpose of this chip. It is this limited functionality that gives controllers like these a general name “microcontroller”. Since, our CPU doesn’t have a particular purpose it is called, as stated above, a general purpose microprocessor.

As a matter of fact, most of the computer sub-systems contain a microcontroller with a specific purpose and its accompanying memory. This memory is of very small capacity than that of the RAM.

What is the purpose of video-memory? Well, it is the storage used to display video on the monitor. A video is a collected of video-frames, where each frame is a picture. These frames are shown one after another so quickly that it seems we are watching moving images, a video. A frame (picture) can be considered as a matrix (rows and columns) of dots, where each dot can have one of millions of colours, and each dot, i.e. the colour value, can be represented by, say 32-bits (4 bytes). Video memory is used to store these frames and each frame can take many megabytes.

Note: 1 Ki (kilo) byte = 1024 bytes, and 1 M (Mega) byte = 1 Ki x 1 Ki bytes. Similarly, 1 Gi (Giga) byte = 1 Ki x 1 Ki x 1 Ki bytes.

What is an adapter? An adapter is a (sub-) system that helps some other system add to (or improve) its functionality; hence, a video adapter improves the video output functionality of the memory adapter. How?

When a video adapter is not present in the memory the CPU is asked to use a portion of RAM as video-memory. Whereas, the video adapter adds the functionality of the memory controller by providing separate video RAM so that there is a separate and dedicated larger portion (than previously allocated in RAM) of video memory available. In addition, most of the video microcontrollers are now complex chips which can be used to perform complex mathematical operations needed to render (draw shapes) and manipulate images. These video microcontrollers are called Graphical Processing Units (GPUs), and can be given instructions with the help of CPU.

A monitor is an output device, that is, it is used to output information in the form of images. There are other output devices that can be attached to the computer system, e.g., a printer is an output device which publishes the information on a paper, and a speaker is an output device that converts a series of high and low voltage values to sound (-waves).

Complementary to output devices are the input devices, which feed information into the system. A keyboard and a mouse are examples of input devices. We use a keyboard to type and give instructions to the computer system, whereas, functioning of mouse is a bit complex and requires further explanation of input/output sub-system.

Some devices act as both input and output devices, e.g., a network router used to connect one computer to another to share information between the two.

All of the input-output devices have their microcontrollers connected to the input-output sub-system and some of these microcontrollers are connected to the input-output sub-system via adapters.

Page 62 of 66

Page 63: Programming by zb

Whenever an input is to be given to a CPU the microcontroller of the input device sends a voltage signal to a dedicated part of Input-output controller called the interrupt controller. Each sub-system is given one connection line, i.e. circuit wiring, (labelled as IRQs) to the interrupt controller, out of many connection lines present in the interrupt controller. IRQ stands for Interrupt Request, and when certain device sends signal to the interrupt controller the interrupt controller in turn sends the interrupt signal (labelled INTR in the figure) along with IRQ number which generated the signal. This interrupt signal to a processor is called a hardware interrupt, and the processor from the connection line number understands which type of input device is trying to interrupt it.

The processor completes the current instruction it is executing and then checks for the interrupt. If it has been interrupted, i.e., the INTR signal line is high (in voltage), it fetches the IRQ number and interacts with the input device that cause the interrupt; otherwise it executes the next instruction.

For example, when we press a key in a keyboard, the otherwise left open circuit below the key gets closed (completed); that is, its keys act like switches in a circuit and the keyboard microcontroller comes to know which key was pressed by knowing which portion of circuit got closed (completed). The microcontroller sends an IRQ to the CPU via an INTR and when the CPU is ready to grant (acknowledge) keyboard controller’s wish it sends an acknowledgement via the control bus. The keyboard can now send the code (integer value) of the key (or a combination of keys) that was pressed to the CPU via data bus. CPU reads the key code, performs come operation, e.g. displays ‘A’ on the monitor and goes back to the next instruction which was to be executed when it got suspended due to an interrupt.

Similarly, the mouse controller sends information about which button was pressed (which circuit go completed) as button code (an integer) to the CPU; and if the mouse is moved a certain distance, it sends this information to the CPU as how much the mouse moved up/down and how much to left/right to its original position. CPU takes appropriate action by moving the mouse pointer on the screen or performing a command when the mouse button is pressed.

There are some hardware interrupts that can be blocked for a while. This process of momentarily blocking of an interrupt is called masking (hiding) of that interrupt and such an interrupt is called a maskable interrupt. Interrupts which cannot be masked (blocked) are called non-maskable interrupts.

Also seen in the diagram is a clock. This is a circuitry that sends an interrupt to the CPU every some billionth fraction of a second. In other words, it sends around 109 interrupts to the CPU per second. Clock interrupt is a non-maskable interrupt, and is used by the CPU to synchronize its operations, i.e. execute instructions. This is what generally called processor speed, i.e. how many times a CPU is interrupted by its clock. Note that, number of times some event occurs is called its frequency of occurrence.

All the memories that we have detailed until now are volatile (volatile memories) in nature; that is, when the computer (power) is turned off the data stored inside these memories gets lost. When the computer starts again the data is usually string of all 0’s. What is we would like to store our data for future use? These memories will be of no help to us in this regard. There are other memories that are non-volatile in nature (non-volatile memories) which store data even when the power is turned off.

One of these non-volatile memories is the hard-disk, which contains platters (disks) stacked one above another but not touching each other, held together by a central spindle. Each of these platters is coated with a magnetic material and can rotate when the central spindle rotates. Magnetic material on the platter contains millions of small magnetic dipoles (magnets with two poles – north

Page 63 of 66

Page 64: Programming by zb

and south) arranged along the circumference of concentric circles (called tracks). These dipoles are initially arranged in one direction, say, if we move clockwise on the circumference of the one such circular arrangement of dipoles, we will encounter north of each dipole first and then its south. Very close to each platter are two metallic sticks one above and one below it; and these sticks are connected to a cylindrical arm (like spindle) which helps them move almost radially (to go towards or away from the centre of the circle along its radius) to the platters. At the end near the platter, each stick has a tiny region called a read-write-head which contains a tiny coil (wire winding). We should know that electricity can generate magnetism (magnetic ability) and vice-versa. For example, if we take an iron rod, wind it thoroughly with a wire and then connect the two ends of the wire to a battery we will produce an electromagnet, i.e., a magnet resulting from electricity. The iron rod would be a magnet until the current is passed along the wire. Similarly, if we move (in and out) a magnet inside of the spiral winding of a wire, that wire would conduct electricity. This principle of electromagnetism is used with the hard-disks. For a read, a coil (on the head) is placed just above the track and when the platter is spun as high-speeds, the direction of current would change if the direction of magnetic dipole changes and every change in a direction is read as a 1 by the circuitry and every no-change is interpreted as a 0. Similarly, during writing data on the hard-disk a strong current is passed in the coil of the head which can change direction of a dipole under it to either make it 0 (original dipole direction) or 1 for later reading.

When the power is turned off, the dipoles don’t lose their direction, and hence the data is not lost. Hard-disks are long lasting but extremely slow in the speed of access of data, but are very cheap compared to RAM and cache. Thus, a hard-disk is used as the secondary memory of the computer system. The data is fetched, “in bulk”, from the hard-disk (via its controller) into the RAM and then executed as explained earlier. There can be more than one hard-disk connected in the computer circuitry.

Page 64 of 66

Page 65: Programming by zb

A single dipole change and no-change (i.e., 1 bit) is not fetched by the CPU into the RAM, rather a predetermined number of bits, typically 512 bytes or 4 KiB. Each such predetermined number is called a track-sector.

Hard-disks are heavy to carry, what if we were to devise a similar technology but memory could be light weight and much cheaper, also we could detach and attach it to the circuitry whenever we need? A Compact Disc (CD) is one such technology. There are CDs which can only be written into once, called CDROMs (Compact Disc Read-Only Memory), and others that can be erased and read called writeable-CDs. Writing to a CD is called burning that CD. A CD can be inserted into a CD-drive, but instead of spinning platters a single CD spins in this drive; also, instead of a head a laser is used to read and write. The laser is used to make minute pits into the surface of the CD tracks; it is this string of tiny pits and plane regions that constitutes 1s and 0s, instead of using directions of magnetic dipoles (not present on the CD). A laser of slightly weaker strength is used to read the pits by using mechanism of reflection from the pits. When a strong light is reflected back it is due to laser striking a plane region (closer to the laser) and when a weaker light is reflected back it is due to the light striking a pit (slightly farther to the laser). The CD drive contains CDROM microcontroller inbuilt.

CDROM and any other such memories which can be attached and detached whenever needed are called removable memories.

Also seen in the above diagram are two network controllers, one of which is a wireless (wifi – for wireless fidelity) network controller. A network controller is a component that connects our computer to another computer so as to interact with it by sending and receiving information. Wired network controller uses a wire to connect two computers and transfers information using that wire as the medium. Whereas, wifi controller transmits information by converting digital voltages into radio waves which travel through air/space and can be received by receiver’s wifi controller. If we want our computer to be connected to many and not just one neighbouring computer, either using wire as a medium or wifi, we take help of a small mini-computer whose purpose is to transmit information from sender to one of connected computers, i.e., the receiver. This small intelligent machine whose only purpose is to understand network information sent and received and to channel it is called a network-router. All the computers wishing to exchange information in the computer-network (collection of interacting computers and components like router) can connect to

Page 65 of 66

Page 66: Programming by zb

the router; in fact, a router can connect to another router, which in turn can be connected to other computers or routers and so on.

The sub-system marked Programmable ROM (Read Only Memory), is an integrated circuit (a chip) that acts like a writeable-CD. The information written onto it is used to start the computer. Information written onto it remains for a long time and can only be changed by programming it electrically, i.e. by passing current. A ROM comes pre-programmed from the firm which produces it, it is because of this it is called a firmware. It is easy now-a-days to program a ROM, we can just write a program (set of instruction) to make desired changes and tell the CPU can do the needful. This is called flashing the ROM. The information written onto the ROM is thus a computer program, and is called the BIOS – for Basic Input Output System. The BIOS acts like the right hand of the processor and instructs the processor at the start of the computer. As soon as the computer starts, our microprocessor loads ROM BIOS at a predefined address in the RAM and starts executing its instructions.

Page 66 of 66