初阶第五节-操作符详解

1.算数操作符

#include
int main()
{
	int a = 6 / 5;
	printf("%d\n", a);//1

	float a = 6 / 5;
	printf("%f\n", a);//1.000000

	float a = 6.0 / 5.0;//6/5.0
	printf("%f\n", a);//1.200000

	int a = 7 % 3;//取模操作符的两端必须是整数
	printf("%d\n", a);//1
	return 0;
}

2.移位操作符 

初阶第五节-操作符详解_第1张图片

//16进制
//0-9  10-15
//0-9  a-f

//10进制
//0-9

//8进制
//0-7

//2进制
//0-1

//整数的二进制表示有3种
//原码 反码 补码

//正整数的原码、反码、补码相同
//负整数的原码、反码、补码需要计算

//7符号位为0
//00000000000000000000000000000111 - 原码
//00000000000000000000000000000111 - 反码
//00000000000000000000000000000111 - 补码

//-7符号位为1
//10000000000000000000000000000111 - 原码
//11111111111111111111111111111000 - 反码(原码的符号位不变,其它位按位取反)
//11111111111111111111111111111001 - 补码(反码+1)

//整数在内存中存储的是补码

2.1左移操作符 

#include
int main()
{
	int a1 = 7;
	int a2 = -7;

	//把a的二进制位向左移动一位
	int b1 = a1 << 1;
	int b2 = a2 << 1;

	printf("a1 = %d\n", a1);//a = 7
	printf("b1 = %d\n", b1);//b = 14

	printf("a2 = %d\n", a2);//a = -7
	printf("b2 = %d\n", b2);//b = -14
	return 0;
}

初阶第五节-操作符详解_第2张图片

初阶第五节-操作符详解_第3张图片

2.2右移操作符  

#include
int main()
{
	int a1 = 7;
	int a2 = -7;

	//把a的二进制位向右移动一位
	int b1 = a1 >> 1;
	int b2 = a2 >> 1;

	printf("a1 = %d\n", a1);//a = 7
	printf("b1 = %d\n", b1);//b = 3

	printf("a2 = %d\n", a2);//a = -7
	printf("b2 = %d\n", b2);//b = -4
	return 0;
}

初阶第五节-操作符详解_第4张图片

初阶第五节-操作符详解_第5张图片

3.位操作符 

初阶第五节-操作符详解_第6张图片

#include
int main()
{
	int a = 3;
	int b = -5;	

	//& -  按(2进制)位与
	int c = a & b;
	printf("c = %d\n", c);//3
	//%d 意味着打印一个有符号的整数
	
	//00000000000000000000000000000011 - 3的补码
	//10000000000000000000000000000101 - -5的原码
	//11111111111111111111111111111010 - -5的反码
	//11111111111111111111111111111011 - -5的补码
	//00000000000000000000000000000011 - 3的补码
	//00000000000000000000000000000011 - a & b(二进制位相乘,只要有0则为0)
	
	//| - 按(2进制)位或
	int c = a | b;
	printf("%d\n", c);//-5

	//00000000000000000000000000000011 - 3的补码
	//10000000000000000000000000000101 - -5的原码
	//11111111111111111111111111111010 - -5的反码
	//11111111111111111111111111111011 - -5的补码
	//00000000000000000000000000000011 - 3的补码
	//11111111111111111111111111111011 - a | b(只要有1则为1)
	//11111111111111111111111111111010
	//10000000000000000000000000000101 - 原码

	//^  - 按(2进制)位异或
	//对应的二进制位进行异或
	//规则:相同为0,相异为1

	int c = a ^ b;
	printf("%d\n", c);//-8

	//00000000000000000000000000000011 - 3的补码
	//10000000000000000000000000000101 - -5的原码
	//11111111111111111111111111111010 - -5的反码
	//11111111111111111111111111111011 - -5的补码
	//00000000000000000000000000000011 - 3的补码
	//11111111111111111111111111111000 - a ^ b(相同为0,相异为1)
	//11111111111111111111111111110111
	//10000000000000000000000000001000 - 原码
	return 0;
}

3.1例题

