Bitmap Method
Bitmap is a method that describes a set logically.
For example, if the set S is {2, 4, 1, 5, 12}, it is described as 0110 1100 0000 1000 in a bitmap. Two bytes can be used to describe S, and the left side is the low-level bit. If you use bitset <16> for storage, {[15], [14],... [1], [0] }={ 0001000000110110 }.
After describing a set with bitmap, it is easy to perform set operations, such as intersection, sum, and difference.
The following describes the specific operations.
Set S = {1, 2, 4, 5}, set T = {2, 5, 8, 10}
The bitmap of the Set S is 0110110000000000.
The bitmap of the set T is 0010010010100000.
The intersection of S and T is S & T = 0010010000000000 =}
Returns the Union of S and T, that is, S | T = 0110110010100000 =}
Calculate the difference set between S and T, that is, S &~ T = (0110110000000000) & (1101101101011111) = 0100100000000000 =}
The complete code for the above example is as follows:
# Include <iostream> # include <bitset> using namespace STD; int main () {cout <"------ bitmap method --- by David ---" <Endl; int s [] = {1, 2, 4, 5}; int T [] = {2, 5, 8, 10}; bitset <16> S, T; S. reset (); T. reset (); int size_s, size_t, I; size_s = sizeof (s)/sizeof (INT); size_t = sizeof (T)/sizeof (INT ); cout <"set S" <Endl; for (I = 0; I <size_s; I ++) {cout <s [I] <"; S. set (s [I]);} cout <Endl; cout <"set T" <Endl; For (I = 0; I <size_t; I ++) {cout <t [I] <""; T. set (T [I]);} cout <Endl; // calculates the intersection bitset <16> r1 (S. to_ulong () & T. to_ulong (); // returns the Union bitset <16> R2 (S. to_ulong () | T. to_ulong (); // returns the bitset of the difference set <16> R3 (S. to_ulong ()&(~ T. to_ulong (); cout <"intersection" <Endl; for (I = 0; I <16; I ++) if (R1 [I]) cout <I <"; cout <Endl; cout <" union "<Endl; for (I = 0; I <16; I ++) if (R2 [I]) cout <I <"; cout <Endl; cout <" difference set "<Endl; for (I = 0; I <16; I ++) if (R3 [I]) cout <I <""; cout <Endl; System ("pause"); Return 0 ;}Run
Column directory
- Data structures and algorithms
- C pointer