Operator | Name |
---|---|
| | OR |
& | AND |
^ | XOR |
<< | Left shift |
>> | Right shift |
~ | Not / One's complement |
a: | 1 | 1 | 0 | 0 |
b: | 1 | 0 | 1 | 0 |
a|b | 1 | 1 | 1 | 0 |
a: | 1 | 1 | 0 | 0 |
b: | 1 | 0 | 1 | 0 |
a&b | 1 | 0 | 0 | 0 |
a: | 1 | 1 | 0 | 0 |
b: | 1 | 0 | 1 | 0 |
a^b | 0 | 1 | 1 | 0 |
a: | 0 | 0 | 1 | 1 | 0 | 1 | = 13 |
a << 1 | 0 | 1 | 1 | 0 | 1 | 0 | = 26 |
a << 2 | 1 | 1 | 0 | 1 | 0 | 0 | = 52 |
a: | 1 | 1 | 0 | 1 | 0 | 0 | = 52 |
a >> 1 | 0 | 1 | 1 | 0 | 1 | 0 | = 26 |
a >> 2 | 0 | 0 | 1 | 1 | 0 | 1 | = 13 |
With signed integers:
~a = -1 * a - 1
~-1 = 0
if (array.indexOf(val) == -1) // if NOT found
vs
if (!~array.indexOf(val)) // if NOT found
Group errors in a single value
Create an error reporting
Networking
Encryption
Color storage
RGB color:
Color storage
Can be stored in a long (4 bytes)
#B2A3C5CC
R: B2 (178), G: A3 (163), B: C5 (197), A: CC (204)
10110010
10100011
11000101
11001100
which is -1297889844
Color storage
Components retrieval
Red value = (color >> 24) & 255
Green value = (color >> 16) & 255
Blue value = (color >> 8) & 255
Alpha value = color & 255
Flags storage
Collection of boolean values
Can be replaced with a single int to store up to 32 flags
Each boolean take 1 byte in memory
Activate a bit
Deactivate a bit
Get a bit value
value | (1 << bitIndex)
value & ~(1 << bitIndex)
value & (1 << bitIndex)