//不能创建临时变量(第三个变量),实现两个数的交换
#include
int main()
{
	int a = 3;
	int b = 5;
	//交换
    
    //方法1
	//3^3 = (011)^(011) = 0 -> a^a = 0
	//0^3 = (000)^(011) = 3 -> 0^a = a
	//异或操作符支持交换律
	printf("a=%d b=%d\n", a, b);//a = 3 b = 5
	a = a ^ b;//3^5
	b = a ^ b;//3^5^5 = 3
	a = a ^ b;//3^5^3 = 5
	printf("a=%d b=%d\n", a, b);//a = 5 b = 3

    //方法2
	printf("a = %d b = %d\n", a, b);//a = 3 b = 5
	//数值太大会溢出
	a = a + b;//8
	b = a - b;//3
	a = a - b;//5
	printf("a = %d b = %d\n", a, b);//a = 5 b = 3

    //方法3
	int c = 0;//空瓶
	printf("a = %d b = %d\n", a, b);//a = 3 b = 5
	c = a;
	a = b;
	b = c;
	printf("a = %d b = %d\n", a, b);//a = 5 b = 3
	return 0;
}
//编写代码实现:求一个整数存储在内存中的二进制中1的个数
//求补码的二进制中1的个数

//int a = 3;
//a&1 可以求出a的第一位是否为1,然后将a右移一位
//00000000000000000000000000000011 - 3的补码
//00000000000000000000000000000001 - 1的补码
//00000000000000000000000000000001 

//代码1
#include 
int main()
{
	int num = 10;
    int count = 0;//计数
	while (num)
	{
		if (num % 2 == 1)
			count++;
		num = num / 2;
	}
	printf("二进制中1的个数 = %d\n", count);//二进制中1的个数 = 2
	return 0;
}


//代码2
#include 
int main()
{
	int num = -1;
	int i = 0;
	int count = 0;//计数
	for (i = 0; i < 32; i++)
	{
		if (num & (1 << i))
			count++;
	}
	printf("二进制中1的个数 = %d\n", count);//二进制中1的个数 = 32
	return 0;
}


//代码3
#include 
int main()
{
	int num = -1;
	int i = 0;
	int count = 0;//计数
	while (num)
	{
		count++;
		num = num & (num - 1);
	}
	printf("二进制中1的个数 = %d\n", count);//二进制中1的个数 = 32
	return 0;
}
//如何改变二进制中第几位的数

#include
int main()
{
	int a = 13;
	//把a的二进制中的第5位置成1
	a = a | (1 << 4);
	printf("a = %d\n", a);//a = 29

	//把a的二进制中的第5位置成0
	a = a & ~(1 << 4);
	printf("a = %d\n", a);//a = 13

	//00000000000000000000000000001101 - 13的补码
	//00000000000000000000000000010000 - 1<<4
	//00000000000000000000000000011101 - 29的补码	                        

	//00000000000000000000000000011101 - 29的补码	
	//11111111111111111111111111101111 - ~(1<<4)
	//00000000000000000000000000001101 - 13的补码
	return 0;
}

4. 赋值操作符 

//=  -- 赋值
//== -- 判断

int weight = 120;//体重
weight = 89;//不满意就赋值
double salary = 10000.0;
salary = 20000.0;//使用赋值操作符赋值

//赋值操作符可以连续使用,比如:
int a = 10;
int x = 0;
int y = 20;

a = x = y+1;//连续赋值
//等价于
x = y+1;
a = x;

 4.1复合赋值符

+= -= *=
/= %= <<=
>>= &= |=
^=
#include
int main()
{
	int a = 10;
	a = a + 100;
	a += 100;   

	a = a >> 3;
	a >>= 3;
	return 0;
}

5.单目操作符 

初阶第五节-操作符详解_第7张图片

5.1单目操作符介绍 

初阶第五节-操作符详解_第8张图片

// ! - 真的变成假,假的变成真
// C语言中,0表示假,非0表示真

#include
int main()
{
	int flag = 0;
	printf("%d\n", !flag);//1

	//flag为真,打印hehe
	if (flag) //0
	{
		printf("hehe\n");
	}
	//flag为假,打印haha
	if (!flag) //1
	{
		printf("haha\n");//haha
	}
	return 0;
}
//&操作符

#include
int main()
{
	int a = 10;
	printf("%p\n", &a);//a在内存中占了4个字节,&a取出的是a变量在内存中的起始地址
	int* p = &a;//int* 是p的类型
	return 0;
}
//sizeof是一个操作符,不是函数
//计算的是变量所占内存空间的大小,单位是字节
//计算类型所创建的变量占据空间的大小,单位是字节

