1,/ 与 %的应用
int main()
{
int a = 5 / 2;//商2余1
printf(“a = %dn”, a);
int b = 5 % 2;
printf(“b = %dn”, b);
return 0;
}
2,移位操作符
右移操作符
1.算术右移
右边丢弃,左边补原符号位
2.逻辑右移
右边丢弃,左边补0
左移操作符
左边丢弃,右边补0
int main()
{
int a = 5;
a
int b = a
//00000000000000000000000000101
printf(“%dn”, b);
return 0;
}
警告:对于移位运算符,不要移动负数位,这个标准是未定义的。例如:
int num = 10;
num>>-1//error
3,位操作符
int main()
{
//& – 按2进制位与
int a = 3;
int b = 5;
int c = a&b;
//00000000000000000000000000011
//00000000000000000000000000101
//00000000000000000000000000001
printf(“%dn”, c);
return 0;
}
//| – 按2进制位或
int a = 3;
int b = 5;
int c = a | b;
////00000000000000000000000000011
////00000000000000000000000000101
////00000000000000000000000000111
printf(“%dn”, c);
return 0;
}
int a = 3;
int b = 5;
int c = a ^ b;
printf(“%dn”, c);
//00000000000000000000000000011
//00000000000000000000000000101
//00000000000000000000000000110
return 0;
int main()
{
int a = 3;
int b = 5;
//int tmp = 0;//临时变量
printf(“before: a = %d b = %dn”, 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 =%dn”, a, b);
return 0;
}
练习1
int main()
{
int num = 0;
int count = 0;
scanf_s(“%d”, &num);
int i = 0;
for (i = 0; i
{
if (1 == ((num >> i) & 1))
count++;
}
printf(“%dn”, count);
//统计num的补码中有几1
//while (num)
//{
// if (num % 2 == 1)
// count++;
// num = num / 2;
//}
return 0;
}
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
printf(“%dn”, a);
a = a & (~(1
printf(“%dn”, a);
return 0;
}
不写了哥们我累死了
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: 一个java文件的JVM之旅 | 京东物流技术团队
答开源创业 15 问:选协议、维权、公关、找钱 准备 我是小C同学编写得一个java文件,如何实现我的功能呢?需要去JVM(Java Virtual Machine)这个地方旅行。 变身 我高高兴兴的来到JVM,想要开始JVM之旅,它确说:“现在的我还不能进去…