how many distinct values can be stored in 7 bits?

59
How many distinct values can be stored in 7 bits? A. 7 B. 8 C. 128 D. 127 E. –42

Upload: others

Post on 24-Nov-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: How many distinct values can be stored in 7 bits?

How many distinct values can bestored in 7 bits?A. 7B. 8C. 128D. 127E. –42

Page 2: How many distinct values can be stored in 7 bits?

Welcome toCOMP1100 and COMP1130

Semester 1, 2018

Page 3: How many distinct values can be stored in 7 bits?

ProgrammingComputing a mean

Page 4: How many distinct values can be stored in 7 bits?

Memory Address Machine Code (hex) Assembler Instructions (Poorly coded) C program

0x100000e9b0x100000ea2

0x100000eb90x100000ebc0x100000ebf0x100000ec50x100000ec90x100000ecd0x100000ed00x100000ed30x100000ed60x100000ed90x100000ede0x100000ee1

0x100000ee60x100000ee90x100000eea

c7 45 f0 00 00 00 00c7 45 ec 00 00 00 00

8b 45 ec3b 45 f40f 8d 21 00 00 0048 63 45 ec48 8b 4d f88b 14 8103 55 f089 55 f08b 45 ec05 01 00 00 0089 45 ece9 d3 ff ff ff

8b 45 f099f7 7d f4

movl $0x0, -0x10(%rbp)movl $0x0, -0x14(%rbp)

movl -0x14(%rbp), %eaxcmpl -0xc(%rbp), %eaxjge 0x100000ee6movslq -0x14(%rbp), %raxmovq -0x8(%rbp), %rcxmovl (%rcx,%rax,4), %edxaddl -0x10(%rbp), %edxmovl %edx, -0x10(%rbp)movl -0x14(%rbp), %eaxaddl $0x1, %eaxmovl %eax, -0x14(%rbp)jmp 0x100000eb9

movl -0x10(%rbp), %eaxcltdidivl -0xc(%rbp)

int sum = 0;int i = 0;

loop:if (i >= length(values))

goto done;sum += values[i];

i = i + 1;

goto loop;done:mean = sum / i;

Page 5: How many distinct values can be stored in 7 bits?

Machine level programming

Not portable: depends on specific CPU model Error proneHard to handle complex problems Machine dictates the language in which the programmer thinksFull controlNeed more problem-oriented languages.Need more abstract and safer languages.Abstraction versus control is a tradeoff!

Page 6: How many distinct values can be stored in 7 bits?

A brief history of programming languages

Inspired by machine code (imperative, macro assemblers)Fortran (‘57), Cobol (‘59), Basic (‘64), C(‘71)

Based on formal model of computation (lambda calculus, functional)Lisp (‘58), ML (‘73)

Structured and strongly typed (imperative)Algol (58), Algol 60, Algol 68, Pascal (‘70)

Based on message-passing simulation models (object-oriented)Simula (‘67), Smalltalk (‘69)

Based on first-order logic (declarative)Prolog (‘72)

Page 7: How many distinct values can be stored in 7 bits?

Programming paradigms

Control flow: imperative/declarativeDeclarative: functional/logic/finite state machinesAllocations and bindings: static/dynamicTime: event-driven/discrete/synchronous/continuousFocus: control flow/data flowConcurrency: sequential/concurrent/distributedStructure: modular/generics/templates/objects/aspects/agentsDeterminism: deterministic/non-deterministicYou don’t need to understand all of these; just many ways to program

Page 8: How many distinct values can be stored in 7 bits?

Influential languages (≠popularity!)

Conceptual foundationsλ-calculus/Lisp, Simula/Smalltalk, Algol, Prolog

Also influentialML/Haskell, Pascal, Eiffel, Ada, Modula-3, C, Java

Check out http://exploringdata.github.io/vis/programming-languages-influence-network/

e.g., PHP widely used, but not particularly influential…

Page 9: How many distinct values can be stored in 7 bits?
Page 10: How many distinct values can be stored in 7 bits?

How is software written and executed

Source high-level programWrite in text editor or IDE (Integrated Development Environment)

Translation and interpretation• Compiler: program that translates source program into machine instructions• Interpreter: program that interprets (executes) a source program

IDEs allow editing, compiling, executing, and debugging from one environment ⇒ rapid development cycle

Page 11: How many distinct values can be stored in 7 bits?

Program in high levellanguage, such as C

Compiler

Equivalent program in machine language

(a) (b) (c)

#include <stdio.h>

int main(){ println(”Hello!”); return 0;}

C

0101010101010010101010110101100110101011101010101010101010101111

x86

Program in high levellanguage, such as Python

Interpreter

Executes program directly on CPU

print “Hello!”

Python

AMDIntel

