Programming

and computers

Aurélien Ginolhac, DHML

University of Luxembourg

Monday 16 March, 2026

Learning objectives

You will learn to:

  • What are the differences between hard/software
  • Graps how computer work
  • A brief history of computers
  • Learn the main programming controls (for/if)

Hardware vs Software

Hardware: body of a computer

Software: soul of a computer

Hardware / Software

OS: / /

OS: /

Many machine OS: : PlayStation, smartTV, routeurs, servers

How is information stored?

Binary

  • 1 or 0 (on or off: high / low voltage)
  • Two digits are easier to manipulate
  • Less complexity = less errors or chances to fail (permutations of bits)
  • This is how everything is stored, even music and pictures!
  • bit = 1 binary digit
  • 1 byte = 8 bits
  • 1 byte can encode up to 256 values (0-255, \(2^8\) ). IP is 0-255.0-255.0-255.0-255.
  • Naming is tricky:1 GB = 8 Gb

Memory

Random Access Memory: RAM

  • Fast
  • Limited capacity (8GB is decent)
  • Wiped out when powered off

Picture: Andrey Matveev, Unsplash

Hard drive (now mostly SSD)

  • Slower, SSD faster than HDD
  • Large capacity (250GB is standard)
  • Retain information

Photo by Benjamin Lehman, Unsplash

How do we get to binary?

Decimal vs binary

Decimal Binary
0 0000000
1 0000001
2 0000010
3 0000011
4 0000100
5 0000101
6 0000110
7 0000111
8 0001000
9 0001001
10 0001010
11 0001011
12 0001100
13 0001101
14 0001110
15 0001111
107 1101011

…a circuit would automatically add two numbers using only relays and switches; for convenience using base 2. In fact any operation that can completely described in a finite number of steps using the words if, or, and etc. can be done automatically with relays. — Claude Shannon (Master’s Thesis, MIT, 1937)

A Brief History

Abacus Objects

Keep track / help for calculus (little stone in Latin)

Not necessarily decimal system, bi-quinary, 5-digit base-20 system etc.

  • Suanpan (China 200 BC)

  • Quipu (circa 1500 AD)

Mechanical systems

Blaise Pascal in 1642-1645 conceived the Pascaline: direct additions / subtractions

Charles Babbage

Analytical Engine using punched cards (1834-1871)

Ada Lovelace

1815-1852, English mathematician and first programmer using punched card

Diagram of Bernoulli numbers (1843): first published computer algorithm. Planned for the Babbage’ analytical machine.

Information Theory - Claude Shannon

Shannon formalism enabled quantification, entropy (noise) using the bit (coined by John Tukey also at Bell Labs).

The Mathematical Theory of Information (1948) laid the fundation of most of the digital world we live in.

Claude Shannon with “Theseus” the mouse in its Maze (1950)

Phone relays, trial and error using 75 bits and memorization of the escape route.

Picture: MIT Museum

Shannon work on encoding word information

  • “Zero-order approximation”—that is, random characters, no structure or correlations.
XFOML RXKHRJFFJUJ ZLPWCFWKCYJ
FFJEYVKCQSGHYD QPAAMKBZAACIBZLHJQD.
  • First order. Each character is independent of the rest, but the frequencies are those expected in English: more e’s fewer z’s.
OCRO HLI RGWR NMIELWIS EU LL NBNESEBYA
TH EEL ALHENHTTA OOBTTYA NAH BRL
  • Second order. Frequencies of each character match English and diagram (pair)1 frequencies (most common is th).
ON IE ANTSOUTINYS ARE TINCTORE ST BE S DEAMY ACHIN
DILONASIVE TUCOOWE AT TEASONARE FUSO TIZIN ANDY TOBESEACE CTISBE.
  • Third order- trigram structure.
IN NO IST LAT WHEY CRATICT FROURE BIRS GROCID
PONDENOME OF DEMONSTURES OF THE REPTAGIN IS
REGOACTIONA OF CRE
  • First-order word approximation.
REPRESENTING AND SPEEDILY IS AN GOOD APT OR COME CAN DIFFERENT 
NATURAL HERE HE THE A IN CAME THE TO OF TO EXPERT GRAY COME 
TO FURNISHES THE LINE MESSAGE HAD BE THESE.
  • Second-order word approximation—now pairs of words appear in the expected frequency, so we do not see “a in” or “to of.”
THE HEAD AND IN FRONTAL ATTACK ON AN ENGLISH WRITER THAT 
THE CHARACTER OF THIS POINT IS THEREFORE ANOTHER METHOD 
FOR THE LETTERS THAT THE TIME OF WHO EVER TOLD THE PROBLEM FOR AN UNEXPECTED.

How computer add numbers?

