Inspired by this article. Neat tricks for speeding up integer computations.
Note: cin.sync_with_stdio(false);
disables synchronous IO and gives you a performance boost.
If used, you should only use cin
for reading input
(don't use both cin
and scanf
when sync is disabled, for example)
or you will get unexpected results.
x = x << 1; // x = x * 2
x = x << 6; // x = x * 64
x = x >> 1; // x = x / 2
x = x >> 3; // x = x / 8
a ^= b; // int temp = b
b ^= a; // b = a
a ^= b; // a = temp
i = -~i; // i++
i = ~-i; // i--
i = ~i + 1; // or
i = (i ^ -1) + 1; // i = -i
x = 131 & (4 - 1); // x = 131 % 4
(i & 1) == 0; // (i % 2) == 0
(a^b) == 0; // a == b
x < 0 ? -x : x; // abs(x)
(x ^ (x >> 31)) - (x >> 31) // abs(x)
a ^ b >= 0; // a * b > 0
(x + 0.5) >> 0; // round(x)
(x + 1) >> 0; // ceil(x)
x >> 0; // floor(x)
Thanks