Q:

C program to find the negative or positive number using bitwise operators and ternary operators

0

This program asks the user to enter any number. Next, this c program checks whether the given value is positive or negative or zero using Conditional Operator or ternary operator

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
// Total bits required to represent integer
#define BITS sizeof(int) * 8
//Logic to check whether a number is positive
int isPositive(int n)
{
    return (!( n & (1 << (BITS -1 )) | (!n)));
}
int main()
{
    int num;
    printf("Enter any number: ");
    scanf("%d", &num);
    isPositive(num)? printf("Positive number\n"):(num)? printf("Negative number\n"): printf("Zero\n");
    return 0;
}

Output:

Enter any number: -10
Negative number

Enter any number: 10
Positive number

Enter any number: 0
Zero

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now