#include
int  main()
{
	short s = 5;
	int a = 10;
	printf("%d\n", sizeof(s = a + 2));//2
	printf("%d\n", s);//5

	printf("%d\n", sizeof(a));//4 - 计算a所占空间的大小,单位是字节
	printf("%d\n", sizeof(int));//4
	printf("%d\n", sizeof a);//4

	int arr[10] = { 0 };
	printf("%d\n", sizeof(arr));//40 - 计算的是整个数组的大小
	printf("%d\n", sizeof(int [10]));//40 - int [10]是arr数组的类型
	printf("%d\n", sizeof(arr[0]));//4 - 计算的是数组首元素的大小
	
	int sz = sizeof(arr) / sizeof(arr[0]);
	printf("%d\n", sz);//10 - 数组的元素个数

	char arr2[] = "welcome to bit!!!!";//[welcome to bit!!!!\0]
	int sz = sizeof(arr2) / sizeof(arr2[0]);
	printf("%d\n", sz);//19 - 计算的时候包含了\0

	char arr3[] = { 'a', 'b', 'c' };//abc
	printf("%d\n", sizeof(arr3));//3

	char ch[5] = { 'a', 'b', 'c' };//不完全初始化,剩余的默认为0
	printf("%d\n", sizeof(ch));//5
	return 0;
}
//~操作符

#include
int main()
{
	int a = 0;
	//00000000000000000000000000000000 - 0的补码
	
	//~ 按位取反
	//11111111111111111111111111111111 - ~a
	//11111111111111111111111111111110
	//10000000000000000000000000000001 - 原码 

	int b = ~a;
	printf("%d\n", a);//0
	printf("%d\n", b);//-1
	return 0;
}
//++、--操作符(会改变原来的变量)

//代码1
#include
int main()
{
	//前置、后置++
	int a = 10;
	int b = a++;//后置++,先使用,再++
	//b=a;a=a+1;
	printf("%d\n", b);//10
	printf("%d\n", a);//11

	int b = ++a;//前置++ - 先++,后使用
	//b=a+1;a=a+1;
	printf("%d\n", b);//11
	printf("%d\n", a);//11

	//前置、后置--
	int a = 10;
	int b = a--;//后置--,先使用,再--
	//b=a;a=a-1;
	printf("%d\n", b);//10
	printf("%d\n", a);//9

	int b = --a;//前置-- - 先--,后使用
	//b=a-1;a=a-1;
	printf("%d\n", b);//9
	printf("%d\n", a);//9
	return 0;
}


//代码2
#include 
int main()
{
	int a, b, c;

	a = 5;
	c = ++a; //c = 6, a = 6
	b = ++c, c++, ++a, a++; //b = 7, c = 7, 8, a = 7, 8
	b += a++ + c; //a++ + c = 16, b = 23, a = 9

	printf("a = %d b = %d c = %d", a, b, c);//a = 9 b = 23 c = 8
	return 0;
}
//*操作符

#include 
int main()
{
	int a = 10;
	printf("%p\n", &a);//& - 取地址操作符
	int * pa = &a;//pa是用来存放地址的 - pa就是一个指针变量

	*pa = 20;//* - 解引用操作符 - 间接访问操作符
	printf("%d\n", a);//20
	return 0;
}

初阶第五节-操作符详解_第9张图片

//(类型)强制类型转换

#include
int main()
{
	int a = (int)3.14;//3.14默认为double类型,所以(int)3.14是将其强制类型转换为int类型
	return 0;
}

6.关系操作符

初阶第五节-操作符详解_第10张图片

#include
int main()
{
	if (3 == 5)
	{

	}
	if ("abc" == "abcdef")//err - 这样写是在比较2个字符串的首字符的地址
	{

	}
	return 0;
}

7.逻辑操作符 

//&& 看的是真假,表示的是并且
//|| 看的是真假,表示的是或者
#include
int main()
{
	int a = 0;
	int b = 5;
	int c = a && b;
	int d = a || b;
	if (a && b) //a,b都为真才真
	{
		printf("%d\n", c);//0
		printf("hehe\n");
	}
	if(a || b) //a,b只要有一个为真就为真
	{
		printf("%d\n", d);//1
		printf("hehe\n");
	}
	return 0;
}

7.1例题 

//总结:
//&& 左边为假,右边就不计算
//|| 左边为真,右边就不计算
//关键要看(a&&b)是否确定为真(或假)

