When you see such masks - remember these key rules for bitwise arithmetic
Assume X is unknown, & is bitwise AND, | is bitwise OR
- X & 1 = X
- X & 0 = 0
- X | 0 = X
- X | 1 = 1
Now look at the mask and its operation. In this case
v7 | 0x8000000000000000LL
The mask
In [1]: 0x8000000000000000 == 1 << 63
Out[1]: True
So in this case a 64 bit number - with the most significant bit (MSB) set and all other 0s
index - 63 62 61 ... 2 1 0
bits - 1 0 0 ... 0 0 0
The operation here is |
- the bitwise OR
We don't know v7
- we can assume it to be X. For individual bits X_i
for i
index
index - 63 62 61 ... 2 1 0
bitsX - X_63 X_62 X_61 ... X_2 X_1 X_0
When bitwise OR is done -
index - 63 62 61 ... 2 1 0
bitsX - X_63 X_62 X_61 ... X_2 X_1 X_0
mask - 1 0 0 ... 0 0 0
With the rules mentioned above - we can operate X | 1 = 1
index - 63 62 61 ... 2 1 0
bitsX - X_63 X_62 X_61 ... X_2 X_1 X_0
mask - 1 0 0 ... 0 0 0
result- 1
for the rest unknown bits X | 0 = X
index - 63 62 61 ... 2 1 0
bitsX - X_63 X_62 X_61 ... X_2 X_1 X_0
mask - 1 0 0 ... 0 0 0
result- 1 X_62 X_61 ... X_2 X_1 X_0
We can see that all of the bits of result
is the same as v7
except the MSB which is now 1.
Therefore, v7 | 0x8000000000000000LL
sets the MSB of v7
Similarly for v7 & 0x7FFFFFFFFFFFFFFFLL
The mask is 0x7FFFFFFFFFFFFFFFLL, which has all bits set to 1 except for the most significant bit (MSB), which is 0.
index - 63 62 61 ... 2 1 0
bits - 0 1 1 ... 1 1 1
We want to perform the bitwise AND operation between v7
and the mask.
index - 63 62 61 ... 2 1 0
bitsX - X_63 X_62 X_61 ... X_2 X_1 X_0
When we perform the bitwise AND operation:
For each bit position, we apply the rule X & 1 = X
and X & 0 = 0
index - 63 62 61 ... 2 1 0
bitsX - X_63 X_62 X_61 ... X_2 X_1 X_0
mask - 0 1 1 ... 1 1 1
result- 0 X_62 X_61 ... X_2 X_1 X_0
The result retains all bits of v7
except the MSB, which is now 0. Therefore, v7 & 0x7FFFFFFFFFFFFFFFLL
clears the MSB of v7