Everything is bits
- Each bit is 0 or 1
- By encoding/interpreting sets of bits in various ways
- Why bits? Electronic Implementation
Encoding Byte Values
- Byte = 8 bits
Boolean Algebra
- Developed by George Boole in 19th Century
- Operate on Bit Vectors
- All of the Properties of Boolean Algebra Apply
Representing & Manipulating Sets
- Representation
- Operations
- & Intersection
- | Union
- ^ Symmetric difference
- ~ Complement
Logic Operations in C
- Contrast to Logical Operators
- &&, ||, !
Shift Operations
- Left Shift: x << y
- Right Shift: x >> y
- Undefined Behavior
Numeric Ranges
- Unsigned Values
- Two’s Complement Values
- Other Values
Signed vs. Unsigned in C
- Constants
- Casting
Casting Signed <=> Unsigned: Basic Rules
- Bit pattern is maintained
- But reinterpreted
- Can have unexpected effects: adding or subtracting 2^w
Expanding, Truncating: Basic Rules
-
Expanding
- Unsigned: zeros added
- Signed: sign extension
- Both yield expected result
-
Truncating
- Unsigned/sighed: bits are truncated
- Result reinterpreted
- Unsigned: mod operation
- Signed: similar to mod
- For small numbers yields expected behavior
Addition, negation, multiplication, shifting
- Unsigned Addition
- Two’s Complement Addition
- Multiplication
- Unsigned Multiplication
- Signed Multiplication
- Power-of-2 Multiply with Shift
- Unsigned Power-of-2 Divide with Shift
- >>>: logical shift
- >>: arithmetical shift
- Counting Down with Unsigned
- Use unsigned when performing modular arithmetic or using bits to represent sets
- Representations in memory, pointers, strings
- Byte-Oriented Memory Organization
- Machine Words
- Word-Oriented Memory Organization
- Byte Ordering