#include 
int main()
{
    int i = 0, a = 0, b = 2, c = 3, d = 4;
    i = a++ && ++b && d++;//a++是先使用,后++。则式子:(0 && ++b)肯定为假(=0),所以++b不计算;且式子(0 && d++)肯定为假(=0),所以d++不计算
    printf("i = %d\na = %d\nb = %d\nc = %d\nd = %d\n", i, a, b, c, d);//0 1 2 3 4

    int i = 0, a = 1, b = 2, c = 3, d = 4;
    i = a++ && ++b && d++;//a++是先使用,后++。则式子:(1 && ++b)可能为真(=1),++b计算;且式子:(1 && d++)可能为真(=1),d++计算
    printf("i = %d\na = %d\nb = %d\nc = %d\nd = %d\n", i, a, b, c, d);//1 2 3 3 5

    int i = 0, a = 0, b = 2, c = 3, d = 4;
    i = a++ || ++b || d++;//a++是先使用,后++。则式子:(0 || ++b)可能为真(=1),所以++b计算;且式子(1 || d++)肯定为真(=1),所以d++不计算
    printf("i = %d\na = %d\nb = %d\nc = %d\nd = %d\n", i, a, b, c, d);//1 1 3 3 4

    int i = 0, a = 1, b = 2, c = 3, d = 4;
    i = a++ || ++b || d++;//a++是先使用,后++。则式子:(1 || ++b)肯定为真(=1),所以++b不计算;且式子(1 || d++)肯定为真(=1),所以d++不计算
    printf("i = %d\na = %d\nb = %d\nc = %d\nd = %d\n", i, a, b, c, d);//1 2 2 3 4
    return 0;
}

8.条件操作符 

初阶第五节-操作符详解_第11张图片

#include
int main()
{
	int a = 3;
	int b = 0;
	if (a > 5)
		b = 1;
	else
		b = -1;

	//三目操作符
	(a > 5) ? (b = 1) : (b = -1);
	b = (a > 5 ? 1 : -1);

    int max = (a > b ? a : b);
	return 0;
}

9.逗号表达式 

初阶第五节-操作符详解_第12张图片

#include
int main()
{
	a = get_val();
	count_val(a);
	while (a > 0)
	{
		//业务处理
		a = get_val();
		count_val(a);
	}
	return 0;
}

//如果使用逗号表达式,改写:
#include
int main()
{
	while (a = get_val(), count_val(a), a > 0)
	{
		//业务处理
	}
	return 0;
}
#include
int main()
{
	int a = 3;
	int b = 5;
	int c = 0;
	//逗号表达式 - 要从做向右依次计算,但是整个表达式的结果是最后一个表达式的结果

	int d = (c = 1, a = c + 3, b = a - 4, c += b);
	        //c=1   a=4        b=0        c=1   

	printf("%d\n", d);//1
	return 0;
}

10.下标引用、函数调用和结构成员 

10.1 [ ]下标引用操作符

#include 
int main()
{
	int arr[10] = { 0 };

	//arr[7] -> *(arr+7) -> *(7+arr) -> 7[arr]
	//arr是数组首元素的地址
	//arr+7就是跳过7个元素,指向了第8个元素
	//*(arr+7)就是第8个元素

	printf("%d\n", arr[4]);
	//[] - 就是下标引用操作符
	//[] 的操作数是2个:arr , 4
	arr[7] = 8;
	7[arr] = 9;
	return 0;
}

10.2 ( ) 函数调用操作符   

接受一个或者多个操作数:第一个操作数是函数名,剩余的操作数就是传递给函数的参数。

//函数的定义
#include
int Add(int x, int y)
{
	return x + y;
}
int main()
{
	int a = 10;
	int b = 20;
	//函数调用
	int ret = Add(a, b);//() - 函数调用操作符,操作数:Add,a,b
	return 0;
}

10.3 访问一个结构的成员 

//结构成员访问操作符
//.
//->

//结构体
//书:书名,书号,定价
//人:名字,年龄,性别

//创建了一个自定义的类型
#include
struct Book
{
	//结构体的成员(变量)
	char name[20];
	char id[20];
	int price;
};
int main()
{
	//结构体变量名.成员名
	struct Book b = {"C语言", "C20210509", 55};
	struct Book * pb = &b;

	//结构体指针->成员名
	printf("书名:%s\n", pb->name);
	printf("书号:%s\n", pb->id);
	printf("定价:%d\n", pb->price);

	printf("书名:%s\n", (*pb).name);
	printf("书号:%s\n", (*pb).id);
	printf("定价:%d\n", (*pb).price);

	printf("书名:%s\n", b.name);
	printf("书号:%s\n", b.id);
	printf("定价:%d\n", b.price);
	return 0;
}

