CS102 - Lesson 4
Bitwise Operators, Switch, Loops, Functions, Basic Arrays
Instructor: Professor Hong
## Review & Questions
## Quiz #1 * 20 minutes
## Bitwise Operators
## Bitwise Operators *
&
-- bitwise AND *
|
-- bitwise inclusive OR *
^
-- bitwise exclusive OR *
<<
-- left shift *
>>
-- right shift *
~
-- one's complement (unary)
## Bitwise Operators Example ``` #include
int main() { printf("5 & 4 = %d\n", 5 & 4); printf("5 | 4 = %d\n", 5 | 4); printf("5 ^ 4 = %d\n", 5 ^ 4); printf("5 << 2 = %d\n", 5 << 2); printf("12 >> 2 = %d\n", 12 >> 2); printf("~5 = %d\n", ~5); // Note 2's complement // 5 is 0000...0101 // Doing on's complement, we have // ~5 is 1111...1010 // which is MIN+sum(2^n) where n excludes n=0 and n=2 // Or simpler: -8 + 0 + 2 + 0 return 0; } ```
## HW Check [https://hong3cooper.github.io/](https://hong3cooper.github.io/) for your section's HW link. HW#3 due 09/28/24 @ 11:59PM HW#4 due 10/05/24 @ 11:59PM