Bit operation tips

Source: Internet
Author: User
Check whether an unsigned number is not 2 ^ N-1 (^ is a power): X & (x + 1)

Change the rightmost 0 to 1: X | (x + 1)

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 | ~ 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) // comparison of unsigned X and Y
X <= Y:
(~ X | Y) & (x ^ y) | ~ (Y-x) // unsigned x, y comparison

No branch code using bitwise operations:

Calculate absolute value
Int ABS (int x)
{
Int y;
Y = x> 31
;
Return (x ^ y)-y; // or: (x + y) ^ y
}

Symbol function: Sign (x) =-1, x <0; 0, x = 0; 1, x> 0
Int sign (int
X)
{
Return (x> 31) | (unsigned (-x)> 31
; // X =-2 ^ 31 failure (^ power)
}

Comparison of three values: CMP (x, y) =-1, x <Y; 0, x = y; 1, x> Y
Int CMP (int x, int y
)
{
Return (x> Y)-(x-y );
}

Doz = x-y, x> = y; 0, x <Y
Int doz (int x, int y)
{
Int d
;
D = x-y;
Return D &((~ (D ^ (x ^ y) & (d ^ X)> 31)
;
}

Int max (int x, int y)
{
Int m;
M = (x-y)> 31;

Return Y & M | X &~ M;
}

Do not use third-party exchange X, Y:
1. x ^ = y; y ^ = x; x ^ = y;
2. x = x + y; y = x-y; X = x-y
;
3. x = x-y; y = Y + X; X = Y-X;
4. x = Y-X; X = x + y;

Double value exchange: x = A, x = B; B, x = A // The regular encoding is X = x =? B:;
1. x = a + B-X;
2. x =
A ^ B ^ X;

The following is a multiple of the K power of 2:
1. X &
(-1) <K)
2. (unsigned) x)> K) <K
Top Rounding:
1. t =
(1 <k)-1; X = (x + T )&~ T;
2. t = (-1) <K; X = (x-t-1) & T
;

Count the number of 1 bits:
1.
Int POP (unsigned X)
{
X =
X-(x> 1) & 0x55555555 );
X = (X & 0x33333333) + (x> 2)
& 0x33333333 );
X = (x + (x> 4) & 0x0f0f0f0f;
X = x
+ (X> 8 );
X = x + (x> 16 );
Return X & 0x0000003f
;
}
2.
Int POP (unsigned X ){
Static char table [256] =,
1, 2, 3,..., 6, 7, 7, 8 };
Return
Table [X & 0xff] + Table [(x> 8) & 0xff] + Table [(x> 16) & 0xff] + Table [(x> 24)]
;
}

Parity computing:
X = x ^ (x> 1 );
X = x ^ (x> 2 );
X = x ^ (
X> 4 );
X = x ^ (x> 8 );
X = x ^ (x> 16)
;
The result is located in the X percentile bit. For the unsigned X, the I-bit of the result is the parity between the I-bit and the leftmost bits of the original number.

 

Bit inversion:
Unsigned Rev (unsigned X)
{
X = (X & 0x55555555) <1
| (X> 1) & 0x55555555;
X = (X & 0x33333333) <2 |
(X> 2) & 0x33333333;
X = (X & 0x0f0f0f) <4 |
(X> 4) & 0x0f0f0f;
X = (x <24) |
(X & 0xff00) <8) | (x> 8) & 0xff00) | (x> 24 );

Return X;
}

Number of reverse incremental bits:
Unsigned inc_r (unsigned X)
{
Unsigned m = 0x80000000
;
X ^ = m;
If (INT) x> = 0)
Do {M> = 1; x
^ = M;} while (x <m );
Return X;
}

Mixed Position Selection:
ABCD efgh ijkl mnop-> AABB ccdd eeff gghh iijj
Kkll mmnn oopp
Unsigned PS (unsigned X)
{
Unsigned T;
T = (x
^ (X> 8) & 0x0000ff00; X = x ^ t ^ (T <8 );
T = (x ^
(X> 4) & 0x00f000f0; X = x ^ t ^ (T <4 );
T = (x ^
(X> 2) & 0x0c0c0c0c; X = x ^ t ^ (T <2 );
T = (x ^
(X> 1) & 0x22222222; X = x ^ t ^ (T <1 );
Return x
;
}

Bit compression:
Select and right shift the 1-bit corresponding to the mask M, for example, compress (abcdefgh, 01010101) = 0000 bdfh
The compress_left (x, m) operation is similar, but the result is on the left:
Bdfh133.
Unsigned compress (unsigned X, unsigned m)
{
Unsigned MK,
MP, MV, T;
Int I;

X & = m;
Mk = ~ M <1;
For (I = 0; I <5;
++ I ){
MP = Mk ^ (MK <1 );
MP ^ = (MP <2
);
MP ^ = (MP <4 );
MP ^ = (MP <8)
;
MP ^ = (MP <16 );
MV = MP & M;

M = m ^ MV | (mv> (1 <I ));
T = x & mv
;
X = x ^ t | (T> (1 <I ));
Mk = Mk
&~ MP;
}
Return X;
}

Bit replacement:
A 32-5-digit array is used to represent the target position of a bit starting from the decimal bit. The result is a 32*5 Bit matrix,
Place the matrix along the diagonal line and store it with 5 32-bit P [5] characters.
Round (x, m)
= Compress_left (x, m) | compress (x ,~ M );
Preparations:
Void Init (unsigned * P)
{
P [1] = forward (P [1], p [0]);
P [2] = forward (Forward (P [2], p [0]), P [1])
;
P [3] = percentile (P [3], p [0]), P [1]), P [2]);
P [4] = random (
Round (round (P [4], p [0]), P [1]), P [2]), P [3]);
}
Actual replacement:
Int rep (
Unsigned X ){
X = percentile (x, P [0]);
X = round (x, P [1]);
X =
Round (x, P [2]);
X = round (x, P [3]);
X = round (x, P [4]);
Return x
;
}

Conversion from binary code to Gray code:
Unsigned B2G (unsigned B)
{
Return B ^
(B> 1 );
}
Gray to binary:
Unsigned g2b (unsigned g)
{

Unsigned B;
B = G ^ (G> 1 );
B = G ^ (G> 2 );

B = G ^ (G> 4 );
B = G ^ (G> 8 );
B = G ^
(G> 16 );
Return B;
}

Find the position of the leftmost 0 byte:
Int zbytel (unsigned X)
{
Static CAHR table [16] = {
};
Unsigned y;
Y =
(X & 0x7f7f7f7f) + 0x7f7f7f7f;
Y = ~ (Y | x | 0x7f7f7f7f );
Return
Table [y * 0x00204081> 28]; // Multiplication can be performed by shift and addition.
}

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.