Bitwise operation--Bitwise AND (&) operation--(fast modulo algorithm)

Source: Internet
Author: User
Tags modulus

Because bit arithmetic operates directly on memory data, it does not need to be converted to decimal, so processing is very fast.

Bitwise-and (Bitwise and), Arithmetic symbols &

The result of a&b operation: the corresponding bit in A and B is also 1, then the corresponding result bit is 1,

For example:

10010001101000101011001111000

& 111111100000000

---------------------------------------------

10101100000000

to 10101100000000 to move right 8 bit got 101011. , this gets a the 8~15 bit of a mask. So according to this revelation, determine whether an integer is in the 0-65535 (common cross-border judgment):

The general (a >= 0) && (a <= 65535) may be judged two times.

Use a bitwise operation only once:

A & ~ ((1<< 16)-1)

The subsequent constants are even better at compile time. In fact, as long as the logic and on the line.

Common techniques:

1. Parity judgment for integers

An integer A, a & 1 This expression can be used to determine the parity of a. The last digit of the binary is 0 for even, and the lowest is 1 for odd. Using a%2 to determine parity is the same as a & 1, but A & 1 is much faster.

2, determine whether n is a positive integer of 2 emerges

(! (n& (n-1))) && N

As an example:

If n = + 10000, n-1 = 1111

So:

10000

& 1111

----------

0

One more example: if n = 100000000, n-1 = 11111111

So:

100000000

&11111111

--------------

0

Good! After reading the above two small examples, I believe we all have a perceptual understanding. Theoretically, if a number A is a positive integer power of 2, then A's binary form must be 1000 ... (there are 0 or more 0 behind), then the conclusion is obvious.

3. Count the number of 1 in n

The simple statistical method is: first to determine the parity of n, for odd when the counter increases by 1, and then move N to the right one, repeat the above steps until the shift is complete.

The simple statistical approach is relatively straightforward, so let's look at a more advanced approach.

For example, consider a 2-bit integer n=11, there are 2 1, the first to extract the inside of the even digit 10, odd digit 01, the even number of bits to the right 1 bits, and then add to the odd digits, because each pair of odd and even bits added and will not exceed the "two-bit", so every two bits in the result holds the number of 1 in N; =0111, first with "one" for the unit to do odd even bit extraction, then the even bit shift (1 bits to the right), add, and then the "two-bit" as the unit to do the odd-even extraction, even bit shift (then need to move 2 bits), add, because there is no parity bit and will not exceed the "four-bit", so the result holds the number of n 1, By analogy, you can get more algorithms for bit N. The whole idea is similar to the Division method.
Here, by the way, the commonly used binary number:

0xaaaaaaaa=10101010101010101010101010101010

0x55555555 = 1010101010101010101010101010101(Odd bit is 1, odd-even bit is extracted in 1-bit units)

0xCCCCCCCC = 11001100110011001100110011001100

0x33333333 = 110011001100110011001100110011(extraction of parity bits in "2-bit" units)

0xf0f0f0f0 = 11110000111100001111000011110000

0x0f0f0f0f = 1111000011110000111100001111 (the parity bit is extracted in "8-bit" units)

0xffff0000 =11111111111111110000000000000000

0X0000FFFF = 1111111111111111 (the parity bit is extracted in "16-bit" units)

For example, the number of 1 of 32-bit unsigned numbers can be as follows:

