手机子王掩码和网关查找
Problem statement: Write a C program to find odd or even no using bitwise operators.
问题陈述:编写一个C程序以使用按位运算符查找奇数或什至无 。
Solution: We can use bitwise operator here to solve the problem.
解决方案:我们可以在这里使用按位运算符来解决问题。
Solution
解
Let n be a number
n &1 = 0000000d where d is the LSB of n
So,
If LSB ==1
It's odd number
Else
Even no
To understand the concept of "LSB" & bitwise operators please read: Bitwise Operators and their working with Examples in C
要了解“ LSB”和按位运算符的概念,请阅读: 按位运算符及其在C语言中与Example一起使用
Example:
例:
Let n be 7 //00000111
n & 1 = 1
00000111(7) & 00000001(1) =00000001 (1)
Thus it's an odd number
Let n be 6 //00000110
N&1=0
00000110(6) & 00000001(1) =00000000 (0)
Thus it's an even number
C implementation
C实现
#include <stdio.h>
int main()
{
int n;
printf("enter no: ");
scanf("%d",&n);
//n&1 actually results in 0000000d where d is your LSB
if(n&1==1)
printf("it's odd no");//for odd no LSB 1
else
printf("it's even no");//for even no LSB 0
return 0;
}
Output
输出量
First run:
enter no: 7
it's odd no
Second run:
enter no: 6
it's even no
翻译自: https://www.includehelp.com/c-programs/find-odd-or-even-number-using-bitmasking.aspx
手机子王掩码和网关查找