判断(1)-(4)打印?【sizeof】
void test1(int arr[])
{
printf("%d\n", sizeof(arr)); //(2) 4/8
}
void test2(char ch[])
{
printf("%d\n", sizeof(ch)); //(4) 4/8
}
int main()
{
int arr[10] = {0};
char ch[10] = {0};
printf("%d\n", sizeof(arr)); //(1) 40
printf("%d\n", sizeof(ch)); //(3) 10
test1(arr); //传参是首元素地址,指针的大小4/8
test2(ch); //传参是首元素地址,指针的大小4/8
return 0;
}
判断打印【&&、||、++】
int i = 0, a = 0, b = 2, c = 3, d = 4;
i = a++ && ++b && d++; //a++ 是后置++,先使用再++。a=0 为假,后面不管是什么都不算了,a++
printf(" a = %d\n b = %d\n c = %d\n d = %d\n", a, b, c, d); //1 2 3 4
int i = 0, a = 1, b = 2, c = 3, d = 4;
i = a++ && ++b && d++;//a=1为真,a++,++b是前置++,先加在使用,b=3为真,d=4为真,d++
printf(" a = %d\n b = %d\n c = %d\n d = %d\n", a, b, c, d); //2 3 3 5
int i = 0, a = 0, b = 2, c = 3, d = 4;
i = a++ || ++b || d++; //a=0为假,a++,b++,b=3为真,结束
printf(" a = %d\n b = %d\n c = %d\n d = %d\n", a, b, c, d); //1 3 3 4
int i = 0, a = 1, b = 2, c = 3, d = 4;
i = a++ || ++b || d++; //a=1为真,结束。a++
printf(" a = %d\n b = %d\n c = %d\n d = %d\n", a, b, c, d); //2 2 3 4
判断打印结果【整形提升】
实例1
//a,b要进行整形提升,但是c不需要整形提升 a,b整形提升之后,变成了负数
//c不发生整形提升,则表达式 c==0xb6000000 的结果是真
//0-9 a b c d e f
int main()
{
char a = 0xb6; //10110110
short b = 0xb600;
int c = 0xb6000000;
if (a == 0xb6)
printf("a");
if (b == 0xb600)
printf("b");
if (c == 0xb6000000)
printf("c"); //C
return 0;
}
实例2
//c只要参与表达式运算,就会发生整形提升,表达式 +c ,就会发生提升,所以 sizeof(+c) 是4个字节.
//表达式 -c 也会发生整形提升,所以 sizeof(-c) 是4个字节,但是 sizeof(c) ,就是1个字节
int main()
{
char c = 1;
printf("%u\n", sizeof(c)); //1
printf("%u\n", sizeof(+c)); //4
printf("%u\n", sizeof(!c)); //1
return 0;
}
注:%u打印无符号数
判断打印【sizeof】
#include <stdio.h>
int i;//全局变量-不初始化-默认是0。如果为局部变量为随机值
int main()
{
i--;
//-1
//10000000000000000000000000000001 - 原码
//11111111111111111111111111111110 - 反码(符号位不变,其余取反)
//11111111111111111111111111111111 - 补码 (反码+1)
//放在内存里是补码,但高位就不再是符号位,一个超大数
//因为sizeof 返回的是无符号数,所以将i也转变成无符号数,i的最高位将不是符号位,而是有效位
if (i > sizeof(i))//sizeof()-计算变量/类型所占内存的大小 >=0 无符号数
{
printf(">\n");//>
}
else
{
printf("<\n");
}
return 0;
}
判断打印【操作符优先级】
int a, b, c;
a = 5;
c = ++a;// c=6 a=6
b = ++c, c++, ++a, a++;//c=7 ,c=8 ,a=7,b=7,a=8
b += a++ + c;//a=8,c=8,b=7,b=7+8+8=23,a=9
printf("a = %d b = %d c = %d\n:", a, b, c);//9 23 8
统计二进制中1的个数【补码的二进制有几个1】
写一个函数返回参数二进制中1的个数
比如:15 00001111 4个1
- 主函数
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 0;
scanf("%d", &a);
//写一个函数求a的二进制(补码)表示中有几个1
int count = count_bit_one(a);
printf("count = %d\n", count);
//system("pause");//system库函数-执行系统命令-pause(暂停)
return 0;
}
-
count_bit_one 法一
//13 //000000000000000000000000000001101 //-1 //100000000000000000000000000000001 原码 //111111111111111111111111111111110 反码 //111111111111111111111111111111111 补码 int count_bit_one(unsigned int n) // 无符号,解决负数 { int count = 0; while (n) { if (n % 2 == 1) { count++; } n = n / 2; } return count; }
-
count_bit_one 法二
// -1 //111111111111111111111111111111111 补码 //000000000000000000000000000000001 int count_bit_one(int n) { int count = 0; int i = 0; for (i = 0; i < 32; i++) { if (((n >> i) & 1) == 1)// 每一位移到最低位,按位与1 { count++; } } return count; }
-
count_bit_one
法三(最佳,高效)//n = n&(n-1) //n //13 //1101 n count++ //1100 n-1 //1100 n count++ //1011 n-1 //1000 n count++ //0111 n-1 //0000 n 跳出 int count_bit_one(int n) { int count = 0; while (n) { n = n & (n - 1); count++; } return count; }
求二进制中不同位的个数
两个int (32位)的整数m和n的二进制表达中有多少个位(bit)不同?
输入例子:1999 2299
输出例子:7
#include <stdio.h>
int get_diff_bit(int m, int n)
{
int tmp = m^n;// ^ 按位异或同为0,相异为1
int count = 0;
//return count_bit_one(tmp);
while (tmp)
{
tmp = tmp&(tmp - 1);
count++;
}
return count;
}
int main()
{
int m = 0;
int n = 0;
scanf("%d%d", &m, &n);
int count = get_diff_bit(m, n);
printf("count = %d\n", count);
return 0;
}
打印二进制的奇数位和偶数位
获取一个整数二进制序列中所有的偶数位和奇数位,分别打印出二进制序列
#include <stdio.h>
//00000000000000000000000000001010
void print(int m)
{
int i = 0;
printf("奇数位:\n");
for (i = 30; i >= 0; i -= 2)
{
printf("%d ", (m >> i) & 1);
}
printf("\n");
printf("偶数位:\n");
for (i = 31; i >= 1; i -= 2)
{
printf("%d ", (m >> i) & 1);
}
printf("\n");
}
int main()
{
int m = 0;
scanf("%d", &m);
print(m);
return 0;
}
交换两个变量(不创建临时变量)
不允许创建临时变量,交换两个整数的内容
-
临时变量
int a = 3; int b = 5; int tmp = 0;//临时变量 printf("before: a=%d b=%d\n", a, b); tmp = a; a = b; b = tmp;
-
加减法-可能会溢出
int a = 3; int b = 5; a = a + b; //8 b = a - b; //3 a = a - b; //5
-
异或的方法
int a = 3; //011 int b = 5; //101 a = a^b; //110 b = a^b; //011 a = a^b; //101