嵌入式系统总是要用户对变量或寄存器进行位操作。给定一个整型变量a,写两段代码,第一个设置a的bit 3,第二个清除a 的bit 3。在以上两个操作中,要保持其它位不变。
#include <stdio.h>
#include <stdlib.h>
#define BIT3 (0x1<<3)
#include <stdlib.h>
#define BIT3 (0x1<<3)
static int a = 0;
static int b = 0;
int set_bit3(int a)
{
a |= BIT3;
return a;
}
int clear_bit3(int b)
{
b &= ~BIT3;
return b;
static int b = 0;
int set_bit3(int a)
{
a |= BIT3;
return a;
}
int clear_bit3(int b)
{
b &= ~BIT3;
return b;
}
int main()
{
static int b = 0;
static int c = 0;
int clear_bit3(int b);
int set_bit3();
//printf("Please input b: ");
//scanf("%x",&b);
//b=clear_bit3(b);
/*a &= ~BIT3;*/
printf("please input c: ");
scanf("%d", &c);
c = set_bit3();
//printf("after clear bit3=%x ", b);
printf("after set bit3=%x ", c);
int main()
{
static int b = 0;
static int c = 0;
int clear_bit3(int b);
int set_bit3();
//printf("Please input b: ");
//scanf("%x",&b);
//b=clear_bit3(b);
/*a &= ~BIT3;*/
printf("please input c: ");
scanf("%d", &c);
c = set_bit3();
//printf("after clear bit3=%x ", b);
printf("after set bit3=%x ", c);
return 0;
}