Bit-map JAVA Implementation

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.