Sunday, May 19, 2013

Binary Number System




DenaryBinaryHexadecimal
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F
160001 0000G

Denary/Decimal 
Denary is the number system that you have most probably grown up with. It is also another way of saying base-10. This means that there are 10 different numbers that you can use for each digit, namely:

0,1,2,3,4,5,6,7,8,9
Notice that if we wish to say 'ten', we use two of the numbers from the above digits, 1 and 0.
ThousandsHundredsTensUnits
10^310^210^110^0
1000100101
5973
Using the above table we can see that each column has a different value assigned to it. And if we know the column values we can know the number, this will be very useful when we start looking at other base systems. Obviously, the number above is: five-thousands, nine-hundreds, seven-tens and three-units.
5*1000 + 9*100 + 7*10 + 3*1 = 597310

Binary 
You should know denary pretty well by your age, but there are different base systems out there, and the most important one for computing is the binary base system. Binary is a base-2 number system, this means that there are two numbers that you can write for each digit:

0, 1
With these two numbers we should be able to write (or make an approximation) of all the numbers that we could write in denary.
One-hundred and twenty-eightsSixty-foursThirty-twosSixteensEightsFoursTwosUnits
2^72^62^52^42^32^22^12^0
1286432168421
01101010
Using the above table we can see that each column has a value assigned to it that is the power of two (the base number!), and if we take those values and the corresponding digits we can work out the value of the number: 1*64 + 1*32 + 1*8 + 1*2 = 106.
If you are asked to work out the value of a binary number, the best place to start is by labelling each column with its corresponding value and adding together all the columns that hold a 1. Let's take a look at another example:
000111112
1286432168421
00011111
So now all we need to do is to add the columns containing 1s together: 1*16 + 1*8 + 1*4 + 1*2 + 1*1 = 31


Convert the following binary numbers into denary
000011002
  
Answer :
1286432168421
00001100
8+4 = 1210
010110012
 
Answer :
1286432168421
01011001
64 + 16 + 8 + 1 = 8910
000001112
  
Answer :
1286432168421
00000111
4 + 2 + 1 = 710
010101012
 
Answer :
1286432168421
01010101
64 + 16 + 4 + 1 = 8510
How do we tell if a binary number is odd?
  
Answer :
It's right most digit is a one
Is there a short cut to working out a binary number that is made of solid ones, such as: 011111112
  
Answer :
Yes, take the first 0's column value and minus one
1286432168421
01111111
= 128 - 1 = 127 = 64 + 32 + 16 + 8 + 4 + 2 + 1
000011112 = 16 - 1 = 15 = 8 + 4 + 2 + 1
000001112 = 8 - 1 = 7 = 4 + 2 + 1
If we were to use octal, a base 8 number system, list the different numbers each digit could take:
  
Answer :
0, 1, 2, 3, 4, 5, 6, 7


Bit patterns in a computer



The language that a computer understands is very simple, so simple that it only has 2 different letters 1 and 0. This is called Binary. Everything you see on a computer, images, sounds, games, text, videos, spreadsheets, websites etc. Whatever it is, it will be stored as a string of ones and zeroes.
monochrome image of a smiley face
Bit - a standard unit to measure computer memory, consisting of of a value that is either 1 or 0

Byte - a standard unit to measure computer memory, consisting of 8 bits. e.g. 10101011

Exercise: Bit patterns in a Computer
How do computers store data?
 [Collapse] 
Answer :
as binary values, using a pattern of 1s and 0s
What sort of data can be stored in binary?
 [Collapse] 
Answer :
  • Video
  • Sound
  • Picture
  • Text
  • Code
  • Spreadsheet
  • Game
  • etc
What does the following binary string represent: 10011100
 [Collapse] 
Answer :
This could be anything:
  • sound data
  • picture data
  • text ASCII for œ
  • unsigned integer = 156
  • video data
  • etc
How many bits in a byte?
 [Collapse] 
Answer :
8
How many bits in a 7 bytes?
 [Collapse] 
Answer :
7 * 8 = 56