#include 
struct Stu
{
	char name[10];
	int age;
	double score;
};
void set_Stu(struct Stu* ps)
{
	//strcpy((*ps).name, "zhangsan");
	//(*ps).age = 20;
	//(*ps).score = 100.0;
	 
	strcpy(ps->name, "zhangsan");
	ps->age = 20;
	ps->score = 100.0;
}
void ptint_Stu(struct Stu* ps)
{
	printf("%s %d %lf", ps->name, ps->age, ps->score);
}
int main()
{
	struct Stu s = { 0 };
	set_Stu(&s);
	ptint_Stu(&s);//zhangsan 20 100.000000
	return 0;
}

错误代码 

初阶第五节-操作符详解_第13张图片

11.表达式求值 

11.1整型提升(类型小于int)

把整型放入char类型中时,先将其整型的原反补码列出来,然后进行截断。若以%d的形式打印出来,则整型提升时补符号位,但如果是unsigned char类型则需要补0,然后将其化为原码打印出来;若以%u的形式打印出来,则整型提升时补符号位,且打印时原反补码相同。

 初阶第五节-操作符详解_第14张图片

//char a = -1;//-1是整数,32个比特位
//10000000000000000000000000000001 - -1的原码
//11111111111111111111111111111110 - -1的反码
//11111111111111111111111111111111 - -1的补码
//11111111 - 截断
//11111111111111111111111111111111 - 整型提升


//int 4byte - 32bit
//char 1byte - 8bit

#include
int main()
{
	char a = 5;
	//00000000000000000000000000000101 - 5的补码
	//00000011 - a

	char b = 126;
	//00000000000000000000000001111110 - 126的补码
	//01111110 - b

	char c = a + b;
	//00000000000000000000000000000101 - a
	//00000000000000000000000001111110 - b
	//00000000000000000000000010000011
	//10000011 - c
	//11111111111111111111111110000011 - c的补码
	//11111111111111111111111110000010 - c的反码
	//10000000000000000000000001111101 - c的原码

	printf("%d\n", c);//-125
	return 0;
}
//1.
//输出什么?
#include
int main()
{
	char a = -1;
	signed char b = -1;
	//10000000000000000000000000000001 - -1的原码
	//11111111111111111111111111111110 - -1的反码
	//11111111111111111111111111111111 - -1的补码
	//截断
	//11111111 - a,b

	//整型提升
	//11111111111111111111111111111111 
	//10000000000000000000000000000000
	//10000000000000000000000000000001 - -1的原码

	unsigned char c = -1;

	//无符号数进行整型提升,直接补0
    //00000000000000000000000011111111 - 原反补码相同

	//要以有符号整型的形式打印
	printf("a = %d\nb = %d\nc = %d\n", a, b, c);//a = -1 b = -1 c = 255
	return 0;
}

//2.
#include 
int main()
{
	char a = -128;
	//10000000000000000000000010000000
	//11111111111111111111111101111111
	//11111111111111111111111110000000
	//截断
	//10000000

	//有符号数进行整型提升,补符号位
	//11111111111111111111111110000000 
	//由于要以%u的形式打印,则无符号数的原反补码相同

	unsigned char b = -128;
	//截断后 - 10000000
	//无符号数进行整型提升,直接补0
	//00000000000000000000000010000000

    //要以无符号整型的形式打印
	printf("%u\n", a);//4294967168
	printf("%u\n", b);//128
	return 0;
}
//3.
#include 
int main()
{
	char a = 128;
	//00000000000000000000000010000000
	//01111111111111111111111101111111
	//01111111111111111111111110000000
	//截断
	//10000000
	//有符号数进行整型提升,补符号位
    //11111111111111111111111110000000 
	//由于要以%u的形式打印,则无符号数的原反补码相同
	
	//要以无符号整型的形式打印
	printf("%u\n", a);//4294967168

	//截断后 - 10000000
	//有符号数进行整型提升,补符号位
	//11111111111111111111111110000000 - 补码
	//10000000000000000000000001111111
	//10000000000000000000000010000000 - 原码

	//要以有符号整型的形式打印
	printf("%d\n", a);//-128
	return 0;
}

