forked from AhmadElsagheer/Competitive-programming-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bitmask.java
28 lines (15 loc) · 808 Bytes
/
Bitmask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package data_structures.linear;
public class Bitmask {
static int setBit(int S, int j) { return S | 1 << j; }
static int clearBit(int S, int j) { return S & ~(1 << j); }
static int toggleBit(int S, int j) { return S ^ 1 << j; }
static boolean isOn(int S, int j) { return (S & 1 << j) != 0; }
static int turnOnLastZero(int S) { return S | S + 1; }
static int turnOnLastConsecutiveZeroes(int S) { return S | S - 1; }
static int turnOffLastBit(int S) { return S & S - 1; }
static int turnOffLastConsecutiveBits(int S) { return S & S + 1; }
static int lowBit(int S) { return S & -S; }
static int setAll(int N) { return (1 << N) - 1; }
static int modulo(int S, int N) { return (S & N - 1); } //S%N, N is a power of 2
static boolean isPowerOfTwo(int S) { return (S & S - 1) == 0; }
}