Adding 5 (0101) and 6 (0110) = 1011

Chained adders to sum bit per bit

How computer work? Logical gates

Gates of half-adder

XOR gate

Exclusive OR: 1 only if one input is 1

AND gate

1 only if both inputs are 1

Full adder, have a carry as third input.

2 XOR + 2 AND + 1 OR gate

4 bits full adder. First is half-adder.

Logical gates with transistors

AND gate on the left, OR gate on the right

  • Optionally I will bring my own breadboards with transistor gates, half-adder and IC full-adder.

Relay, Vaccum tubes, Transistors

How computer went faster and smaller. But designed for phone comp

Relay Vacuum tube Transistor

Relay comes from the word fresh horse that replaced an exhausted one (for long distance telegraphy, A weakened current could still operate a relay enabling a new circuit powered by a new battery) […] had more potential than its inventors realized” (combined signal from different sources).

1958 FACOM computer relay

5,000 relays computing \(\sqrt2\), sound ON!

Operating Systems

Department 1127 Bell Labs: UNIX (Dec 1969)

Dennis Ritchie and Ken Thompson

Created the C language, modern file architecture, pipes, shells, everything a process etc. Files contains uninterpreted bytes. Any device can then be part of the file system. Abstraction that can connect any new devices.

Legacy of UNIX is everywhere:

Sys.Date()
[1] "2026-04-21"
# dates are just integers
as.integer(Sys.Date())
[1] 20564
# that refers to a specific date (datetime are seconds)
Sys.Date() - as.integer(Sys.Date())
[1] "1970-01-01"
Sys.time()
[1] "2026-04-21 07:20:48 UTC"
as.integer(Sys.time())
[1] 1776756048
# datetime are seconds)
Sys.time() - as.integer(Sys.time())
[1] "1970-01-01 00:00:00 UTC"

UNIX heritage

1974 a copy of AT&T Research Unix launched the University of California, Berkeley.

Will become the Berkeley Software Distribution (BSD), then FreeBSD, MacOS etc.

Click to enlarge

Linux

Linus Torvalds

Who wanted to understand things.

At 22, he open-sourced his own OS

All android phones, all world servers, cars etc.

Not to be forgotten

Alan Turing

  • Turing machine, Enigma decryption

Margaret Hamilton

  • Apollo embedded-systems

What is a program, a language?

  • A program is a series of instructions
  • Started with punched cards (Babbage)
  • A language is a syntax
    • +/- close to machine instructions
    • Interpreted (R/Python), slower and easier
    • Compiled (C, Rust) efficient and complex
  • The instructions requirements depend on the language
    • Memory allocation (C)
    • Memory ownership (Rust)
    • Strict indentation (Python)
  • But languages don’t make communities, people do (Julia)

Syntax

Some differences are superficial:

  • BASIC: print
  • Java: System.out.print();
  • C++: printf();

But, high level vs low level programming languages

#!/usr/bin/env python

print("Hello from BASV!")
  • C++, while compiled still high-level
#include <stdio.h>
int main(void) {      
    printf("Hello from BASV!\n");
    return 0; 
} 
section .data
    msg db "Hello from BASV!", 10   ; 10 is newline
    len equ $ - msg

section .text
    global _start

_start:
    mov     rax, 1          ; syscall: sys_write
    mov     rdi, 1          ; file descriptor: stdout
    mov     rsi, msg        ; pointer to string
    mov     rdx, len        ; length of string
    syscall

    mov     rax, 60         ; syscall: sys_exit
    xor     rdi, rdi        ; exit code 0
    syscall
nasm -f elf32 hello.asm -o hello.o
ld -m elf_i386 hello.o -o hello

Run-time errors

Errors detected during execution lead to crash like division by 0

C language

#include <stdio.h>
 
int main(void)
{   
    int a = 8;
    int b = 4;
    int c = 0;
    int average = (a + b) / 2;
    int result = average / c;
    
    printf("mean (%d, %d) by %d = %d\n", a, b, c, result);
    return 0; 
} 

Floating point exception 💣

No crash, but returns Infinite

a <- 8
b <- 4
c <- 0
average <- (a + b) / 2
result <- average / c
result
[1] Inf

Logical errors

Errors that are not detected, execution continues

C, spot the mistake

#include <stdio.h>
 
int main(void)
{   
    int a = 8;
    int b = 4;
    int c = 2;
    int average = a + (b / 2);
    int result = average / c;
    
    printf("mean (%d, %d) by %d = %d\n", a, b, c, result);
    return 0; 
} 

Solution

  • Average defined as a + (b / 2)
  • And not (a + b) / 2

Algorithm: definition

