Application of formulas and examples for bit operations

Source: Internet
Author: User
Tags bitwise bitwise operators

Reproduced in this article: http://topic.csdn.net/u/20080626/20/59a05c26-acb3-4d74-a153-711ce3a664ff.html

Application of formulas and examples for bit operations

Application formula for bitwise operation
Clear 0 to use with, a location one available or
To reverse and exchange, gently loosen the different or
Shift Operations
Point 1 They are both binocular operators, and two of the operands are plastic, and the result is plastic.
2 "<<" left: Fill 0 on the right vacated position, the left bit will be squeezed from the head, the value of which is equal to 2.
3 ">>" Move right: the right side is squeezed out. For an empty space to be left out, if it is a positive number, the vacancy is 0, and a negative number may be 0 or 1, depending on the computer system used.
4 ">>>" operator, the right side is squeezed out, for the left to remove the empty space to fill up 0.
Application of bitwise operators (source operand s mask mask)
(1) Bitwise AND--&
1 Clear 0 Special location (mask 0, other bit 1,s=s&mask)
2 Take a number of the middle finger positioning (mask specific position 1, the other bit 0,s=s&mask)
(2) Bitwise OR--|
It is often used to place the source operand in some position 1 and the other bits unchanged. (1 in mask, other bits 0 s=s|mask)
(3) A bit different or--^
1 to reverse the value of a particular bit (a specific position in the Mask 1, the other bit 0 s=s^mask)
2 without introducing the third variable, exchange the value of two variables (set A=A1,B=B1)
Status after Target action 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 Operation formula:
-X = ~x + 1 = ~ (x-1)
~x =-x-1
-(~x) = X+1
~ (x) = X-1
X+y = x-~y-1 = (x|y) + (x&y)
XY = x + ~y + 1 = (x|~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|~y) & ((x^y) |~ (y-x))
x< y: (~x&y) | ((~x|y) & (x-y)/unsigned x,y comparison
X<=y: (~x|y) & ((x^y) |~ (y-x))//unsigned x,y comparison
Application examples
(1) to determine whether an int variable A is odd or even
a&1 = 0 Even
a&1 = 1 Odd
(2) Take the K-bit (k=0,1,2......sizeof (int)) of the int type variable A, i.e. a>>k&1
(3) The K-position of the int type variable A is 0, namely a=a&~ (1&LT;&LT;K)
(4) A K position 1, or a=a|, of the INT type variable A (1<<k)
(5) INT-type variable cycle left K-time, that is a=a<<k|a>>16-k (set sizeof (int) =16)
(6) INT-type variable a cycle right shift k, that is a=a>>k|a<<16-k (set sizeof (int) =16)
(7) Average of integers
For two integer x,y, if you use (x+y)/2 to mean an overflow, because the x+y may be greater than Int_max, but we know that their average value is definitely not overflow, we use the following algorithm:
int average (int x, int y)//Returns the average of x,y
{
Return (X&y) + ((x^y) >>1);
}
(8) to determine whether an integer is a power of 2, for a number x >= 0, to determine whether he is a power of 2
Boolean power2 (int x)
{
Return ((x& (x-1)) ==0) && (x!=0);
}
(9) Do not exchange two integers without temp
void swap (int x, int y)
{
x ^= y;
Y ^= x;
x ^= y;
}
(10) Calculating absolute value
int abs (int x)
{
int y;
y = x >> 31;
Return (x^y)-y; Or: (x+y) ^y
}
(11) Conversion of modulo operation into bit operation (without overflow)
A% (2^n) equivalent to A & (2^n-1)
(12) The multiplication operation transforms into the bit operation (in the case that does not produce overflow)
A * (2^n) is equivalent to a<< n
(13) Conversion of division operations into bit operations (without overflow)
A/(2^n) is equivalent to a>> n
Example: 12/8 = = 12>>3
(a)% 2 is equivalent to A & 1
(a) if (x = = a) x= b;
else x= A;
Equivalent to x= a ^ b ^ x;
(x) The opposite number is expressed as (~x+1)


Instance

function | Example | Bit operations
----------------------+---------------------------+--------------------
Get rid of the last one | (101101->10110) | X >> 1
At the end add a 0 | (101101->1011010) | X << 1
At the end add a 1 | (101101->1011011) | X << 1+1
Turn the last one into 1 | (101100->101101) | x | 1
Turn the last one into 0 | (101101->101100) | x | 1-1
The last one to take the counter | (101101->101100) | x ^ 1
Turn the right number of k digits into 1 | (101001->101101,k=3) | x | (1 << (k-1))
Turn the right number of k digits into 0 | (101101->101001,k=3) | X & ~ (1 << (k-1))
Right number k position counter | (101001->101101,k=3) | x ^ (1 << (k-1))
Take the last three places | (1101101->101) | X & 7
Take the last K-position | (1101101->1101,k=5) | X & ((1 << k)-1)

Take the right number k position | (1101101->1,k=4) | x >> (k-1) & 1

Turn the last K position into 1 | (101001->101111,k=4) | x | (1 << k-1)
End K Position Counter | (101001->100110,k=4) | x ^ (1 << k-1)
Turn the right 1 to 0 | (100101111->100100000) | X & (X+1)
Turn right first 0 into 1 | (100101111->100111111) | x | (x+1)
Turn the right 0 to 1 | (11011000->11011111) | x | (x-1)
Take the right continuous 1 | (100101111->1111) | (x ^ (x+1)) >> 1
Take off right first 1 on the left | (100101000->1000) | X & (x ^ (x-1))
Judging Odd (x&1) ==1
Judge even (x&1) ==0

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.