leetcode-bit arithmetic

Source: Internet
Author: User

Bit operation can greatly reduce the complexity of the algorithm space, improve efficiency, very ingenious!

Let's talk about the simple use of bit arithmetic.

1. Bitwise AND &

Purpose: Zeroing, taking some of the points in a number positioning, preserving some of the points in a number of positioning

2. Bitwise OR |

Purpose: Assigns some bits of a number to 1

3. Bitwise XOR or ^

Purpose: Exchange two numbers

A=a^b; B=a^b; A=a^b

Let's take a look at the results of these three statements:

The first a=a^b, so the second b=a^b= (a^b) ^b=a^ (b^b) =a^0=a, now the value of B is a

The third a=a^b= (a^b) ^a=b^ (a^a) =b^0=b, now the value of a is B! Complete the interchange!

4. Take Counter ~

5. Move left <<, right >>

The greatest advantage of bit operation is simple operation and high efficiency. You can see the following example:

1. Suppose that a variable has the following properties {input, output, type, length (if it is an array, if not an array of length 0)}, how do you represent this variable?

Use a struct to represent these attributes? Even with a class? Hey, in fact, only with an int type is OK. An int has 32 bits, enough that we can arbitrarily set the position of the flag bit. For example, the 32nd bit represents the output, the 31st bit represents the input, 第17-24位 represents the type, and 第1-16位 represents the length. Well, if a variable is input (then 32 bits is 1), not output (then 31 bits is 0), type is char (assuming char corresponds to 1, then 17-24 bit is 00000001), length is 5, the following int type can represent all information:

int type= (1<<31) | (1<<16) |5

You can later obtain any information by bitwise and, for example, to get the length, you can type & 0X0000FFFF

2. Representation of the Chessboard:

What data structure can you choose to represent a chessboard? A two-dimensional array is definitely the first idea that most people come up with, but a two-dimensional array does not work as easily as it is imagined. If the chessboard is 8*8, we can actually use a 64-bit length variable to represent the chessboard. Each one corresponds to each of the squares of the chessboard. For traditional black-and-white games, you can use two variables: the first 64-bit variable records the checkerboard situation of a sunspot (if the [i][j] position of the chessboard has sunspots, the i-1 *8+j bit is 1, otherwise 0), and the second 64-bit variable records the checkerboard of the white. In this way, the judgment of the pieces, eat children, flip sub-operation can be used with, or, XOR or equal-bit operation is very convenient and efficient implementation. Some chess games have basic "chess" mode, and this mode can be recorded with another variable. This variable, as a template, can be used to perform bitwise operations with the original checkerboard variable to determine if the original chessboard has a particular chess pattern. If you use arrays to represent a chessboard, these operations can be much more complex.

And for chess such as board games, pieces of the type is not only a black and white, you should maintain a number of checkerboard variables, specifically, you can refer to the following links:

Http://www.elephantbase.net/computer/struct_bitboard.htm

1. Bitwise AND Operation bitwise AND operator "&" are binocular operators. Its function is to participate in the operation of the two number of the corresponding binary phase. Only the corresponding two binaries are 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in a complementary fashion.

For example: The 9&5 can be written as follows: 00001001 (9 twos complement) &00000101 (5 's twos complement) 00000001 (1 of the twos complement) is visible 9&5=1.

Bitwise AND operations are usually used to clear some bits by 0 or to preserve certain bits. For example, A's high eight bits clear 0, reserved low eight bits, can be used as a&255 operation (255 binary number is 0000000011111111).

Application:

A. Clear 0 special positioning (mask in the specific position 0, the other bit is 1,s=s&mask)
B. Take a number of the middle finger positioning (mask in a specific position 1, the other bit is 0,s=s&mask)

2. Bitwise OR operation bitwise OR operator "|" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase or. As long as the corresponding two binary has one for 1 o'clock, the result bit is 1. The two numbers participating in the operation are in complement.

For example: 9|5 can be written as follows:

00001001|00000101
00001101 (decimal 13) Visible 9|5=13

Application:

It is commonly used to set the source operand to some location 1, other bits unchanged. (Mask in a specific position 1, the other bit is 0 s=s|mask)

3. Bitwise XOR or the bitwise XOR operator "^" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary dissimilarity or, when the two corresponding binary differences, the result is 1. Participation in the arithmetic is still in complement, for example 9^5 can be written as follows:

00001001^00000101 00001100 (decimal 12)

Application:

A. Reverse the value of a specific bit (at a specific position in mask 1, the other bit is 0 s=s^mask)
B. Do not introduce a third variable, exchange the value of two variables (set A=A1,B=B1)
Target Operation Post-operation status
A=A1^B1 a=a^b A=A1^B1,B=B1
B=A1^B1^B1 b=a^b A=A1^B1,B=A1
A=B1^A1^A1 a=a^b A=B1,B=A1

4. Negation operation

Negation operator ~ is a single-mesh operator with right-associative. Its function is to reverse the bitwise negation of the number of participating operations. For example, the operation of the: ~ (0000000000001001) result is: 1111111111110110

5. Left-shift operation

The left-shift operator "<<" is the binocular operator. Its function to the left of the "<<" operand of all the binary all left a number of bits, the "<<" to the right of the number to specify the number of bits moved, high drop, low 0. Its value is equal to multiply by 2. For example: A<<4 refers to moving the binary of a to the left by 4 bits. such as a=00000011 (decimal 3), 00110000 (decimal 48) After moving left 4 bits.

6. Right-shift operation

The right-shift operator ">>" is the binocular operator. The function is to shift all the binary of the left operand of ">>" to the right of several bits, and the number to the right of ">>" to specify the number of bits to move. Its value is equal to except 2.

For example: Set a=15,a>>2 to move 000001111 right to 00000011 (decimal 3). For the left-hand exit, if it is a positive number, the vacancy is 0, if it is negative, it may be 0 or 1, depending on the computer system used. Move in 0 called logical right SHIFT, move in 1 called arithmetic right shift, Turbo C with logical right shift.

leetcode-bit arithmetic

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.