An algorithm is a well-ordered collection of unambiguous and effectively computable operations that when executed produces a result and halts in a finite amount of time

Schneider and Gersting 1995

Pseudocode: prepare and plan

  • Not in normal language, nor in pure code
  • Separate syntax from logical misconceptions
  • Like a cooking recipe

Example

  • Given a vector of several numbers
  • How to check numbers equal to 2?

  1. Iterate through the elements with a variable i
  2. Test if i is equal to 2
  3. Print the value of i.

in

serie <- c(1, 2, 3 ,4)
for (i in serie) {
  if (i == 2) print(paste("found 2 in i = ", i))
}
[1] "found 2 in i =  2"

And with vectorization! What are the results?

serie == 2
serie[serie == 2]
sum(serie == 2)

Variables

  • Variables store information for later use
  • Variable names should be clear and understandable

“It is merely an empty basket until you put something in it — Charles Babbage

Naming

  • Be consistent!, use naming conventions:
    • alllowercase
    • period.separated
    • underscore_separated (_*_)
    • lowerCamelCase
    • UpperCamelCase

Examples

  • my_age, class_size to store a number
  • my_name to store letters (a “string”)
  • add_footer, for a function (action)
  • is_odd, for a function that detects odd numbers and return a logical (TRUE/FALSE)

One named value in memory

We assign the name x to the value 1.

assignment operator is <-

x <- 1

In memory:

0x5e903189e078 is the RAM (volatile) address where 1 is stored.

Values / Indices

How to have more than one value to store?

Data structure: vectors

We assign the name y to the values 1, 2, 3.

concatenation function is c()

y <- c(1, 2, 3) # shortcut 1:3

For characters instead of integers

yc <- c('a', 'a', 'b')

global string pool is actually used for performance

Pointers are memory addresses

Control statements: if, else

Definition: if (condition)

  • condition must be a boolean (TRUE/FALSE)
  • Could be one of more conditions
  • Combined by AND or OR statement
  • else is optional
  • else if is possible
  • Use indentations in code
Syntax
if (condition) {
  do_task()
} else {
  do_something_else()
}

Logical/boolean operators, AND, OR, NOT

3 types

Variables
Logical tests
A B A AND B A OR B NOT A
False False False False True
False True False True True
True False False True False
True True True True False

var1 <- 1:20 # a vector of 1, 2, 3, ..., 20
# for AND use &
if (var1 > 10 & var1 <= 20)
# for OR use |
if (var1 > 10 | var1 <= 20)

What will be the difference?

Conditional blocks in R

if, else if and else

  • Blocks are delimited with curly braces { and }
  • Indent your code after {, RStudio does it for you
  • Re-indent with Cmd/Ctrl + i

Example: compare 2 numbers, handling the 3 possibilities

n1 <- 11
n2 <- 7
if (n1 == n2) {
  print(paste(n1, "and", n2, "are equal"))
} else if (n1 > n2) {
  print(paste(n1, "is greater than", n2))
} else {
  print(paste(n1, "is smaller than", n2))
}
[1] "11 is greater than 7"

We will see dplyr::case_when() for multiple if / else statements

For loops

  • Loop over values
v <- c(3:6)
for (i in v) {
  print(i)
}
[1] 3
[1] 4
[1] 5
[1] 6
  • Loop over indices (seq_along() handles empty vector)
for (i in seq_along(v)) {
  print(paste("indice", i, "=", v[i]))
}
[1] "indice 1 = 3"
[1] "indice 2 = 4"
[1] "indice 3 = 5"
[1] "indice 4 = 6"

We will try to avoid writing any for loops!

Using functional programming.

For loops in , the “are slow” myth

Growing vector

for_loop <- function(x) {
  res <- c() # initialize an empty vector
  for (i in seq_len(x)) {
    res[i] <- i
  }
}

Using Rcpp

library(Rcpp) # binds cpp compilation to R
cppFunction("NumericVector rcpp(int x) {
  NumericVector res(x);
  for (int i=0; i < x; i++) {
    res[i] = i;
  }
}")

Correct allocation

for_loop <- function(x) {
  res <- vector(mode = "integer", length = x)
  for (i in seq_len(x)) {
    res[i] <- i
  }
}

History & Fiction

Cubetto

For the students interested (= not mandatory):

As introduction to functions we have an extra 45 minutes course with the Cubetto and my own breadboard adders

Before we stop

You learned to:

  • What is a computer
  • What is a program / memory
  • Write pseudo-code
  • Control flows
    • For loops
    • Conditional blocks


I think that the present century will see a great upsurge and development of the whole information business (collection, transmitting and perhaps more importantly the business of processing it) — Claude Shannon

Acknowledgments 🙏 👏

licensed under the CC International License.

Thank you for your attention!