[Switch] After reading this bit operation, you will learn about it.

Source: Internet
Author: User
Tags bitwise operators
Bit operation application tips

Use or
To reverse and exchange, you can easily use an exception or
Shift operation
Key Aspect 1: They are binary operators, both of which are integers, and the result is also an integer.
2 "<" Left shift: Fill 0 on the right blank bit. The left bit will be squeezed out from the header, and its value is equal to 2.
3 ">" right shift: The right bit is squeezed out. For the vacant positions removed from the left, if it is a positive number, the vacant space is supplemented with 0. If it is a negative number, it may be supplemented with 0 or 1, depending on the computer system used.
4 ">>>" operator, the right bit is squeezed out, and 0 is added for the left removed space.
Application of bitwise operators (source operand s mask)
(1) bitwise AND --&
1. Reset the special location (0 for a specific position in the mask, 1 for other bits, S = S & Mask)
2. Locate a specified position in a specified number (1 in mask, 0 for other bits, S = S & Mask)
(2) bitwise OR -- least
It is often used to set the source operand to some positions of 1, and the other BITs remain unchanged. (The specific position of the mask is 1, and the other bit is 0 s = s ¦ mask)
(3) bitwise OR -- ^
1. Reverse the value of the specific location (1 in a specific position in mask and 0 s = s ^ mask in other bits)
2. If the third variable is not introduced, the values of the two variables are exchanged (set a = A1, B = B1)
Status after Operation
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
Binary complement calculation formula:
-X = ~ X 1 = ~ X-1)
~ X =-X-1
-(~ X) = x 1
~ (-X) = X-1
X y = x -~ Y-1 = (x ¦ y) (X & Y)
X-y = x ~ Y 1 = (x bytes ~ Y )-(~ X & Y)
X ^ y = (x ¦ y)-(X & Y)
X ¦ y = (X &~ Y) y
X & Y = (~ X ¦ y )-~ X
X = Y :~ (X-y ¦ y-x)
X! = Y: x-y ¦ y-x
X <Y: (x-y) ^ (x ^ y) & (x-y) ^ X ))
X <= Y: (x bytes ~ Y) & (x ^ y) returns ~ (Y-x ))
X <Y :(~ X & Y) equals ((~ X ¦ y) & (x-y) // unsigned x, y comparison
X <= Y :(~ X ¦ y) & (x ^ y) ¦ ~ (Y-x) // unsigned x, y comparison
Application Example
(1) judge whether int variable A is an odd or even number.
A & 1 = 0 even
A & 1 = 1 odd
(2) Take the K-bit (k =, 2…) of int-type variable ...... Sizeof (INT), that is, a> K & 1
(3) Clear the K bit of int variable A by 0, that is, a = &~ (1 <K)
(4) Place the K position of int type variable A 1, that is, a = a round (1 <K)
(5) int-type variable loops shift K to the left, that is, A = A <k then a> 16-k (set sizeof (INT) = 16)
(6) int type variable A shifts K times to the right, that is, a = A> K then a <16-k (set sizeof (INT) = 16)
(7) Average integer
For two integers x and y, if the average value is calculated using (x y)/2, the overflow will occur, because x y may be greater than int_max, however, we know that their average values will certainly not overflow. We use the following algorithm:
Int average (int x, int y) // returns the average value of X and Y.
{
Return (X & Y) (x ^ y)> 1 );
}
(8) judge whether an integer is the power of 2. For a number x> = 0, determine whether it is the power of 2.
Boolean power2 (int x)
{
Return (X & (x-1) = 0) & (X! = 0 );
}
(9) two integers are not exchanged using temp.
Void swap (int x, int y)
{
X ^ = y;
Y ^ = X;
X ^ = y;
}
(10) Calculate the absolute value
Int ABS (int x)
{
Int y;
Y = x> 31;
Return (x ^ y)-y; // or: (x Y) ^ y
}
(11) modulo operation into bitwise operation (without Overflow)
A % (2 ^ N) is equivalent to a & (2 ^ n-1)
(12) multiplication is converted into bitwise operations (without Overflow)
A * (2 ^ N) is equivalent to a <n
(13) Division operations are converted into bitwise operations (without Overflow)
A/(2 ^ N) is equivalent to a> N
For example: 12/8 = 12> 3
(14) A % 2 is equivalent to a & 1
(15) if (x = A) x = B;
Else x =;
It is equivalent to X = a ^ B ^ X;
(16) The opposite number of X is expressed (~ X 1)


Instance

Function alias example bitwise operation
---------------------------------------------------------------------
Remove the last percentile (101101-> 10110) percentile x> 1
Add 0 rows (101101-> 1011010) until x <1
Add a 1st percentile (101101-> 1011011) percentile x <1 1
Change the last digit to 1 second (101100-> 101101) second X second 1
Change the last digit to 0 percentile (101101-> 101100) percentile x percentile 1-1
Last bitwise round (101101-> 101100) returns x ^ 1
Change the right K-digit to 1 percentile (101001-> 101101, K = 3) percentile x percentile (1 <(k-1 ))
Change the right K-digit to 0 percentile (101101-> 101001, K = 3) percentile X &~ (1 <(k-1 ))
Right K-bit anti-round (101001-> 101101, K = 3) Round x ^ (1 <(k-1 ))
Take the last three digits (1101101-> 101) into X & 7
Take the last K-bit percentile (1101101-> 1101, K = 5) percentile X & (1 <k)-1)

Take the K-digit percentile (1101101-> 1, K = 4) percentile x> (k-1) & 1

Change the last K bit to 1 percentile (101001-> 101111, K = 4) percentile x percentile (1 <k-1)
Last K bit anti-round (101001-> 100110, K = 4) Round x ^ (1 <k-1)
Change 1 on the right to 0 degrees (100101111-> 100100000) degrees X & (x 1)
Change the first 0 from the right to 1 percentile (100101111-> 100111111) percentile x percentile (x 1)
Change 0 on the right to 1 percentile (11011000-> 11011111) percentile x percentile (x-1)
Take 1 consecutive percentile (100101111-> 1111) percentile (x ^ (x 1)> 1
Remove the left margin (100101000-> 1000) margin X & (x ^ (x-1) of the first 1 from the right ))
Returns an odd number (X & 1) = 1.
Returns an even number (X & 1) = 0.

For example, calculate the total number of values from X (high) to Y (low ).

Public static int findchessnum (int x, int y, ushort K)
{
Int Re = 0;
For (INT I = y; I <= x; I)
{
Re = (k> (I-1) & 1 );
}
Return re;
}

 

[Switch] After reading this bit operation, you will learn about it.

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.