Looking Inside a Value
Application : UTF-8 Encoding
Working with Bits
Hexadecimal Review
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
C Bit-Level Operators
Looking at Bits
Meet ~ (a Unary Bitwise Operator)
Friends of ~
Binary Bitwise Operators
Bitwise And
Bitwise Or
Bitwise vs Logic Operators
Exclusive Or
Shift Operators(Left)
Shift Operators(Right)
Shift Operators(Right Arithmetic)
0 1 0 0 1 0 1 0
0 0 0 1 0 0 1 0
For signed operands, you get
copies of the high-order bit shifted in.
* Why arithmetic shift?
– For signed values, this maintains the sign.
* We can use left shift to multiply by 2 or any
power of 2
– x «_space;y is the same as x * 2^y
* And, we can use right shift like a divide operation
– x»_space; y is the same as x / 2^y -> But, this only
works for positive numbers.
unsigned int x y;
x = 75 «_space;2; // that’s 300
y = 75»_space; 2; // that’s 18
Makes sense for signed
No left arithmetic operators
OK, How Do We Use These Operators?
Clearing Selected Bits
Setting Selected Bits
Inverting Selected Bits
Testing Selected Bits
Counting or Printing
Counting Bits
Copying Selected Bits
Result:
0 1 0 0 0 0 1 1
| 0 0 1 0 1 1 0 0
—————–
0 1 1 0 1 1 1 1