Set A formal collection of objects which can
Set • A formal collection of objects, which can be anything • May be finite or infinite • Can be defined by: – Listing the elements – Drawing a picture – Writing a rule defining what is inside • Operations – ’ – = – Cartesian Product: • A B = { (x, y) | x A and y B } • Think of picking food from a menu: how many possible meals? • The empty set is a subset of every set. It’s unique.
Sets of strings • • Begin with an alphabet Σ Σn = all words with n “letters” Σ* = all words of any length, including length 0 * in general means “ 0 or more instances of” – 0* means all words containing any number of 0’s only – (0 + 11)* means any concatentations of “ 0” and “ 11” • “language” = any set of words from Σ* • Example: Let’s say Σ = { a, b }. – L = { ε, a, b, aa, ab, ba, bb } is a language with 7 words. This language happens to be Σ 0 Σ 1 Σ 2. • Example: set of legal identifiers
Working with sets • Boolean algebras • Characteristic value of a set • Bit vectors – Representation – Operations – Use in programming language Example: Vacation program – Bit masks
Properties • Set theory and propositional logic are both boolean algebras. – What do we mean by “an algebra” ? • Definition of a boolean algebra: a mathematical system with values 0 and 1 and operators + and * having these properties: – Closure, Commutativity, Distributivity, identity element, inverse • Logic and set theory are “subclasses” of boolean algebra. They inherit these properties.
Properties (2) • What is the practical consequence? • Expressions involving set operations are analogous to boolean expressions. • So, to verify that A (B C) (A B) (A C), we can do so by converting it to the corresponding propositional formula, and seeing if it’s a tautology. a (b c) (a b) (a c) • In general, another way to show X Y is to pick an arbitrary x X and show why it must be in Y. – To show X = Y is done in two parts: X Y and Y X.
Characteristic value • Often, a set consists of just whole numbers. – E. g. { 1, 4, 6 } • In this case, it’s convenient to refer to the set by a single numerical value: the set’s characteristic value. • The value is the sum of all 2 i where i is a number in the set. – E. g. { 1, 4, 6 } 21 + 24 + 26. • The binary representation of this number shows the set! This is called a bit vector. 31 . . . 7 6 5 4 3 2 1 0 0 1 0
Logic operations • Examples 10101101 AND 11110000 AND 01101011 00011111 • Try the same examples with “OR” and “XOR” • Observations: – What happens when you AND with a 1? With a 0? – What about OR’ing with a 1 versus a 0? – What about XOR? • How would we … – Make the rightmost three bits all 1’s? All 0’s?
Operations • Inside the computer, set operations on bit vectors are very fast. • Notation in C, C++ and Java: | & ^ ~ • Let’s look at x = { 1, 4, 6 } and y = { 3, 4, 5 }. what 31 … x 6 5 1 y x|y 1 3 2 1 1 0 1 1 1 1 x&y Blank cells are zero. 1 x^y ~x 4 1 1 1 • How would you handle subset?
Binary number • Value is based on powers of two: … 256 128 64 32 16 8 4 2 1 0 1 0 1 This number equals 128+32+8+4+1 = 173 • Interesting relationship between unary ~ and – ~n = – (n + 1)
Shorthand • Occasionally we need to write a large number in binary. Better to do so as “hexadecimal” – a shorthand that compresses 4 bits in one symbol. • Notation: begin with “ 0 x” followed by, usually, 8 digits. • Each digit is: 0 -9, a-f a = ten = “ 1010” b = eleven = “ 1011” c = twelve = “ 1100” d = thirteen = “ 1101” e = fourteen = “ 1110” f = fifteen = “ 1111” • Example: What does 0 x 7 ffc 0 look like in binary?
Shift operations • Two directions – Shift left (<<) basically means to multiply by a power of 2. – Shift right (>>) essentially divides by a power of 2 and eliminates the remainder. • For example 1 << 4 = 24 = 16 5 << 3 = 5 * 23 = 40 13 >> 1 = 13 / 21 = 6
Practical use • Let’s suppose we want to use an integer to hold 31 boolean values, representing the days of a month. We’ll use bit positions 1 -31 only. • Start with sept = 0, clearing all bits. • To turn on a certain bit, we need to “or with 1” sept = sept | (1 << 29) • To turn off a bit, we need to “and with 0” sept = sept & ~(1 << 29) • How would we determine value of just 29 th bit? • Multiple bits? …
Mask • Often we want to inspect/modify more than 1 bit, so we need to create an operand that looks like: – Ones in middle: 00000111110000000 – Zeros in middle: 11111110000011111 • How? Two ways – Can subtract powers of 2 – Use only bitwise operations
Examples • • ~0 ~0 << 5 ~(~0 << 5) << 4 … 11111111 … 1111 11100000 … 0000 00011111 … 00000001 11110000 • And you can invert one more time if needed. • How could we write this as a difference of powers of 2?
Bit field • Store more than one value in a variable, to save space. • Example: date as month-day-year – How many bits do we need for each? date Month Day Year 4 5 11 = 0 |= month << 16 |= day << 11 |= year
- Slides: 15