//4.
#include
int main()
{
    int i = -20;
	unsigned  int  j = 10;

	//10000000000000000000000000010100 - -20的原码
	//11111111111111111111111111101011 - -20的反码

	//11111111111111111111111111101100 - -20的补码
	//+
	//00000000000000000000000000001010 - 10的补码
	//11111111111111111111111111110110
	//10000000000000000000000000001001
	//10000000000000000000000000001010 - -10的原码


	printf("%d\n", i + j);//-10

	//11111111111111111111111111110110 - 原反补码相同
	printf("%u\n", i + j);//4294967286
	return 0;
}
#include
int main()
{
	char a = 0xb6;//10110110 - 类型改为unsigned char高位补0才会输出a
	short b = 0xb600;
	int c = 0xb6000000;

	if (a == 0xb6)
		printf("a");
	if (b == 0xb600)
		printf("b");
	if (c == 0xb6000000)
		printf("c");//c

	return 0;
}


#include
int main()
{
	char c = 1;
	printf("%zu\n", sizeof(c));//1
	printf("%zu\n", sizeof(+c));//4
	printf("%zu\n", sizeof(-c));//4
	printf("%zu\n", sizeof(!c));//4 gcc - 4

	return 0;
}

11.2算术转换(类型大于等于int)

初阶第五节-操作符详解_第15张图片

12.操作符的属性 

初阶第五节-操作符详解_第16张图片初阶第五节-操作符详解_第17张图片

初阶第五节-操作符详解_第18张图片

初阶第五节-操作符详解_第19张图片

 12.1问题表达式

初阶第五节-操作符详解_第20张图片

初阶第五节-操作符详解_第21张图片

 初阶第五节-操作符详解_第22张图片

初阶第五节-操作符详解_第23张图片

13.练习

//写一个函数返回参数二进制中1的个数

//代码1
#include
int count_num_of_1(unsigned int n) //无符号整数,-1的符号位也被算作-1的大小
{
	int count = 0;
	while (n)
	{
		if ((n % 2) == 1)
		{
			count++;
		}
		n = n / 2;
	}
	return count;
}
//-1
//10000000000000000000000000000001
//11111111111111111111111111111110
//11111111111111111111111111111111
int main()
{
	int num = 0;
	scanf("%d", &num);
	int n = count_num_of_1(num);
	printf("%d\n", n);
	return 0;
}


//代码2
#include
int count_num_of_1(int n) 
{
	int count = 0;
	int i = 0;
	for (i = 0; i < 32; i++)
	{
		if ((n >> i)&1 == 1)
		{
			count++;
		}
	}
	return count;
}
int main()
{
	int num = 0;
	scanf("%d", &num);
	int n = count_num_of_1(num);
	printf("%d\n", n);
	return 0;
}


//代码3
#include
int  count_num_of_1(int n)
{
	int count = 0;
	while (n)
	{
		n = n & (n - 1);
		count++;
	}
	return count;
}
//n = 15
//n = n&(n-1)
//1111  n
//1110  n-1
//1110  n
//1101  n-1
//1100  n
//1011  n-1
//1000  n
//0111  n-1
//0000  n
int main()
{
	int num = 0;
	scanf("%d", &num);
	int n = count_num_of_1(num);
	printf("%d\n", n);
	return 0;
}
//两个int(32位)整数m和n的二进制表达中,有多少位(bit)不同?

//代码1
#include
int count_diff_bit(int m, int n)
{
	int count = 0;
	int i = 0;
	for (i = 0; i < 32; i++)
	{
		if (((m >> i) & 1) != ((n >> i) & 1))
		{
			count++;
		}
	}
	return count;
}
int main()
{
	int m = 0;
	int n = 0;
	scanf("%d %d", &m, &n);//1999 2299
	int ret = count_diff_bit(m, n);
	printf("%d\n", ret);//7
	return 0;
}


//代码2
#include
int count_diff_bit(int m, int n)
{
	int count = 0;
	int ret = m ^ n;//^异或操作符,相同为0,相异为1
	while (ret) //统计一下ret中二进制位有多少个1
	{
		ret = ret & (ret - 1);
		count++;
	}
	return count;
}
int main()
{
	int m = 0;
	int n = 0;
	scanf("%d %d", &m, &n);//1999 2299
	int ret = count_diff_bit(m, n);
	printf("%d\n", ret);//7
	return 0;
}
//获取一个整数二进制序列中所有的偶数位和奇数位,分别打印出二进制序列

