Lecture 2 Notes: Representing Information
Administrative details
Homeworks due on Friday at 11:59:59pm<br\ Help from TF’s is not guaranteed after 9:00pm.<br\ Sections next week will cover git (version control software)
Representation of information
- Memory
- Integers
- Bitwise arithmetic
- Undefined behavior
Computer = smoking hot cpu surrounded by an ocean of memory
Layer of abstraction between C and the machine
A systems programmer programs for all of these layers
while -> property of C abstract machine<br\ INT_MIN: minimum representable value of type “int” -> property of target architecture
- x86, x86-64
fork: starts a new program -> property of OS
Bits
A bit is either 0 or 1
- smallest possible unit of information that can exist
Why are bits a good unit for computer design?
- good accuracy
Memory is a cloud of bits
- locations in memory are called addresses
- we group bits into units of 8, called a byte
- byte : smallest addressable unit of storage in a computer’s memory<br\
In C:
- unsigned char -> BYTE
- can store 1 char in Latin script
- collection of 8 bits
- b7 b6 … b1 b0 can represent numbers from 0 to 255 (= 28 - 1) inclusive
Base 2 (Binary)
1111 1111 = 1x27 + 1x26 + … + 1x20 = 255
what is 1111 1111 + 1? with only 8 bits, the answer is 0. (we need 9 bits to represent 256).
1100 0000 = 128 + 64 = 192
Base 16 (Hexadecimal)
1111 1111 = 0xFF<br\ 1100 0000 = 0xC0
Digits in hex: <br\
hex dec binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
Addresses
- On x86, an address is 32 bits (4 bytes long)
- representable addresses: [0, 232 - 1] (4 gigabytes)
- On x86-64, an address is 64 bits (8 bytes long)
- representable addresses: [0, 264 - 1] (16 exabytes)
To C, memory is a huge array of bytes:
unsigned char memory[];
Pointers and Arrays
Memory addresses can be treated exactly like integers.
unsigned char* x; //this is a type representing an “address of a byte”
unsigned char* x = memory; // x’s numeric value is set to 0
x = x + 1; // x’s numeric value is 1
x = x - 1; // x’s numeric value is 0
unsigned char* y;
y = memory + 100;
if (x < y) // true
unsigned char bob; // allocate space in memory for a char
x = &bob; // address of operator
gcc -c x.c -o x.o
- generates an object file for x.c
extern
- keyword meaning the variable is in some other file
objdump -s x.o
- -s : print everything
- -t : print labels
Information
Memory also contains code.<br\
- For example, some portion of memory is allocated to store main’s instructions
Information is data + context.<br\ Steganography - hiding information in an image