Pseudocode


Pseudocode - informal high-level description of a computer program or other algorithm, intended for human reading rather than machine reading.

Pseudocode uses a combination of programming terminology and plain English to describe algorithms in a form that is easier for people to understand than conventional programming language code. There are no standards for pseudocode and a program in pseudocode is not an executable program. Psuedocode is used in textbooks and scientific publications to describe various algorithms, and also in planning of computer program development, for sketching out the structure of the program before the actual coding takes place. It typically leaves out details that are not essential for human understanding of the algorithm, such as variable declarations, system-specific code and some subroutines.
Example: Pseudocode
Below is an example of a pseudocode algorithm
For i = 1 to 100
    set print_number to true
    if i mod 3 equals 0
        print "Bizz" and set print_number to false
    if i mod 5 equals 0
        print "Buzz" and set print_number to false
    if print_number is true, print i
    print a newline
Converting this into VB.NET, we get:
dim print_number as boolean
For i = 1 to 100
    print_number = true
    if i mod 3 = 0 then
       console.writeline("Bizz")
       print_number = false
    end if
    if i mod 5 = 0 then
       console.writeline("Buzz")
       print_number = false
    end if
    if print_number then
       console.writeline(i)
    end if
    console.writeline()

[edit]Structured English

Structured English - A restricted part of the English language used to describe algorithms

Structured English is very similar to Pseudo code, but it tends not to use so many mathematical symbols. Often people start with Structured English, convert it to Pseudo Code and then write Executable Code. Examples of common keywords: START, BEGIN, END, STOP, DO, WHILE, DO WHILE, FOR, UNTIL, DO UNTIL, REPEAT, END WHILE, END UNTIL, END REPEAT, IF THEN, IF, ELSE, IF ELSE, END IF, THEN, ELSE THEN, ELSE IF, SO, CASE, EQUAL, LT, LE, GT, GE, NOT, TRUE, FALSE, AND, OR, XOR, GET, WRITE, PUT, UPDATE, CLOSE, OPEN, CREATE, DELETE, EXIT, FILE, READ, EOF, EOT, WITH, RETURN
Structured EnglishPseudo CodeExecutable Code
BEGIN 
 READ name
 IF name EQUAL "Harry" THEN
  WRITE "Why don't you marry Pippa?"
 ELSE
  WRITE "Are you Royal enough?"
 END IF
END
BEGIN 
 INPUT name
 IF name == "Harry" THEN
  OUTPUT "Why don't you marry Pippa?"
 ELSE
  OUTPUT "Are you Royal enough?"
 END IF
END
dim name as string
name = console.readline()
if name = "Harry" then
  console.writeline("Why don't you marry Pippa?")
else
  console.writeline("Are you Royal enough?")
End if
Exercise: Pseudocode
What are the rules when writing pseudocode?
 [Expand] 
Answer :
What is the difference between pseudocode and a programming language such as javascript?
 [Expand] 
Answer :
For the following pseudocode write a VB.NET equivalent:
for x from 1 to 7 inclusive
  add x to total
loop
print total
 [Expand] 
Answer :
Write pseudocode for the following problem:
Find the average of 4 numbers and display it
 [Collapse] 
Answer :
There are many ways to answer this correctly as long as it is clear you got it right. Ask your friend to check it.
input 4 numbers
sum = add numbers together
avg = sum / 4
print avg

Trace Tables


Trace table - a technique used to test algorithms to make sure that no logical errors occur

Hand tracing or 'dry running' allows you to use a trace table to
  • see what code will do before you have to run it
  • find where errors in your code are
Taking a program like the one below we need to keep track (trace) all the variables and outputs.
Dim y as integer = 3
For x = 1 to 4
        y = y + x
Loop
Console.writeline(y)
To do this we create a trace table:
xyoutput
13
24
36
49
41313
The exam will normally ask you to create a trace table of some sort so you need to be very confident with them. The exam will usually give you the headings but just in case, there are several steps in making a trace table, the first one is to note the table headings, this involves the following:
  1. VARIABLES: note all the variables in the piece of code you are looking at (this includes arrays). Note each variable as a heading
  2. OUTPUTS: note if there is an output and put this as a heading
  3. INPUTS: if there are inputs specified, put an inputs column and be prepared to fill it in.
