Bitwise operations Summary (bitwise AND, OR, XOR)

Source: Internet
Author: User
Tags arithmetic bitwise
Bitwise -AND Operator (&)

Participate in the operation of the two data, press bits for "and" operation.

Arithmetic Rules:0&0=0;   0&1=0;    1&0=0; 1&1=1;

That is, the two-bit is "1" at the same time, the result is "1", otherwise 0

For example: 3&5 is 0000 0011& 0000 0101 = 00000001 Therefore, 3&5 is worth 1.

In addition, negative numbers participate in bitwise and arithmetic in the form of complement.

Special use of "with operations":

(1) Clear zero. If you want to clear a unit to zero, even if all of its bits is 0, as long as the value with a everyone is zero and the result is zero.

(2) Take a number to locate the middle finger

Method: Find a number, corresponding to the x to take the bit, the corresponding bit of the number is 1, the rest of the bits are zero, this number and X for "and operations" can be obtained by means of X-position.

Example: Set x=10101110,

Take the lower 4 bits of x, which can be obtained by x & 0000 1111 = 00001110;

It can also be used to take 2, 4, 6 bits of x.

bitwise OR operator (|)

To participate in the operation of the two objects, press bits for "or" operation.

Operation rules: 0|0=0; 0|1=1; 1|0=1; 1|1=1;

That is, the two objects that participate in the operation have a value of 1 and 1.

Example: 3|5 is 00000011 | 0000 0101 = 00000111 Therefore, the 3|5 is worth 7.

In addition, negative numbers participate in a bitwise OR operation in the complement form.

"OR operation" special effect:

(1) often used for some position of a data 1.

Method: Find a number, corresponding to the x to set 1 bits, the corresponding bit of the number is 1, the remaining bits are zero. This number is in X-phase or can make some position in X 1.

Example: Lower 4 position of x=10100000 1, with X | 0000 1111 = 1010 1111 can be obtained.

xor operator (^)

To participate in the operation of the two data, press bits for "XOR" operation.

Operation rules: 0^0=0; 0^1=1; 1^0=1; 1^1=0;

That is: The two objects participating in the operation, if two corresponding bits are "XOR" (the value is different), then the bit result is 1, otherwise 0.

Special effect of "XOR operation":

(1) to make a specific bit flip to find a number, corresponding to the X to flip you, the corresponding bit of the number is 1, the rest of the bit is zero, this number and x corresponding bit XOR.

Example: x=10101110, the X low 4-bit flip, with x ^0000 1111 = 1010 0001 can be obtained.

(2) differ from 0 or, retain the original value, X ^ 00000000 = 1010 1110.

The following emphasis is on the bitwise XOR, XOR is actually not carrying the addition, such as 1+1=0,,0+0=0,1+0=1.

Several properties of XOR:

1. Exchange Law

2, the Binding law (ie (a^b) ^c = = a^ (b^c))

3, for any number x, there are x^x=0,x^0=x 4, reflexivity: a^b^b=a^0=a;

XOR is most common in polynomial division, but its most important nature is reflexivity: A xor b XOR B = A, that is, a given number a, with the same arithmetic factor (B) for two different or operation, still get A itself. This is a magical nature that takes advantage of this nature and can get a lot of interesting applications. For example, all program textbooks will point out to beginners that to exchange the values of two variables, you must introduce an intermediate variable. However, if you use XOR, you can save a variable's storage space: With a A, a, a two variable, the stored values are a-B, the following three-line expression will interchange their value expression (value):

A=a^b;

B=b^a;

A=a^b;

application Example 1:

1-1000 is placed in an array of 1001 elements, and only one element value is duplicated, and the others appear only
A. Each array element can only be accessed once, and an algorithm is designed to find it; no secondary storage empty
, can design an algorithm implementation. Solution One, it is obvious that someone has proposed a more wonderful solution, adding up all the numbers, minus the 1+2+...+1000.
This algorithm is perfect enough to believe that the answer to the question is the algorithm, the only problem is that if the sequence is too large, it may lead to overflow.
Solution Two, XOR is not the problem, and performance is better.
All the numbers are different or, the results obtained are different from the results of the 1^2^3^...^1000, and the result is the number of repetitions.

Application Example 2 (General & and ^):(topics Link: http://gdutcode.sinaapp.com/problem.php?cid=1051&pid=7)

In a series of numbers, all but two numbers appear two times, seeking two numbers, and outputting them in order from small to large. For example 2 2 1 1 3 4. The final output is 3 and 4.

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
#define N 1000010
int a[n];

int main ()
{
    //freopen ("why.in", "R", stdin);
    Freopen ("Why.out", "w", stdout);
    int t;
    scanf ("%d", &t);
    while (t--) {
        int n;
        scanf ("%d", &n);
        int x = 0;
        for (int i = 1; I <= n; i++) {
            scanf ("%d", &a[i]); x ^= a[i];
        }
        int NUM1 = 0, num2 = 0;
        int tmp = 1;
        while (!) ( TMP & x)) TMP <<= 1;
		cout<<tmp<<endl;
        for (int i = 1; I <= n; i++) {
            if (tmp & A[i]) num1 ^= a[i];
            else num2 ^= a[i];
        }
        printf ("%d%d\n", min (Num1, num2), Max (NUM1, num2));
    }
    return 0;
}
This problem is the first time in the input has been different or, you can put the two number of different or the product to find out, such as the above example x=3^4;

Then find a variable TMP to separate the two numbers. Bitwise AND the words can be found to separate these two numbers exist in NUM1 and num2 respectively. And then there's the result.


left shift operator (<<)

Move each bits of an operand to the left several bits (left bits discarded, 0 on the right).

Example: a = a<< 2 moves the bits of a to the left 2 bits, 0 to the right,

Move left 1 digits after a = A;

If the left-hand side does not contain 1, then each left-shift is equal to the number multiplied by 2. Right shift operator (>>)

All bits of a number are shifted to the right by a number of digits, positive left 0, negative left 1, and right discard.

The operand is shifted one bit to the right, which is equal to the number divided by 2.

For example: a = a>> 2 shifts the bits of a to the right 2 bits,

Left complement 0 or 1 depends on whether the number of shifts is positive or negative.

Compound assignment operator

The bitwise operators are combined with the assignment operators to form a new compound assignment operator, which is:

&= Example: a &=b equivalent to a=a& b

|= Example: a |=b equivalent to A=a |b

>>= Example: a >>=b equivalent to a=a>> b

<<= Example: a<<=b equivalent to a=a<< b

^= Example: a ^= b equals a=a^ b

The arithmetic rules are similar to the arithmetic rules of the compound assignment operators mentioned earlier. bit operations with different lengths of data

If two different lengths of data are bit-calculated , the system aligns the two to the right and then the bitwise Operation .

Take the "and" operation as an example: we know that in C, the long type is 4 bytes, int is 2 bytes, if a long data with an int data "and" operation, right-side alignment, the left side of the insufficient bit according to the following three cases to complement,

(1) If the integer data is positive, the left side is 16 0.

(2) If the integer data is negative, the left side is 16 1.

(3) If the shaping data is an unsigned number, the left side also complements 16 0.

such as: Long A=123;int b=1; calculate a& B.

such as: Long A=123;int b=-1; calculate a& B.

such as: Long a=123;unsigned intb=1; calculate A & B.

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.