Program in high levellanguage, such as Java

Compiler

Equivalent program in machine language

public class Hello { public static void main (String[] args){ System.out. println (”Hello!”); }}

Java

0101010101010010101010110101100110101011101010101010101010101111

x86

1010101101011001010101010101110010101000001010101110101010101100

BytecodeJIT

Compiler

Program in platformindependent

bytecode

Page 12: How many distinct values can be stored in 7 bits?

Lots of programs work reliably

Most flight computersCar braking controllersBanking systemsHigh speed trains, subway systemsInternet search enginesProfessional A/V equiment…Programming delivers trusted systems that even our lives depend on

Page 13: How many distinct values can be stored in 7 bits?

What makes a professional programmer?

“Fluent” in all essential programming constructs and paradigmsCan choose and use the right tools for the jobConsiders capabilities of available hardwareUnderstands translation into executable machine code and how to control that translationUnderstands testing and verification and uses them adequatelyFinds best suited abstractions and modularisations (experience!)Knows how to analyse unexpected problems (debugging)

Page 14: How many distinct values can be stored in 7 bits?

Functional ProgrammingComputing a mean

Page 15: How many distinct values can be stored in 7 bits?

Haskell

A lazy, functional programming language (based on the λ-calculus).Statically typed with polymorphism and operator overloading.Uses monads to provide side-effects and imperative sequencing.Pragmatically

• Compiler + interactive interpreter• Concise, compact syntax• Compiler detects many (most) errors (but error messages may be confusing)• Performs well (though not as “fast” as C, C++, or even Java)• Commonly used for verifiable systems, high-integrity systems• Not directly useful for real-time or high-performance computation

Page 16: How many distinct values can be stored in 7 bits?

Functional

Functions are first-class: functions are values that can be used in exactly the same ways as any other sort of value.The meaning of Haskell programs is centred around evaluating expressions rather than executing instructions.The result is a different way of thinking about programming.

Page 17: How many distinct values can be stored in 7 bits?

What is a function?

A recipe for generating outputs from inputs• ‟multiply a number by itself”A set of (input, output) pairs:• (1,1) (2,4) (3,9) (4,16) (5,25)An equation:

f x = x2

A graph relating inputs to output (for numbers only)

-2 -1 0 1 2

1

2

Page 18: How many distinct values can be stored in 7 bits?

Functions and states

y = f(x1,x2,…,xn)

x1

x2

xn

InputOutput

A repeatable (deterministic) function: fully described by its input-output relation.

This function is in exactly the same “state” every time it is called.

Page 19: How many distinct values can be stored in 7 bits?

Functions and states

y = f(x1,x2,…,xn)

x1

x2

xn

InputOutput

Limiting observability:• After the function receives its inputs it is oblivious to the outside world.

Page 20: How many distinct values can be stored in 7 bits?

Functions and states

y = f(x1,x2,…,xn)

x1

x2

xn

InputOutput

Limiting observability:• When the function produces its output it reveals nothing else.

Page 21: How many distinct values can be stored in 7 bits?

Functions and states

Limiting observability:• There can be no other communication with the outside world.

y = f(x1,x2,…,xn)

x1

x2

xn

InputOutput

Page 22: How many distinct values can be stored in 7 bits?

Functions and states

Limiting observability:• The function cannot hold values internally after the function has completed

y = f(x1,x2,…,xn)

x1

x2

xn

InputOutput❌

Page 23: How many distinct values can be stored in 7 bits?

Functions and states

Limiting observability:• The function cannot write to its input values

y = f(x1,x2,…,xn)

x1

x2

xn

InputOutput

Page 24: How many distinct values can be stored in 7 bits?

Functions and states

y = f(x1,x2,…,xn)

x1

x2

xn

InputOutput

The result is a side-effect free function!

Easy to understand and verify.Can be designed in isolation, supporting modularity.

Page 25: How many distinct values can be stored in 7 bits?

Pure

Haskell expressions are always referentially transparent:• no mutations; everything is immutable• expressions are side-effect free• calling the same function with the same arguments results in the same

output; programs are deterministicBenefits:• equational reasoning and refactoring: “replace equals by equals” (algebra)• parallelism: evaluating in parallel is easy when no side effects• fewer headaches: easier to debug, maintain, and reason about programs

Page 26: How many distinct values can be stored in 7 bits?
Page 27: How many distinct values can be stored in 7 bits?

Lazy

Expressions are not evaluated until their results are needed.• It is easy to define a new control structure just by defining a function.• It is possible to define and work with infinite data structures.• It enables a more compositional programming.• BUT it makes reasoning about time and space usage more difficult!

Page 28: How many distinct values can be stored in 7 bits?

Kinds of data