It is very easy to jump right in when filling in trace tables, but you must be careful. The exam will try and trick you, so trying to predict what a trace table will do it not a good idea. In fact, the best idea is to switch your brain off and tackle the problem line by line, exactly as a computer would. Take a look at the following example:

Algorithm Design


Algorithm - a set of instructions independent of any programming language that calculates a function or solves a problem.

Express the solution to a simple problem as an algorithm using flowcharts, pseudo-code or structured English and the standard constructs:

performing or operating each step consecutively in the order they arise
Console.writeline("This is line 1")
Console.writeline("This is line 2")
Console.writeline("This is line 3")

Assignment [edit]

Assigning a value to a variable
Dim x, y, sum as integer
x = 5
y = 15
sum = x + y

Selection [edit]

Selection is choosing a step
If-Then-Else-diagram.svg

If x > 0 then
   Console.writeline("x is positive")
End If
If x = 0 then
   Console.writeline("x equals 0")
End If
If x < 0 then
   Console.writeline("x is negative")
End If

Repetition [edit]

A sequence of steps that loop until a requirement is met
Do-while-loop-diagram.svg

While-loop-diagram.svg

CPT-For-loop-diagram-complex.svg
x = 0
y = 5
Do
   x = x + 1
Loop Until x = y

wikiBooks

Finite state machine




Example of a simple finite state machine

p = start state
a = transition
q = accept state
A finite state machine consists of states, inputs and outputs. The number of states is fixed; when an input is executed, the state is changed and an output is possibly produced. Finite state machines are widely used when designing computer programs, but also have their uses in engineering, biology, linguistics and other sciences thanks to their ability to recognise sequences.
A finite state machine expressed visually is a State transition diagram. It is used to show all the states, inputs and outputs. Each state is represented with a circle, and each transition with an arrow. Transitions are labelled with an input that causes a transition and possibly an output that results from it. A double circle signifies the accept state. Not all FSMs will have an accept states and it is possible they could run for ever
A finite state machine can be with or without outputs:

Finite state automaton \

Looking at the above diagram we can see that it starts in state S1, an input of 1 will keep it in state one, and an input of 0 will move it to state S2. Once in S2 an input of 1 will keep it there, and an input of 0 will switch it back to S1. This means that the following inputs are valid:

110011001
001110011
It might appear to accept any binary value, but this isn't true. The only state it can accept in is state S1. This places the following rule on all accepted inputs: "A combination of binary digits involving an even number of zeros". This is useful for parity checks. If I try the following:
110011011
I am stuck in state S2 and the FSM has not accepted. Can you create a FSM to only accept Binary numbers with odd numbers of 1s?

Decision tables


Decision tables are compact and precise ways of modelling complicated logic, such as that which you might use in a computer program. They do this by mapping the different states of a program to an action that a program should perform. Decision tables take on the following format:
The four quadrants
ConditionsCondition alternatives
ActionsAction entries
The limited-entry decision table is the simplest to describe. The condition alternatives are simple Boolean values, and the action entries are check-marks, representing which of the actions in a given column are to be performed.
A technical support company writes a decision table to diagnose printer problems based upon symptoms described to them over the phone from their clients. They type the following data into the advice program:
  1. Printer does print (N)
  2. Red light is flashing (Y)
  3. Printer is recognised (N)
The program then uses the decision table to find the correct actions to perform, namely that of Check / Replace ink.
Printer troubleshooter
Rules
ConditionsPrinter does not printYYYYNNNN
A red light is flashingYYNNYYNN
Printer is unrecognisedYNYNYNYN
ActionsCheck the power cable  X     
Check the printer-computer cableX X     
Ensure printer software is installedX X X X 
Check/replace inkXX  XX  
Check for paper jam X X