# [Programming Pearl] How to Use bit logic to implement bit Vectors

Source: Internet
Author: User

The question at the beginning of programming is as follows:

How to Use bitwise logical operations (such as And, Or, shift) to implement bitwise vectors?

1. What is a bit vector?

In many cases (for example, if an object meets or does not meet a certain nature), it is sufficient to use a binary bit to represent an object. However, you cannot use a variable name to directly represent a single bit (there is no single-bit data type ). Therefore, it is considered that Multiple Digits form a basic data type, and the bit usage method is achieved through operations on this basic data type. At the same time, for convenience, the basic data types composed of BITs are composed of arrays, so that a certain range of BIT data sets can be operated. We call this form of data structure a bit vector.

Next, let's take another note. operations on the bitwise vector cannot be accessed by name, but can only be operated by bit, that is, the number of data to be operated. In our opinion, the bits are sequential from 0 to n. In fact, Because we store the bits with arrays of basic data types, these bits are separated into different array elements, in different positions of different array elements. Assume that we use the int type as the basic data type. In an int type (c ++), we can store 32 digits. For a specific bit (position: POS), we first consider which int array element it is located in (POS/32, that is, knowing which array element it is located ), then, consider the position (Pos mod 32) where the bit is in the array element ).

There are three major operations on the counterpoint vector: To locate specific position 1, to locate specific location 0, to determine specific location.

II. Implementation Code

With the above knowledge, let's take a look at the implementation code for this problem in programming.

`1 # define bitsperword 32 // The basic data type used is 32 bits, int type 2 # define shift 5 // It depends on the array element in which the bit is located. 3 # define mask 0x1f // It depends on the position in the array element. 4 # define n 10000000/ /bit length 5 int A [1 + N/bitsperword]; // array length 6 7 // set the corresponding position 1 8 void set (int I) {9 A [I> shift] | = (1 <(I & Mask); 10} 11 12 // place the corresponding location 013 void CLR (int I) {14 A [I> shift] & = ~ (1 <(I & Mask); 15} 16 17 // judge the corresponding 18 void test (int I) {19 return a [I> shift] & (1 <(I & Mask); 20}`

The above code is analyzed below.

1) determine the array element, that is, I/32. For the division of multiples of 2, we can use the displacement operation, because 32 = 2 ^ 5, so I/32 = I> 5, so a [I> shift] determines the array element.

2) After the array element is determined, we need to determine the relative position of the bit, that is, I mod 32. Observe that when I is performing the right shift (5 bits) operation, eliminate the lower five bits, and the lower five bits are the remainder. Therefore, you can use I & 0x1f to obtain the lower five bits, and then obtain the relative position of the bits, for example, I & Mask.

3) after determining the position, we further convert it to the corresponding bit location mask, that is, 1 <(I & Mask ).

Through the above three steps, we have completed the positioning work.

Analyze three statements:

1) set (): because it is set to 1, you only need to perform or operate on the original array element and the bitwise mask;

2) CLR (): Because the operation is set to 0, the bitwise position mask is taken first, so that the specific position is 0, and then the original array element can be operated.

3) test (): to determine the specific location, you only need to perform and operate on the original array element and bit location mask to determine whether the current BIT is 1.

[Programming Pearl] How to Use bit logic to implement bit Vectors

Related Keywords:

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.