Integers: 42, -69Floats: 3.14Characters: ‘h’Strings: “hello”Booleans: True, FalsePictures: ♞

Page 29: How many distinct values can be stored in 7 bits?

Applying a function

invert :: Picture -> Pictureknight :: Picture

invert knight

♞ ♘invert

Page 30: How many distinct values can be stored in 7 bits?

Composing functions

beside :: Picture -> Picture -> PictureflipV :: Picture –> Pictureinvert :: Picture -> Pictureknight :: Picture

beside (invert knight) (flipV knight)

♞ ♘invert

flipVbeside

���

Page 31: How many distinct values can be stored in 7 bits?

double

Defining a new function

double :: Picture -> Picturedouble p = beside (invert p) (flipV p)

♞ ♘invert

flipVbeside

���

Page 32: How many distinct values can be stored in 7 bits?

Defining a new function

double :: Picture -> Picturedouble p = beside (invert p) (flipV p)

♞ double ��

Page 33: How many distinct values can be stored in 7 bits?

Terminology

Type signaturedouble :: Picture -> Picture

Function declarationdouble p = beside (invert p) (flipV p)

function name function body

Page 34: How many distinct values can be stored in 7 bits?

Terminology

double p = beside (invert p) (flipV p)

formal parameter

function definition

double knight

actual parameter

expression

Page 35: How many distinct values can be stored in 7 bits?

Types

The meaning of a program will depend on the types of the values it manipulates:• Even though values are just represented as bits, the operations on

those bits will interpret them in some wayFor example, consider the addition operator +:

It takes two number values and combines them to give a new number.To add two integers it must interpret the bits of those values as integers and use integer addition to produce the result.Integer addition applied to non-integers would make no sense.

Page 36: How many distinct values can be stored in 7 bits?

Applying a function

scale:: Picture -> Integer -> Pictureknight :: Picture

scale knight 2

♞ ♞scale

2

Page 37: How many distinct values can be stored in 7 bits?

Applying a function

scale:: Picture -> Integer -> Pictureknight :: Picture

scale knight knight

♞scale♞

Page 38: How many distinct values can be stored in 7 bits?

Types

A type is a collection of values that are the same sort of thing:e.g., numbers or pictures (and even functions!)

In a strongly-typed programming language you cannot use values and the functions that manipulate them inconsistently.Makes programs type-safe.This can be enforced statically (compile time) or dynamically (run time).Static typing helps clarify thinking and express program structure, serves as documentation, and turns run-time errors into compile-time errors.

Page 39: How many distinct values can be stored in 7 bits?

Static typing

Helps clarify thinking and express program structure.Serves as a form of documentation.Turns run-time errors into compile-time errors.

Page 40: How many distinct values can be stored in 7 bits?

Examples

Int• Set of values, subset of the integers, fixed precision• Stored in some fixed number of bits• [Live coding experiments to find out range using minBound/maxBound]

Integer• Set of values, all the integers, arbitrary precision

(up to some number of bits available in memory)• [Live coding experiment to see behaviour]

Page 41: How many distinct values can be stored in 7 bits?

Programming: craft and correctness

A human activity (“the art of programming”)• A program can be well-crafted and aesthetically pleasing• A program can be unmaintainable or unreadable

Programs are formal artefacts• An expression in (temporal) logic• Can be correct/incorrect (against a specification)

Page 42: How many distinct values can be stored in 7 bits?

Obfuscated codeThe International Obfuscated C Code Contest

http://www.ioccc.org/2014/endoh2/prog.c

#include <stdio.h>#define TA q=/*XYXY*/#define/*X YXY*/CG r=void p(int n,int c){;for(;n--;) putchar(c)#define Y( z)d;d=c++\%2<1?x=x*4 +z,c%8>5?\x=x?p(1,x), 0:x:0:0;d=#define/*X YX*/C Y(1)#define/*X YX*/G Y(2);}int(*f)( void),d,c,#define/*X YX*/A Y(0)#define/*XY*/AT int\

