qfedu-c-level/day5/homework/h13.c

34 lines
777 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 请编程,实现以下操作: 定义unsigned char 型变量a赋值0x7f
// 将其第1、5、7位置12、3、6位清0其它位保持不变。
#include <stdio.h>
void printBinary(char num)
{
int size = sizeof(num);
for (int i = size * 8 - 1; i >= 0; i--)
{
int bit = (num >> i) & 1;
printf("%d", bit);
}
printf("\n");
}
int main()
{
unsigned char a = 0x7f; // 十六进制
printf("原样输出: ");
printBinary(a);
a |= 0b1010001; // 0b 表示二进制数的标识符
printf("1、5、7位置1输出: ");
printBinary(a);
a &= ~0b100110;
printf("2、3、6位清0输出: ");
printBinary(a);
printf("结果的十六进制输出: ");
printf("0x%hhx\n", a); // 0x59
return 0;
}