2015.2.26

Source: Internet
Author: User
1. CC ++ bit operations: 6 operators with the first glance: And, or |, different or ^, not ~, Right Shift, left shift priority: Bit operation | ^ ~ % 1.1 consistent with the operation rule: 111100010000 and multiplication example: 1133 is used: (1). If it is cleared, you only need to find one.

1. The C/C ++ bit operations first look at the six operators: And, or |, exclusive or ^, not ~, Right Shift, left shift priority: Bit operation | ^ ~ % 1.1 and calculation rules: 1 1 = 1 1 0 = 0 0 1 = 0 0 0 = 0 // example of the same operation as multiplication: 113 = 3 that is 00001011 00000011 = 00000011 = 3 use: (1 ). you only need to find one for resetting.

1. A glimpse of C/C ++ operations
Six major bitwise operators: And &, or |, XOR or ^, not ~, Right Shift>, left shift <
Priority: Bit operation & | <>>^ ~ %
1.1 & Operation
Calculation rules:
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0 // same as Multiplication
Example:
11 & 3 = 3
That is, 00001011 & 00000011
= 00000011 = 3
Application:
(1). Reset
You only need to find a binary number, where each bit meets the following conditions:

The original number is 1, and the corresponding digit in the new number is 0.

Then perform the & Operation on the two to achieve the goal of clearing.
Example:
5 (00000101) & 250 (11111010) = 0
5 (00000101) & 2 (00000010) = 0
Yes, and the operation can clear the finger position.

(2) locate certain indicators in a number
If you have an integer a (2 byte) and want to take the lower byte, you only need to bitwise a and 8 1.
A 00101100 10101100
B 00000000 11111111
C 00000000 10101100
A & B = c, to retain the low position only.
(3) Retention refers to positioning:
Perform the bitwise AND operation with a number. This number is equal to 1.
For example, if you want to retain the numbers 84, I .e. 01010100 (2) from the left, the numbers 3, 4, 5, 7, and 8 are as follows:
01010100 (00111011) & 00010000 () =)
(4) determine whether it can be divided by 2:
If (a & 1 = 0) is equivalent to if (a % 2 = 0 );
Likewise:
If (a & 2 = 0) is equivalent to if (a % 4 = 0 );
If (a & 4 = 0) is equivalent to if (a % 8 = 0 );...

1.2 | or operation
Calculation rules:
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0
There is 1 to 1, and no 1 to 0.
Example:
11 | 3 = 11
That is, 00001011 | 00000011 = 00001011 = 11
Application:
You can use a number to record multiple boolean values.

1. 3. ^ exclusive or operation
Calculation rules:
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
The value ranges from 1 to 0.
Application:
(1). Find the singular number:
Given an array of integers, every element appears twice should t for one. Find that single one.
In an array, all numbers appear twice, and only one
For example, int t = {1, 2, 3, 2, 1, 5} needs to locate 5.
It is perfect to use an exclusive OR, and all the same things will disappear, leaving 5 behind.
Maybe it's a pair of jealousy.

for( int i = 0 ; a[i] != '\0' ; i++) a[0] ^= a[i];return a[0];

(2). Data exchange without the temp value

// Do not use temp to swap two integers void swap (int & x, int & y) {x ^ = y; y ^ = x; x ^ = y ;}

1.4 ~ Non-operation
It can only be used for data flip:
~ Y =-y-1

1.5 Shift Operator
(1). Left shift:
Promotion: with sufficient space:

(2). Right Shift:
Int n = n/2 is equivalent to int n = n> 1 is equivalent to int n> = 1
M times n/= 2 is equivalent to n> m;

(3). Additional Words
100% 8 = 100-math. floor (100/8) * 8 = 100-(100> 3) <3)

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.