//10
//00000000000000000000000000001010

#include
int main()
{
	int i = 0;
	int num = 0;
	scanf("%d", &num);

	//获取奇数位的数字
	for (i = 30; i >= 0; i-=2)
	{
		printf("%d ", (num >> i) & 1);
	}
	printf("\n");
	//获取偶数位的数字
	for (i = 31; i >= 1; i -= 2)
	{
		printf("%d ", (num >> i) & 1);
	}
	return 0;
}
//多组输入,一个整数(2-20),表示输出的行数,也表示组成"X"的反斜线和正斜线的长度
//针对每行的输入,输出用"*"组成的X形图案

#include
int main()
{
	int n = 0;
	while (scanf("%d", &n) == 1)//只有一个字符时,scanf读取成功返回1,n个字符返回n
	{
		int i = 0;
		int j = 0;
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < n; j++)
			{
				if (i == j)
					printf("*");
				else if (i + j == n - 1)
					printf("*");
				else
					printf(" ");
			}
			printf("\n");
		}
	}
	return 0;
}
//多组输入,一行有两个整数,分别表示年份和月份,用空格分隔
// 输出一个整数,表示这一年的这一个月有多少天

#include
int is_leap_year(int y)
{
	return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0));
}
int main()
{
	int y = 0;
	int m = 0;
	int d = 0;
	int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	while (scanf("%d%d", &y, &m) == 2)//只有一个字符时,scanf读取成功返回1,n个字符返回n
	{
		int d = days[m];
		if ((is_leap_year(y) == 1) && (m == 2))//判断闰年2月
		{
			d++;
		}
		printf("%d\n", d);
	}
	return 0;
}
//多组输入数据,每一行输入三个a,b,c(0
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	while (scanf("%d %d %d", &a, &b, &c) == 3)//scanf的返回值取决于有几个字符
	{
		if ((a + b > c) && (a + c > b) && (b + c > a))//判断是否为三角形
		{
			if ((a == b) && (b == c))//等边
			{
				printf("Equilateral triangle!\n");
			}
			else if ((a == b, c != b) || (a == c, b != c) || (b == c, a != c))//等腰
			{
				printf("Isosceles triangle!\n");
			}
			else
				printf("Ordinary triangle!\n");
		}
		else
			printf("Not a triangle!\n");
	}
	return 0;
}
//输入一个整数n(0 <= n <=10^9)
//输出整数n的每位,如果是奇数,把它变成1;如果是偶数,把它变成0
 
#include
#include
int main()
{
	int i = 0;
	int sum = 0;
	int n = 0;
	scanf("%d", &n);
	while (n)
	{
		int bit = n % 10;
		if (bit % 2 == 1)//如果为奇数
		{
			bit = 1;
		}
		else  //如果为偶数
		{			
			bit = 0;
		}
		sum += bit * pow(10, i);
		i++;
		n /= 10;
	}
	printf("%d\n", sum);
	return 0;
}
#include
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;
	int e = 0;
	for (a = 1; a <= 5; a++)
	{
		for (b = 1; b <= 5; b++)
		{
			for (c = 1; c <= 5; c++)
			{
				for (d = 1; d <= 5; d++)
				{
					for (e = 1; e <= 5; e++)
					{
						if (   ((a == 3) + (b == 2) == 1)
							&& ((b == 2) + (e == 4) == 1)
							&& ((c == 1) + (d == 2) == 1)
							&& ((c == 5) + (d == 3) == 1)
							&& ((e == 4) + (a == 1) == 1))
						{
							if (a * b * c * d * e == 120)
							{
								printf("a=%d b=%d c=%d d=%d e=%d\n", a, b, c, d, e);
								//a=3 b=1 c=5 d=2 e=4
							}
						}
					}
				}
			}
		}
	}
	return 0;
}

初阶第五节-操作符详解_第24张图片 

#include
int main()
{
	int killer = 0;
	for (killer = 'a'; killer <= 'd'; killer++)
	{
		//判断
		if ((killer != 'a') + (killer == 'c') + (killer == 'd') + (killer != 'd') == 3)
		{
			printf("%c\n", killer);//c
		}
	}
	return 0;
}

初阶第五节-操作符详解_第25张图片

                                                                                                                     

你可能感兴趣的:(java,算法,数据结构)