C language bit operation

Source: Internet
Author: User
Tags bitwise

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

    1. #define Bitget (Number,pos) ((number) |= 1<< (POS))//place a position 1
    2. #define Bitget (Number,pos) ((number) &= ~ (1<< (POS))//Position 0
    3. #define Bitget (Number,pos) ((number) >> (POS) &1))//Use a macro to get a bit of a number
    4. #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:

    1. #define SETBIT (x, y) x|= (1<<y)//x Y position 1
    2. #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

    1. #define ROTATE_LEFT (x, N) ((x) << (n)) | ((x) >> ((8 * sizeof (x))-(n)))
    2. #define Rotate_right (x, N) ((x) >> (n)) | ((x) << ((8 * sizeof (x))-(n)))

4. Calculate absolute Value

    1. int abs (int x)
    2. {
    3. int y;
    4. y = x>>31;
    5. Return (x^y)-y; Or: (x+y) ^y
    6. }
5. Judging the symbols of integers
    1. int sign (int x)
    2. {
    3. Return (X>>31) | (unsigned (x)) >>31;
    4. }
6. Two number comparison
    1. X==y: ~ (x-y|y-x)
    2. X!=y:x-y|y-x
    3. X<y: (x-y) ^ ((x^y) & ((x-y) ^x))
    4. X<=y: (X|~y) & ((x^y) |~ (y-x))
    5. X<y: (~x&y) | ((~x|y) & (x-y))//unsigned x, y comparison
    6. X<=y: (~x|y) & ((x^y) |~ (y-x))//unsigned x, y comparison
7. Exchange two-digit value (swap)
    1. 1.x ^= y; Y ^= x; x ^= y;
    2. 2.x = X+y; y = x-y; x = XY;
    3. 3.x = x-y; y = y+x; x = y-x;
    4. 4.x = Y-x; x = y-x; x = X+y;
8. Bit count
  1. Method One:
  2. int count (Long v)
  3. {
  4. int number = 0;
  5. while (v)
  6. {
  7. V &= (V-1);
  8. number++;
  9. }
  10. return number;
  11. }
  12. Method Two:
  13. int count (unsigned x)
  14. {
  15. × = X ((x>>1) &0x55555555);
  16. x = (x&0x33333333) + (x>>2) &0x33333333);
  17. x = (x+ (x>>4)) &0x0f0f0f0f;
  18. x = x+ (x>>8);
  19. x = x+ (x>>16);
  20. Return x&0x0000003f;
  21. }
9. Conversion of binary and gray codes
    1. (1). Conversion of binary code to Gray code:  
    2.   unsigned b2g (unsigned b )  
    3.   { 
    4.       return B ^  (b>>1)  ; 
    5.   } 
    6.    (2). Gray code to binary code: &NBSP;
    7.   unsigned g2b (unsigned G)  
    8.  {
    9.    & nbsp; unsigned b ; 
    10.      b = g ^  (G>>1)  ; 
    11.      b = g ^  (g>>2)  ; 
    12.      b = g ^  (g>>4)  ; 
    13.       b = g ^  (g>>8)  ; 
    14.      b = g ^   (g>>16)  ; 
    15.      return b ;
    16.   }
10. Bit reversal
    1. Unsigned rev (unsigned x)
    2. {
    3. x = (x & 0x55555555) << 1 | (x>>1) & 0x55555555;
    4. x = (x & 0x33333333) << 2 | (x>>2) & 0x33333333;
    5. x = (x & 0x0f0f0f0f) << 4 | (x>>4) & 0x0f0f0f0f;
    6. x = (x<<24) | ((x&0xff00) <<8) | ((x>>8) & 0xff00) | (x>>24);
    7. return x;
    8. }

C language bit operation

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.