if (Number %26amp; 0x1)
//number is odd
else
//number is even
Program to find if a number is odd or even using bitwise operator in C?
When using C as the numeric 0 means boolean false at the same time you can use
if (num %26amp;%26amp; 1) { /*odd here*/ } else {/*even here */}
Loren Soth
Reply:ok.. both your previous posters got the logic right, but the syntax wrong
for a bitwise and operation use a single %26amp;
%26amp;%26amp; is used for logical anding
Reply:main()
{
int x;
scanf("%d",%26amp;x);
if((x%26amp;1)==1)
printf("The number is odd");
else
printf("The Number is even");
}
Reply:every odd number (in binary) has 1 on the end ie 10001 is odd.
so just do if (mynum %26amp;%26amp; 1 == 1) odd
(mynum %26amp;%26amp; 1 == 0) even
AND 1 will put all flags to zero except last one
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment