## Bitwise Operators

I have a certificate examination next time, and I would refresh my knowledge about bitwise operators. I thought, that’s worth a note.

### Bitwise AND (&)

``````A 	B 	A AND B
0 	0 	0
0 	1 	0
1 	0 	0
1 	1 	1
``````

#### Examples

``````10 & 5
= 10: 1010
=  5: 0101
=     0000 => 0
``````
``````5 & 6
= 5: 0101
= 6: 0110
=    0100 => 4
``````
``````5 & 9
= 5: 0101
= 9: 1001
=    0001 => 1
``````

### Bitwise OR (|)

``````A 	B 	A OR B
0 	0 	0
0 	1 	1
1 	0 	1
1 	1 	1
``````

#### Examples

``````10 | 5
= 10: 1010
=  5: 0101
=     1111 = 15
``````
``````5 | 6
= 5: 0101
= 6: 0110
=    0111 = 7
``````
``````5 | 9
= 5: 0101
= 9: 1001
=    1101 = 13
``````

### Bitwise XOR (^)

``````A 	B 	A XOR B
0 	0 	0
0 	1 	1
1 	0 	1
1 	1 	0
``````

#### Examples

``````10 ^ 5
= 10: 1010
=  5: 0101
=     1111 = 15
``````
``````5 ^ 6
= 5: 0101
= 6: 0110
=    1011 = 11
``````
``````5 ^ 9
= 5: 0101
= 9: 1001
=    1100 = 12
``````

### Bitwise NOT (~)

``````A 	~A
0 	1
1 	0
``````

#### Example

`````` ~0101
= 1010
``````

### Right shift (»)

It shifts each bit to the right

#### Example

``````8 = 1000
1000 >> 0 = 1000 = 8
1000 >> 1 = 0100 = 4
1000 >> 2 = 0010 = 2
1000 >> 3 = 0001 = 1
``````

### Left shift («)

It shifts each bit to the left

#### Example

``````8 = 1000
1000 << 1 = 10000 = 16
1000 << 2 = 100000 = 32
1000 << 3 = 1000000 = 64
1000 << 4 = 10000000 = 128
``````