Subtopic: Bitwise operations Bitwise examples Some bitwise problems

4 downloads 636 Views 68KB Size Report
Applications: - Compact storage of status data. - Storage of sets. - Arithmetic at hardware level. - Systems programming. David Keil 1/03 3. Shift-left operator.
1. Types, loops, subprograms

1/03

1

C/C++ Operation operator

Shift-left-3 multiplies by 8 because 2 3 = 8 1/03

1/03

2

Example

Complement

~

~ 100000002 = 011111112

OR

|

11002 | 10012 = 11012

AND

&

11002 & 1001 2 = 10002

Left shift


> 2 = 1102

XOR

^

10012 ^ 10102 = 0011 2

3

Some bitwise problems

David Keil

1/03

4

Setting a bit to 1 with OR • To set nth bit of a number, create a mask by shifting a 1 left (n − 1) times, and then bitwise-OR the number with the mask.

Evaluate: • 8 >> 1 = • 3 input; cout x; mask = 1 s; Output: cout