Private Static int bitsperword = 32;
Private Static int shift = 5;
Private Static int mask = 0x1f;
Private Static int n= 99999999;
Static int [] A = new int [1 + N/bitsperword];
Static void set (int I ){
A [I> shift] | = (1 <(I & Mask ));
}
Static int get (int I ){
Return (A [I> shift] & (1 <(I & Mask)> I;
}
Static void Init (int I ){
A [I> shift ~ (1 <(I & Mask ));
}
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
For (INT I = 0; I <n; I ++ ){
Init (I );
}
Set (0 );
Set (1 );
Set (2 );
System. Out. println (get (0 ));
System. Out. println (get (1 ));
System. Out. println (get (2 ));
}
A [I> shift] is equivalent to I/32
1 <(I & Mask) is equivalent
Two Values & the operation is to set the specified number of digits to 1
Bit-map description
Input 4
00000000000000000000000000010000
Input 2
00000000000000000000000000010100
Input 3
00000000000000000000000000011100
Input 1
00000000000000000000000000011110
Is relative position 1
Advantage: the query speed is high and the memory usage is small. It is suitable for determining whether or not the query exists and removing duplicates.
Disadvantage: it cannot be moved repeatedly.