C-language implementation of bitmap bitmap code sharing _c language

Source: Internet
Author: User

In fact, we use each element to represent a 32-bit binary string so that the element retains information about the presence of 32 adjacent numbers, and the array range drops to 10000000/32. For example, for number 89256, due to 89256 mod 32=2789 ... 8, so we should place the 8th bit of the 32-bit string in a[2789] (from the low number) to 1.

#define WORD
#define SHIFT 5////Move 5 digits, the left is the equivalent of multiplying by 32, and the right is equivalent to dividing
by 32 #define MASK 0x1F//16 into the
#define N 10000000
int bitmap[1 + N/word];
 * * Position Function--with "|" operator, I&mask is equivalent to mod operation
 * M mod n operation, when n = 2 of the x power, m mod n = m& (n-1)
 /
void set (int i) {
 bitmap[i ;> SHIFT] |= (1 << (I & MASK));
}
/* Purge bit operation, with &~ operator/
void Clear (int i) {
 bitmap[i >> SHIFT] &= ~ (1 << (I & MASK));
} /
* Test bit operation with & operator/
int test (int i) {return
 bitmap[i >> SHIFT] & (1 << (I & MASK)) c19/>}

Implementation sort (cannot be repeated):

 int main (void) {FILE *in = fopen ("In.txt", "R");
 FILE *out = fopen ("OUT.txt", "w");
 if (in = = NULL | | out = = NULL) {exit (-1);
 int i = 0;
 int m;
 for (i = 0; i < N; i++) {Clear (i);
 while (!feof (in)) {fscanf (in, "%d", &m);
 printf ("%d/n", m);
 Set (m);
 printf ("Abnother");
  for (i = 0; i < N; i++) {if (Test (i)) {printf ("%d/n", I);
 fprintf (out, "%d/n", I);
 } fclose (in);
 Fclose (out);
return exit_success; }

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.