Intcount_one (unsignedlong N)
{
0xaaaaaaaa,0x55555555 the odd and even bits in "1-bit" units respectively.
n = ((n & 0xAAAAAAAA) >>1) + (n & 0x55555555);

0xcccccccc,0x33333333 the odd and even bits in "2-bit" units respectively.
n = ((n & 0xCCCCCCCC) >>2) + (n & 0x33333333);

0xf0f0f0f0,0x0f0f0f0f the odd and even bits in "4-bit" units respectively.
n = ((n & 0xf0f0f0f0) >>4) + (n & 0x0f0f0f0f);

0xff00ff00,0x00ff00ff the odd and even bits in "8-bit" units respectively.
n = ((n & 0xff00ff00) >>8) + (n & 0x00ff00ff);

0xffff0000,0x0000ffff the odd and even bits in "16-bit" units respectively.
n = ((n & 0xffff0000) >>16) + (n & 0x0000ffff);

RETURNN;
For example, for example, my birthday is the lunar calendar.2Month One, just use211, turn it into binary:

N =11010011

Calculate n = ((n &0xaaaaaaaa) >> 1) + (n& 0x55555555);

Get n = 10010010

Calculate n = ((n &0XCCCCCCCC) >> 2) + (n& 0x33333333);

Get n = 00110010

Calculate n = ((n &0xf0f0f0f0) >> 4) + (n& 0x0f0f0f0f);

Get n = 00000101-----------------à can no longer be divided, then 5 is the answer.

4, for the positive integer modulo operation (note, negative numbers can not be so calculated)

First, the more simple:

Multiplication method is very time-consuming, as long as the logarithm left one is multiplied by 2, the right one is divided by 2, the legend uses the bit operation efficiency to improve the 60%.

Multiply 2^k well-known:n<<k. So you will be silly to knock 2566*4 result 10264 ? The direct 2566<<4 is done, fast and accurate.

Except 2^k well-known:n>>k.

What about mod2^k ? (Modulo the multiples of 2 )

n& ((1<<k)-1)

The use of popular words to describe is , a multiple of 2 modulo, as long as the number and 2 of Multiples -1 do the bitwise AND operation.

Good! Easy to understand, let's give an example.

Think: If the result is to require modulo 2^k , do we really need to take the mold every time?

It is easy to think of a fast power-to-modulus method.

Fast power-take modulus algorithm

Often do the problem when you encounter to calculate a^b modc Situation, this time, a careless on the TLE . So how do we solve this problem? Bit arithmetic to help you.

First introduce the Qin Jiushao algorithm:( Numerical analysis is very clear )

Rewrite an n-th polynomial f (x) =a[n]x^n+a[n-1]x^ (n-1) +......+a[1]x+a[0] into the following form:

F (x) =a[n]x^n+a[n-1]x^ (n-1)) +......+a[1]x+a[0]

= (a[n]x^ (n-1) +a[n-1]x^ (n-2) +......+a[1]) x+a[0]

= ((a[n]x^ (n-2) +a[n-1]x^ (n-3) +......+a[2]) x+a[1]) x+a[0]

=. .....

=(...... ((A[n]x+a[n-1]) x+a[n-2]) x+......+a[1]) x+a[0].

When you find the value of a polynomial, first calculate the value of the first polynomial within the inner parentheses, i.e.

V[1]=A[N]X+A[N-1]

The values of the polynomial are then computed by the inward and outer layers, i.e.

V[2]=v[1]x+a[n-2]

V[3]=v[2]x+a[n-3]

......

V[N]=V[N-1]X+A[0]

In this way, the value of the n-th polynomial f (x) is converted to the value of the N-once polynomial.

Good! With the basics in front, let's start solving the problem.

by (a xb) MoD c= ((a modc) x b) mod c.

We can represent the achievement of B First:

B = a[t]x 2^t +a[t-1]x2^ (t-1) + ... + a[0]x 2^0. ( a[i]=[0,1]).

So we are by a^b mod c = (a^ (a[t] x 2^t +a[t-1]x2^(t-1)+...a[0] x 2^0) modc.

However we ask a^ (2^ (i+1)) mod c= ((a^ (2^i)) mod c) ^2 mod C. Obtained.

The specific implementation is as follows:

Fast power-mode algorithm using Qin Jiushao algorithm, simple and beautiful

Fast calculation (a ^ p)% m value
__int64fastm (__int64 A, __int64 p, __int64 m)
{
if (p = = 0) return 1;
__int64 r =a% m;
__int64 k = 1;
while (P > 1)
{
if ((P & 1)!=0)
{
K = (k * r)% m;
}
R = (R * r)% m;
P >>=1;
}
Return (R * k)% M;
}

http://acm.pku.edu.cn/JudgeOnline/problem?id=3070

5. Calculation mask

For example, a mask that intercepts a low 6 -bit:0x3f
This is indicated by the bitwise operation:(1<< 6)-1
This is also very good to read the mask, because the number of bits of the mask is directly reflected in the expression.

The bitwise OR operation is simple, as long as the corresponding bit in A and B appears 1, then the corresponding bit of a|b is 1. It's not much to say.

6. Subsets

Enumerates a subset of a set. Sets the original set to mask, the following code can list all its subsets:

Bitwise operation--Bitwise AND (&) operation--(fast modulo algorithm)

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.