summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorloshprung <lshprung@scu.edu>2020-03-06 11:25:18 -0800
committerloshprung <lshprung@scu.edu>2020-03-06 11:25:18 -0800
commit433eccd27bbd47b5e0c4330575bcd31bcfc4c760 (patch)
tree5d6c0b9c9cfb51d9a413356e364ee8e38462dc10
parentafb09551d41f2c0ac666c68dc50c14b243e2784d (diff)
Post-class 03/06HEADmaster
-rw-r--r--03-04.md91
1 files changed, 90 insertions, 1 deletions
diff --git a/03-04.md b/03-04.md
index dfd1de5..1e4478b 100644
--- a/03-04.md
+++ b/03-04.md
@@ -168,5 +168,94 @@ int n; // n is 0000 0000 0000 1101
n << 1 //now n is 0000 0000 0001 1010
n << 4 //now n is 0000 0000 1101 0000
n >> 3 //now n is 0000 0000 0000 0001
-n << 4 //now n is 0000 0000 1101 0000
```
+
+- Compound assignment operators `<<=` and `>>=`
+ - cause the value resulting from the shift to be stored in the variable supplied as the left operand
+
+### Bitwise AND, XOR, and OR
+
+- The bitwise operators `&` (and), `^` (xor), and `|` (or)
+ - Take two operands that are viewed as strings of bits
+ - The operator determines each bit of the result by considering corresponding bits of each operand
+ - For each bit:
+ - `r = n & m` -> 1 when both n and m are 1
+ - `r = n | m` -> 1 when n and/or m is 1
+ - `r = n ^ m` -> 1 when n and m do not match
+
+- Example:
+
+```
+n = 0000 0000 0000 1101
+m = 0000 0000 0011 1100
+m & n = 0000 0000 0000 1100
+
+n = 0000 0000 0000 1101
+m = 0000 0000 0011 1100
+m | n = 0000 0000 0011 1101
+
+n = 0000 0000 0000 1101
+m = 0000 0000 0011 1100
+m ^ n = 0000 0000 0011 0001
+```
+
+- Compound assignment operators `&=`, `|=`, `^=`
+ - cause the resulting value to be stored in the variable supplied as the left operand
+
+---
+
+- Notes on shifting
+ - `<<` by 1 is the same as multiplying by 2
+ - `>>` by 1 is the same as dividing by 2
+- Notes on `~` and `!`
+ - `~` and `!` are different operators
+ - `~` is a bitwise operator
+ - each bit is reversed
+ - `!` is a logical complement or negation
+ - `!nonzero` -> false (zero)
+ - `!zero` -> true (one)
+
+- Notes on AND
+ - `x & 0` is always 0
+ - `x & 1` is always x
+- Notes on OR
+ - `x | 1` is always 1
+ - `x | 0` is always x
+
+- Masks -- Used to change specific bits in an integer
+ - To set specific bits
+ - Use OR with a mask in which only the bits to be set have 1:
+
+```
+short c = 0000 0101
+short mask = 0000 0010
+c | mask = 0000 0111
+```
+
+- To zero specific bits
+ - Use AND with a mask in which only the bits to be zeroed have 0
+
+```
+short c = 0000 0101
+short mask = 1111 1110
+c & mask = 0000 0100
+```
+
+- To verify specific bit
+ - Use AND with a mask in which only the bit to be verified is 1
+ - Result == 0 implied that bit == 0
+ - Result != 0 implies that bit == 1
+
+```
+short c = 0000 0101
+short mask = 0000 0100
+c & mask = 0000 0100 //not zero ==> bit is not zero
+```
+
+- Notes on XOR
+ - x ^ 0 is always x
+ - x ^ 1 is always ~x
+ - x ^ x is always 0
+ - x ^ ~x is always 1
+ - x ^ y == z, then
+ - x == z ^ y and y == z ^ x