1,/ 与 %的应用

int main()

{

int a = 5 / 2;//商2余1

printf("a = %d\n", a);

int b = 5 % 2;

printf("b = %d\n", b);

return 0;

}

B站C语言第十三课——操作符_赋值

2,移位操作符

右移操作符

1.算术右移

右边丢弃,左边补原符号位

2.逻辑右移

右边丢弃,左边补0

左移操作符

左边丢弃,右边补0


int main()

{

int a = 5;

a << 1;

int b = a << 1;

//00000000000000000000000000101

printf("%d\n", b);

return 0;


}

B站C语言第十三课——操作符_进制_02

警告:对于移位运算符,不要移动负数位,这个标准是未定义的。例如:

int num = 10;

num>>-1//error

3,位操作符


int main()

{

//& - 按2进制位与

int a = 3;

int b = 5;

int c = a&b;

//00000000000000000000000000011

//00000000000000000000000000101

//00000000000000000000000000001

printf("%d\n", c);

return 0;


}


B站C语言第十三课——操作符_进制_03

 //| - 按2进制位或

int a = 3;

int b = 5;

int c = a | b;

////00000000000000000000000000011

////00000000000000000000000000101

////00000000000000000000000000111

printf("%d\n", c);

return 0;

}


B站C语言第十三课——操作符_操作符_04

 int a = 3;

int b = 5;

int c = a ^ b;

printf("%d\n", c);

//00000000000000000000000000011

//00000000000000000000000000101

//00000000000000000000000000110

return 0;

B站C语言第十三课——操作符_赋值_05

int main()

{

int a = 3;

int b = 5;

//int tmp = 0;//临时变量

printf("before: a = %d b = %d\n", a, b);

//tmp = a;

//a = b;

//b = tmp;

//加减法 缺陷:可能会溢出

/*a = a + b;

b = a - b;

a = a - b;*/

a = a^b;

b = a^b;

a = a^b;


printf("after: a = %d b =%d\n", a, b);

return 0;

}

B站C语言第十三课——操作符_操作符_06

练习1

int main()

{

int num = 0;

int count = 0;

scanf_s("%d", &num);

int i = 0;

for (i = 0; i < 32; i++)

{

 if (1 == ((num >> i) & 1))

  count++;

}

printf("%d\n", count);

//统计num的补码中有几1

//while (num)

//{

// if (num % 2 == 1)

//  count++;

// num = num / 2;

//}


 

return 0;


}

B站C语言第十三课——操作符_进制_07

4,赋值操作符 = 

复合赋值符

int main()

{

int a = 10;

a = a + 2;

a += 2;//复合赋值符


a = a >> 1;

a >>= 1;

a = a & 1;

a &= 1;

return 0;


}

按位取反~

int main()

{

int a = 11;

a = a | (1 << 2);

printf("%d\n", a);  

a = a & (~(1 << 2));

printf("%d\n", a);

return 0;

}

B站C语言第十三课——操作符_进制_08

不写了哥们我累死了