c语言中按位逻辑运算符位移运算符
Posted 小虾米2018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言中按位逻辑运算符位移运算符相关的知识,希望对你有一定的参考价值。
c语言中按位逻辑运算符、位移运算符
#include <stdio.h>
int count_bits(unsigned x)
{
int bits = 0;
while(x)
{
if(x & 1U)
bits++;
x >>= 1;
}
return bits;
}
int int_bits(void)
{
return count_bits(~0U);
}
int print_bits(unsigned x)
{
int i;
for(i = int_bits() - 1; i >= 0; i--)
{
putchar(((x >> i) & 1U) ? \'1\' : \'0\');
}
putchar(\'\\n\');
}
int main(void)
{
unsigned a, b;
puts("please input two unsigned integers.");
printf("a = "); scanf("%u", &a);
printf("b = "); scanf("%u", &b);
printf("\\nbinary a = "); print_bits(a);
printf("\\nbinary b = "); print_bits(b);
printf("\\na & b = "); print_bits(a & b);
printf("\\na | b = "); print_bits(a | b);
printf("\\na ^ b = "); print_bits(a ^ b);
printf("\\n ~a = "); print_bits(~a);
printf("\\n ~b = "); print_bits(~b);
return 0;
}
以上是关于c语言中按位逻辑运算符位移运算符的主要内容,如果未能解决你的问题,请参考以下文章