bits and bytes boolean algebra (2.1.7--2.1.10) boolean algebra expressing in c
TRANSCRIPT
![Page 1: Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C](https://reader031.vdocuments.us/reader031/viewer/2022020417/5697c0301a28abf838cdaaad/html5/thumbnails/1.jpg)
Bits and Bytes
• Boolean Algebra (2.1.7--2.1.10)
• Boolean algebra• Expressing in C
![Page 2: Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C](https://reader031.vdocuments.us/reader031/viewer/2022020417/5697c0301a28abf838cdaaad/html5/thumbnails/2.jpg)
Boolean Algebra• Developed by George Boole in 19th Century– Algebraic representation of logic
• Encode “True” as 1 and “False” as 0AndAnd
A&B = 1 when both A=1 and B=1
NotNot ~A = 1 when A=0
OrOr A|B = 1 when either A=1 or B=1
Exclusive-Or (Xor)Exclusive-Or (Xor) A^B = 1 when either A=1 or B=1, but not both
![Page 3: Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C](https://reader031.vdocuments.us/reader031/viewer/2022020417/5697c0301a28abf838cdaaad/html5/thumbnails/3.jpg)
– CommutativityA | B = B | A A + B = B + AA & B = B & A A * 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 sumA & (B | C) = (A & B) | (A & C) A * (B + C) = A * B + B * C
– Sum and product identitiesA | 0 = A A + 0 = AA & 1 = A A * 1 = A
– Zero is product annihilatorA & 0 = 0 A * 0 = 0
– Cancellation of negation~ (~ A) = A – (– A) = A
Boolean Algebra Boolean Algebra Integer Integer RingRing
![Page 4: Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C](https://reader031.vdocuments.us/reader031/viewer/2022020417/5697c0301a28abf838cdaaad/html5/thumbnails/4.jpg)
– Boolean: Sum distributes over product
A | (B & C) = (A | B) & (A | C) A + (B * C) (A + B) * (B + C)
– Boolean: IdempotencyA | A = A A + A A
• “A is true” or “A is true” = “A is true”A & A = A A * A A
– Boolean: AbsorptionA | (A & B) = A A + (A * B) A
• “A is true” or “A is true and B is true” = “A is true”A & (A | B) = A A * (A + B) A
– Boolean: Laws of ComplementsA | ~A = 1 A + –A 1
• “A is true” or “A is false”– Ring: Every element has additive inverse
A | ~A 0 A + –A = 0
Boolean Algebra Boolean Algebra Integer Integer RingRing
![Page 5: Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C](https://reader031.vdocuments.us/reader031/viewer/2022020417/5697c0301a28abf838cdaaad/html5/thumbnails/5.jpg)
Properties of & and ^ (Xor)•Boolean Ring– {0,1}, ^, &, , 0, 1 – Identical to integers mod 2– is identity operation: (A) = A
A ^ A = 0
•Property Boolean Ring– Commutative sum A ^ B = B ^ A– Commutative product A & B = B & A– Associative sum (A ^ B) ^ C = A ^ (B ^ C)
– Associative product (A & B) & C = A & (B & C)
– Prod. over sum A & (B ^ C) = (A & B) ^ (B & C)
– 0 is sum identity A ^ 0 = A– 1 is prod. identity A & 1 = A– 0 is product annihilator A & 0 = 0– Additive inverse A ^ A = 0
![Page 6: Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C](https://reader031.vdocuments.us/reader031/viewer/2022020417/5697c0301a28abf838cdaaad/html5/thumbnails/6.jpg)
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 01000001 01111101 00111100 10101010
![Page 7: Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C](https://reader031.vdocuments.us/reader031/viewer/2022020417/5697c0301a28abf838cdaaad/html5/thumbnails/7.jpg)
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
~010000012 --> 101111102
– ~0x00 --> 0xFF
~000000002 --> 111111112
– 0x69 & 0x55 --> 0x41
011010012 & 010101012 --> 010000012
– 0x69 | 0x55 --> 0x7D
011010012 | 010101012 --> 011111012
![Page 8: Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C](https://reader031.vdocuments.us/reader031/viewer/2022020417/5697c0301a28abf838cdaaad/html5/thumbnails/8.jpg)
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
![Page 9: Bits and Bytes Boolean Algebra (2.1.7--2.1.10) Boolean algebra Expressing in C](https://reader031.vdocuments.us/reader031/viewer/2022020417/5697c0301a28abf838cdaaad/html5/thumbnails/9.jpg)
Cool Stuff with Xorvoid funny(int *x, int *y)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
BABeginBA^B1(A^B)^B = AA^B2A(A^B)^A = B3ABEnd
*y*x