- Bitwise Integer Operations (int)
Get the maximum integer
int maxInt = ~(1 << 31); int maxInt = (1 << 31) - 1; int maxInt = (1 << -1) - 1;
Get the minimum integer
int minInt = 1 << 31; int minInt = 1 << -1;
Get maximum length
long maxLong = ((long)1 << 127) - 1;
Multiplied by 2
n << 1;
Divided by 2
n >> 1; // n/2
Multiplied by the mth power of 2
n << m;
Divided by m-th degree 2
n >> m;
Check odd number
(n & 1) == 1;
Exchange of two values
a ^= b; b ^= a; a ^= b;
Get absolute value
(n ^ (n >> 31)) - (n >> 31);
Get a maximum of two values
b & ((ab) >> 31) | a & (~(ab) >> 31);
Get a minimum of two values
a & ((ab) >> 31) | b & (~(ab) >> 31);
Check if both have the same sign
(x ^ y) >= 0;
Compute 2 ^ n
2 << (n-1);
Is factorial 2
n > 0 ? (n & (n - 1)) == 0 : false;
Modulo 2 ^ n vs m
m & (n - 1);
Get average
(x + y) >> 1; ((x ^ y) >> 1) + (x & y);
Get the mth bit n (low to high)
(n >> (m-1)) & 1;
Set the mth bit of n to 0 (low to high)
n & ~(1 << (m-1));
n + 1
-~n
n - 1
~-n
Get contrast
~n + 1; (n ^ -1) + 1;
if (x == a) x = b; if (x == b) x = a;
x = a ^ b ^ x;
Mohasin Ali Nov 25 '14 at 10:10 2014-11-25 10:10
source share