m(void/**/){d=#define/*XYX*/T Y(3)

#define GC d; return\0;}int(*f) (void )=m;

x,q,r; int main(){if(f)f();else {for(puts("#include" "\40\"pro\g.c\"\n\n \101T"+0);d=!d?x=(x= getchar())<0?0:x,8*8 :d,TA++c%8,TA(1+7*q- q*q)/3,r=c*15-c*c-36 ,p(r<0?!q+4:r/6+!q+4 ,32),q||x;c%=16)q?p( 1,"ACGT"[x/d&3]),p(q ,126),p(1,"TGCA"[x/d &3]),d/=4,p(001,10): puts(c%8?\"CG":"TA") ;puts("GC");}return 0;}/**/

Page 43: How many distinct values can be stored in 7 bits?

Programming myths

It’s easy, everybody can do it with just a few tutorialsEspecially sales people for the latest trendy language

It’s intrinsically hardWhat old programmers say to keep their jobs

A weird way to spend your dayHollyword nerd stereotypes

In realityA specialized, historically recent, technological, professional activity, which requires care and focus (just like any advanced profession)

Page 44: How many distinct values can be stored in 7 bits?

Programming truths

Some things will never work …• Decision problems

is a program syntactically correct? Is x a prime number? Will a program stop for a given input?

The last one is the halting problem, which is representative of a class of inherently undecideable problems

Page 45: How many distinct values can be stored in 7 bits?

Undecideable problems

Kurt Gödel (31): loosely, “any consistent theory can express true statements which cannot be proven in this theory”Alan Turing (’37): “no program can decide whether another arbitrary program will terminate on a given input”Henry Gordon Rice (’53): “for any non-trivial property of partial functions, no general and effective method can decide whether an algorithm computes a partial function with that property”

Page 46: How many distinct values can be stored in 7 bits?

Function composition operator

flipV :: Picture –> PictureflipH :: Picture -> Picturerotate :: Picture -> Picturerotate = flipH . flipVknight :: Picture

Rotate knight

♞ flipV � flipV

rotate

Page 47: How many distinct values can be stored in 7 bits?

Expressions and evaluation (reduction)

Evaluating any Haskell expression works much like a calculator:

42.0 → 42.0

3 + 4 / (1234 – 1)→ 3 + 1 / 1233→ 3 + 3.2441200324412004e-3→ 3.0032441200324413

Page 48: How many distinct values can be stored in 7 bits?

Expressions and evaluation (reduction)

(3 + 4) * (5 – 2)→ 7 * (5 – 2)→ 7 * 3→ 21

Or alternatively:(3 + 4) * (5 – 2)→ (3 + 4) * 3→ 7 * 3→ 21

Can even compute 3+4 and 5-2 concurrently (in parallel)!

Page 49: How many distinct values can be stored in 7 bits?

Expressions and evaluation (reduction)

double :: Integer –> Integerdouble n = 2 * n

double (3 + 1) → double 4→ 2 * 4→ 8

Page 50: How many distinct values can be stored in 7 bits?

Types

All expressions have types::type 42.042.0 :: Fractional a => a

Types can be from a class of types, here a type derived from the class of Fractional types (i.e., real numbers or fractions).

:type (3 + 4) / (5 - 2)(3 + 4) * (5 - 2) :: Num a => a

Here Num is a class of numeric types (i.e., numbers).

Page 51: How many distinct values can be stored in 7 bits?

The essence of functional programming

Types model objects in the problem domain.Programming means writing functions over types.Computing with functions means evaluation (reduction).Haskell variables name values and cannot vary.Functions have no side-effects.

Page 52: How many distinct values can be stored in 7 bits?

The essence of Haskell programming

Programs are higher level: define relationship between input and output (the what) rather than how to compute a result.First class functions: can be passed around like any other data.Functions have no side-effects: monads embed side-effects inside Haskell and its type system.Haskell programs are easy to parallelize: there is no shared state!Definitions are equations: easy to validate properties, allowing proofs.Haskell programs are easy to refactor.

Page 53: How many distinct values can be stored in 7 bits?

A domain specific language for pictures

horse :: Picture

flipH :: Picture -> PictureflipV :: Picture -> PictureinvertColour :: Picture -> Picture

above :: Picture -> Picture -> Picturebeside :: Picture -> Picture -> Picture

scale :: Picture -> Integer -> Picture

Page 54: How many distinct values can be stored in 7 bits?

Forming complex pictures

Infix use of above:horse `above` (flipH horse)

Naming pictures:bigPic = scale (horse `above` (flipH horse)) 42

Defining new functions:mirror pic = pic `beside` (flipV pic)

Page 55: How many distinct values can be stored in 7 bits?
Page 56: How many distinct values can be stored in 7 bits?

ASCII-art model of pictures

[Live coding look at ASCII-art representation as lists.]flipH = reverse -- reverse list of linesflipV = map reverse -- reverse each line of listabove = (++) -- join two listsbeside = zipWith (++) -- join corresponding lines

Page 57: How many distinct values can be stored in 7 bits?

The Prelude

The function reverse and the basic types come from the Haskell Prelude.[Live programming, browse Prelude.]

Page 58: How many distinct values can be stored in 7 bits?

Changing models

Abstraction means we can use a different representation for pictures.Display pictures using SVG graphics instead of ASCII-art.[Demonstration]

Page 59: How many distinct values can be stored in 7 bits?

[Live Coding from Chapters 1 and 2 of text]

See files Chapter1.hs and Pictures.hs