Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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.

2 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

3 Working with sets Boolean algebras Characteristic value of a set Bit vectors – Representation – Operations – Use in programming language Example: Vacation program – Bit masks

4 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.

5 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.

6 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 }  2 1 + 2 4 + 2 6. The binary representation of this number shows the set! This is called a bit vector. 31...76543210 0001010010

7 Logic operations Examples 1010110101101011 AND 11110000AND00011111 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?

8 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 }. How would you handle subset? what31…6543210 x111 y111 x | y11111 x & y1 x ^ y1111 ~x111111 Blank cells are zero.

9 Binary number Value is based on powers of two: This number equals 128+32+8+4+1 = 173 Interesting relationship between unary ~ and – ~n = – (n + 1) …2561286432168421 010101101

10 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 “0x” followed by, usually, 8 digits. Each digit is: 0-9, a-f a = ten = “1010”d = thirteen = “1101” b = eleven = “1011”e = fourteen = “1110” c = twelve = “1100”f = fifteen = “1111” Example: What does 0x7ffc0 look like in binary?

11 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 = 2 4 = 16 5 << 3 = 5 * 2 3 = 40 13 >> 1 = 13 / 2 1 = 6

12 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? …

13 Mask Often we want to inspect/modify more than 1 bit, so we need to create an operand that looks like: – Ones in middle: 0000000000111110000000 – Zeros in middle: 1111111000001111111111 How? Two ways – Can subtract powers of 2 – Use only bitwise operations

14 Examples ~0 … 11111111 11111111 ~0 << 5 … 11111111 11100000 ~(~0 << 5)… 00000000 00011111 ~(~0 << 5) << 4… 00000001 11110000 And you can invert one more time if needed. How could we write this as a difference of powers of 2 ?

15 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 = 0 date |= month << 16 date |= day << 11 date |= year MonthDayYear 4511


Download ppt "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."

Similar presentations


Ads by Google