获取设置一个字节某一个位的数值
void print2(unsigned char a)
{
int r[8];
r[0] = ( a & (0x01 << 0)) >> 0; // 最低位
r[1] = ( a & (0x01 << 1)) >> 1;
r[2] = ( a & (0x01 << 2)) >> 2;
r[3] = ( a & (0x01 << 3)) >> 3;
r[4] = ( a & (0x01 << 4)) >> 4;
r[5] = ( a & (0x01 << 5)) >> 5;
r[6] = ( a & (0x01 << 6)) >> 6;
r[7] = ( a & (0x01 << 7)) >> 7; // 最高位
printf("%d %d %d %d %d %d %d %d\n",r[7],r[6],r[5],r[4],r[3],r[2],r[1],r[0]);
}
int main(void)
{
unsigned char a = 0xBF;
print2(a);
unsigned char b = a | (0x01 << 6); //set on
print2(b);
b = a | (0x01 << 2); //set off 1
unsigned char c = b ^ (0x01 << 2); //set off 2 ( 先set on 在 ^ )
print2(c);
}
output:
1 0 1 1 1 0 1 1 //0xBF
1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1