Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C.

Similar presentations


Presentation on theme: "Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C."— Presentation transcript:

1 Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C

2 Boolean Algebra Developed by George Boole in 19th Century – Algebraic representation of logic Encode “True” as 1 and “False” as 0And A&B = 1 when both A=1 and B=1 Not ~A = 1 when A=0 Or A|B = 1 when either A=1 or B=1 Exclusive-Or (Xor) A^B = 1 when either A=1 or B=1, but not both

3 – Commutativity A | B = B | AA + B = B + A A & B = B & AA * B = B * A – Associativity (A | B) | C = A | (B | C)(A + B) + C = A + (B + C) (A & B) & C = A & (B & C)(A * B) * C = A * (B * C) – Product distributes over sum A & (B | C) = (A & B) | (A & C)A * (B + C) = A * B + B * C – Sum and product identities A | 0 = AA + 0 = A A & 1 = AA * 1 = A – Zero is product annihilator A & 0 = 0A * 0 = 0 – Cancellation of negation ~ (~ A) = A– (– A) = A Boolean Algebra  Integer Ring

4 – Boolean: Sum distributes over product A | (B & C) = (A | B) & (A | C)A + (B * C)  (A + B) * (B + C) – Boolean: Idempotency A | A = AA + A  A “A is true” or “A is true” = “A is true” A & A = AA * A  A – Boolean: Absorption A | (A & B) = AA + (A * B)  A “A is true” or “A is true and B is true” = “A is true” A & (A | B) = AA * (A + B)  A – Boolean: Laws of Complements A | ~A = 1A + –A  1 “A is true” or “A is false” – Ring: Every element has additive inverse A | ~A  0A + –A = 0 Boolean Algebra  Integer Ring

5 Properties of & and ^ (Xor) Boolean Ring –  {0,1}, ^, &, , 0, 1  –Identical to integers mod 2 –  is identity operation:  (A) = A A ^ A = 0 PropertyBoolean Ring –Commutative sumA ^ B = B ^ A –Commutative productA & B = B & A –Associative sum(A ^ B) ^ C = A ^ (B ^ C) –Associative product(A & B) & C = A & (B & C) –Prod. over sumA & (B ^ C) = (A & B) ^ (B & C) –0 is sum identityA ^ 0 = A –1 is prod. identityA & 1 = A –0 is product annihilatorA & 0 = 0 –Additive inverseA ^ A = 0

6 General Boolean Algebras Operate on Bit Vectors –Operations applied bitwise All of the Properties of Boolean Algebra Apply 01101001 & 01010101 01000001 01101001 | 01010101 01111101 01101001 ^ 01010101 00111100 ~ 01010101 10101010 01000001011111010011110010101010

7 Bit-Level Operations in C Operations &, |, ~, ^ Available in C – Apply to any “integral” data type long, int, short, char – View arguments as bit vectors – Arguments applied bit-wise Examples (Char data type) – ~0x41 --> 0xBE ~01000001 2 -->10111110 2 – ~0x00 --> 0xFF ~00000000 2 -->11111111 2 – 0x69 & 0x55 --> 0x41 01101001 2 & 01010101 2 --> 01000001 2 – 0x69 | 0x55 --> 0x7D 01101001 2 | 01010101 2 --> 01111101 2

8 Contrast: Logic Operations in C Contrast to Logical Operators – &&, ||, ! View 0 as “False” Anything nonzero as “True” Always return 0 or 1 Early termination Examples (char data type) – !0x41 --> 0x00 – !0x00 --> 0x01 – !!0x41 --> 0x01 – 0x69 && 0x55 --> 0x01 – 0x69 || 0x55 --> 0x01

9 Cool Stuff with Xor void funny(int *x, int *y) { *x = *x ^ *y; /* #1 */ *x = *x ^ *y; /* #1 */ *y = *x ^ *y; /* #2 */ *y = *x ^ *y; /* #2 */ *x = *x ^ *y; /* #3 */ *x = *x ^ *y; /* #3 */} –Bitwise Xor is form of addition –With extra property that every value is its own additive inverse A ^ A = 0 BA Begin BA^B 1 (A^B)^B = AA^B 2 A(A^B)^A = B 3 AB End *y*x


Download ppt "Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C."

Similar presentations


Ads by Google