Data structure of the bitmap (bitmap) detailed _c language

Source: Internet
Author: User

1. Overview

Bitmap (bitmap) is a very common structure, which is widely used in indexes, data compression and so on. This paper introduces the realization method of bitmap and its application scene.

2. Bitmap implementation

(1) Realize
In the bitmap, each element is "0" or "1", indicating that its corresponding element does not exist or exists.

Copy Code code as follows:

#define Int_bits sizeof (INT)

#define SHIFT 5//2^5=32

#define MASK 0x1f//2^5=32

#define MAX 1024*1024*1024//max number

int bitmap[max/int_bits];

/*

* Set the first bit

* I >> shift is equivalent to I/(2 ^ shift),

* I&mask equivalent to mod operation M MoD n operation

*/

void set (int i) {

Bitmap[i >> SHIFT] |= 1 << (i & MASK);

}

Get the first bit

int test (int i) {

return bitmap[i >> SHIFT] & (1 << (I & MASK));

}

Clear the first I

int clear (int i) {

return bitmap[i >> SHIFT] & ~ (1 << (I & MASK));

}

(2) Function library implementation

C + + STL has bitmap class, it provides a lot of methods, see: http://www.cplusplus.com/reference/stl/bitset/

3. Bitmap Application

3.1 Enumeration
(1) Full combination
String-Full-combination enumerations (for strings of length n, with 2^n types), such as: abcdef, you can construct a mapping relationship from a string to a binary, and complete sorting by enumerating the binaries.

Copy Code code as follows:

Null--> 000000
F--> 000001
E--> 000010
Ef--> 000011
......
Abcedf--> 111111

(2) Hamilton distance

Enumeration algorithm, the complexity is O (n^2), how to reduce the complexity of it?
If it is n two-dimensional points, then how can we find out in a quicker way

With simple mathematical deformations, we can get this mathematical formula:

By observation, we find that each symbol of the same element must be reversed, such as: x_i-y_i, so we have a binary idea, that is to enumerate the x-axis y-axis of these two-i-dimensional points, so that we can use the binary form of a 0~3 number to represent the positive sign of each element, 1 means + Number, 0 for − number, such as: 2 for the bits form of 10 indicates x_i-y_i. So we can record the values of the different symbols of these two tuples by 2^2*n, and the different expressions for each binary only need to record their value, so that we only need max_i and min_i the same binary expression Max_i–min_i, Finally, we can solve the ans=max{max_i-min_i}.

With bitmaps, the algorithm time complexity can be O (N).

3.2 Search

When designing search pruning, you need to save historical information that has been searched, and in some cases, you can use bitmaps to reduce the space occupied by historical information data.

3.3 Compression

(1) Find a distinct integer in 250 million integers, note that there is not enough memory to accommodate the 250 million integers?

(2) Tencent interview questions: give 4 billion unsigned int integer, not ordered, and then give a number, how to quickly determine whether the number in the 4 billion number?

4. Summary

Bitmap is a very simple and fast data structure that can be optimized for the storage space and speed of the card (without the need for space to change time).

5. Reference materials
(1) "C implementation bitmap Bitmap": http://www.jb51.net/article/54438.htm
(2) Wussen on "0" and "1" in the contest of informatics

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.