C Language Common Position operation (place 0 or set 1) __c language

Source: Internet
Author: User
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

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.