In the C language, it is sometimes necessary to perform a bitwise operation, such as for some of these bits. The purpose of this is to achieve the goal, without affecting the other bits. The usual set-up operations are as follows:
#define SETBIT (x, y) x|= (1<<y)//x Y position 1
#define CLRBIT (x, y) x&=~ (1<<y)//The Y-position of X is clear 0
As an example:
int main (int argc, char* argv[])
{
unsigned char a = 0x55;
unsigned char b = a| (1<<1); First position 1
unsigned char c = a&~ (1<<2); Second position 0
printf ("Hello world! 0x%x,0x%x/n ", b,c);
return 0;
}
Output 0x57,0x51. 0x57 is from 01010101 to 01010111;0x51 i.e. from 01010101 to 01010001.
First, basic bit operation
| |
Or |
& |
And |
~ |
Take counter |
^ |
XOR or |
<< |
Move left |
>> |
Move right |
Common usage of bitwise operation
1. Get the value of a bit
- #define Bitget (Number,pos) ((number) |= 1<< (POS))//place a position 1
- #define Bitget (Number,pos) ((number) &= ~ (1<< (POS))//Position 0
- #define Bitget (Number,pos) ((number) >> (POS) &1))//Use a macro to get a bit of a number
- #define Bitget (Number,pos) ((number) ^= 1<< (POS))//reverse the POS bit of number
2. Set the value of a bit (set to 0 or 1)
Method One:
- #define SETBIT (x, y) x|= (1<<y)//x Y position 1
- #define CLRBIT (x, y) x&=~ (1<<y)//The Y-position of X is clear 0
Method Two:
Set 0, use 0 to go ' with '
int a|= (1<<X) x is a number that requires 1, such as fourth position 1: = "" a|= "(1<<4)
Set 1, use 1 to go ' or '
int a&=~ (1<<x) 0<= a position "p=" >
3. Cyclic shift
- #define ROTATE_LEFT (x, N) ((x) << (n)) | ((x) >> ((8 * sizeof (x))-(n)))
- #define Rotate_right (x, N) ((x) >> (n)) | ((x) << ((8 * sizeof (x))-(n)))
4. Calculate absolute Value
- int abs (int x)
- {
- int y;
- y = x>>31;
- Return (x^y)-y; Or: (x+y) ^y
- }
5. Judging the symbols of integers
- int sign (int x)
- {
- Return (X>>31) | (unsigned (x)) >>31;
- }
6. Two number comparison
- 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
7. Exchange two-digit value (swap)
- 1.x ^= y; Y ^= x; x ^= y;
- 2.x = X+y; y = x-y; x = XY;
- 3.x = x-y; y = y+x; x = y-x;
- 4.x = Y-x; x = y-x; x = X+y;
8. Bit count
- Method One:
- int count (Long v)
- {
- int number = 0;
- while (v)
- {
- V &= (V-1);
- number++;
- }
- return number;
- }
- Method Two:
- int count (unsigned 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;
- }
9. Conversion of binary and gray codes
- (1). Conversion of binary code to Gray code:
- unsigned b2g (unsigned b )
- {
- return B ^ (b>>1) ;
- }
- (2). Gray code to binary code: &NBSP;
- unsigned g2b (unsigned G)
- {
- & nbsp; unsigned b ;
- b = g ^ (G>>1) ;
- b = g ^ (g>>2) ;
- b = g ^ (g>>4) ;
- b = g ^ (g>>8) ;
- b = g ^ (g>>16) ;
- return b ;
- }
10. Bit reversal
- Unsigned rev (unsigned x)
- {
- x = (x & 0x55555555) << 1 | (x>>1) & 0x55555555;
- x = (x & 0x33333333) << 2 | (x>>2) & 0x33333333;
- x = (x & 0x0f0f0f0f) << 4 | (x>>4) & 0x0f0f0f0f;
- x = (x<<24) | ((x&0xff00) <<8) | ((x>>8) & 0xff00) | (x>>24);
- return x;
- }
C language bit operation