Bit operating skills

This article posted on the personal blog of game programmers Liu, long-term update, please indicate the source address https://www.cnblogs.com/xiaohutu/p/10951911.html

You need to add or modify please leave a message ~

1. Take bit n value (from the right)

int r = (v >> n) & 1;

2. a position 1 (from the right)

v = v | (1 << n);

3. Place a bit negated (from the right)

v = v ^ (1 << n);

4. Finally, a 1 is set to 0

v = v & (v - 1);

5. Finally, a 0 is set to 1

v = v | (v + 1 );

6. Analyzing parity

(v & 1)

7. calculating an absolute value

int a = v >> 31;
int r = (v ^ a) - a;
或者 int r = (v + a) ^ a;

8. modulo

// V% (2 ^ n) can be converted into an n-bit value before removing 
int MOD & V = ( 2 ^ n - . 1 );

9 by 2 ^ n

int r = v << n;

10. In addition to n-th power of 2

int r = v >> n;

 11. In addition to more than 2

int r = v & 1;

12. Analyzing statement v == a b: a;?

v = a ^ b ^ v;

 13.a and b swap

void swap(int& x , int& y)
{
     x ^= y;
     y ^= x;
     x ^= y;
}

14. N is a power of 2,

bool power2(int v)
{
    return ((v & (v - 1)) == 0) && (v != 0);
}

15, averaging integer

int average(int x, int y)
{    
    return (x & y) + ((x ^ y) >> 1);
}

16. the sign of v

int V;
 int Sign;
 // -1 or 0 
Sign = - (V < 0 ); 
Sign = V >> ( the sizeof ( int ) * CHAR_BIT - . 1 );
 // -1 or + 1'd 
Sign = + . 1 | (v >> ( sizeof ( int ) * CHAR_BIT - 1 );
 // returns -1, 0, 
Sign = (v =! 0 ) | - ( int ) ((unsigned int ) (( int ) v)> > ( the sizeof ( int ) * CHAR_BIT - . 1 )); 
Sign = (V =!0) | (v >> (sizeof(int) * CHAR_BIT - 1));
sign = (v > 0) - (v < 0);

17. The contrast is determined whether the two symbols

bool f = ((a ^ b) < 0);

18. no branch statement min or max function

//min(a,b)
int r = b ^ ((a ^ b) & -(a < b));
//max(a,b)
int r = a ^ ((a ^ b) & -(a < b));

19. Whether negated (without judgment statement)

bool negative;
int r = (v ^ -negative) + negative;

The combined mask 20. The mask a and b, the bit mask value b is 1, whereas a value of

unsigned int r = a ^ ((a ^ b) & mask);

21. Calculation of the binary number n 1 of v

unsigned int v;
unsigned int n;
for (n = 0; v; n++)
  v &= v - 1

22. The binary bit-reversed

unsigned int
reverse(register unsigned int x)
{
    x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
    x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
    x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
    x = (((x & 0xff00ff00) >> 8) | ((x &) <<0x00ff00ff8));
    return((x >> 16) | (x << 16));
}

22. Reverse 2 bits

unsigned int
reverse(register unsigned int x)
{
  register unsigned int y = 0x55555555;
  x = (((x >> 1) & y) | ((x & y) << 1));
  y = 0x33333333;
  x = (((x >> 2) & y) | ((x & y) << 2));
  y = 0x0f0f0f0f;
  x = (((x >> 4) & y) | ((x & y) << 4));
  y = 0x00ff00ff;
  x = (((x >> 8) & y) | ((x & y) << 8));
  return((x >> 16) | (x << 16));
}

23. The floating-point determination with 0

#define FasI(f)  (*((int *) &(f)))
#define FasUI(f) (*((unsigned int *) &(f)))
#define lt0(f)    (FasUI(f) > 0x80000000U)
#define le0(f)    (FasI(f) <= 0)
#define gt0(f)    (FasI(f) > 0)
#define ge0(f)    (FasUI(f) <= 0x80000000U)

24. Next a power of 2

unsigned int nextpowerof2(register unsigned int x)
{
  x |= (x >> 1);
  x |= (x >> 2);
  x |= (x >> 4);
  x |= (x >> 8);
  x |= (x >> 16);
  return(x+1);
}

24. The most significant bit reserved (on a power of a 2) 1

unsigned int lastpowerof2(register unsigned int x)
{
  x |= (x >> 1);
  x |= (x >> 2);
  x |= (x >> 4);
  x |= (x >> 8);
  x |= (x >> 16);
  return(x & ~(x >> 1));
}

 

Guess you like

Origin www.cnblogs.com/xiaohutu/p/10951911.html
Recommended