计算一个数二进制中1的个数

#include <stdio.h>
int main()
{
int num = 6;
int i = 0;
int count = 0; //计数
while (num)
{
count++;
num = num & (num - 1); //不建议想原理 带几个值进去试试
}
printf("二进制中1的个数 = %d\n", count);
return 0;
}

两数交换

#include <stdio.h>
int main()
{
int a = 10; //两数交换
int b = 20;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("a = %d b = %d\n", a, b);
return 0;
}

printf你真的了解吗

#include <stdio.h>
int main()
{
printf("%d",printf("%d",printf("%d",43))); //4321
return 0;
}

sizeof本质是操作符

#include <stdio.h>
int main()
{
int a = 1;
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof a); //单目操作符
return 0;
}

64位机器指针的大小

#include <stdio.h>
void test1(int arr[])
{
printf("%d\n", sizeof(arr)); //64位机器指针大小 8字节
}
void test2(char ch[])
{
printf("%d\n", sizeof(ch)); //64位机器指针大小 8字节
}
int main()
{
int arr[10] = {0};
char ch[10] = {0};
printf("%d\n", sizeof(arr)); // 40
printf("%d\n", sizeof(ch)); // 10
test1(arr);
test2(ch);
return 0;
}

&&和&

#include <stdio.h>
int main()
{
//c语言中&&为短路与,c语言中&为按位与,java中&&为逻辑与,java中&为短路与
int i = 0, a = 0, b = 2, c = 3, d = 4;
i = a++ && ++b && d++;
printf(" a = %d\n b = %d\n c = %d\n d = %d\n", a, b, c, d); //1 2 3 4

a = 0, b = 2, c = 3, d = 4;
i = a++||++b||d++;
printf(" a = %d\n b = %d\n d = %d\n", a, b, d);//1 3 4
return 0;
}

整型提升

int main()
{
char a = 0xb6;
short b = 0xb600;
int c = 0xb6000000; //整型提升规则:无符号数高位补0 有符号负数高位补1 有符号正数高位补0
if (a == 0xffffffb6) //运算时发生整型提升 运算后 按照原来的数据类型进行截断 保留低位 舍弃高位
printf("a");
if (b == 0xffffb600)
printf("b");
if (c == 0xb6000000)
printf("c");
return 0;
}
#include <stdio.h>
int main()
{
char a = 255;
unsigned char b = -1;
printf("a = %d\n", a); //a的有符号数的真值在-127到127 无符号数0到2^32-1
printf("a = %u\n", a);
printf("b = %u\n", b);
return 0;
}