How to place 0 or 1.
Method One: Write a macro, easy to transplant
#define SETBIT (X,y) x|= (1<<y)//x Y position 1
#define CLRBIT (X,y) x&=~ (1<<y)//x Y-Position 0 Method Two: C language bit operation in addition to improve operational efficiency, in embedded system programming, it's another of the most typical applications, And very widely is being used is the bit and (&), or (|), not (~) operation, which has a great relationship with the programming characteristics of embedded systems. We usually have a bit setting on the hardware registers for example, we set the 6th bit of the interrupt mask control register for the Am186er 80186 processor to 0 (open Interrupt 2), the most common practice is:
#define Int_i2_mask 0x0040
Wtemp = Inword (Int_mask);
Outword (Int_mask, wtemp &~int_i2_mask);
Setting this bit to 1 is a way to:
#define Int_i2_mask 0x0040
Wtemp = Inword (Int_mask);
Outword (Int_mask, Wtemp | Int_i2_mask);
The practice of determining whether the bit is 1 is:
#define Int_i2_mask 0x0040
Wtemp = Inword (int_mask);
if (Wtemp & Int_i2_mask)
{
...
} method Three: int a|= (1<<X)//x is a number that needs to be placed 1, such as the fourth position 1 is: a|= (1<<4)
int b&=~ (1<<X)//Place 0 x=x|0x0100& nbsp; //the third place 1
x=x&0x1011 //the third place 0 #define BITGET (number,pos) ( Number) >> (POS) &1)//using a macro to get a bit of a certain
#define Bitget (Number,pos) ((numbers) |= 1<< (POS))//place 1
#defi Ne bitget (number,pos) ((number) &= ~ (1<< (POS))//Place a location 0
#define Bitget (Number,pos) ((number) ^= 1<< (p OS)///To take the POS bit of number counter typical operations are:
Wtcon |= (1 << 5)//wtcon fifth bit 1
Wtcon &= ~ (1 << 5)//WTC On the fifth-bit clear 0
The above method is very common in embedded system programming, we need to firmly grasp.
Reprint from http://blog.sina.com.cn/s/blog_5f33242b0100ywcz.html