C program to check if a number is positive, negative or zero using bit operators

In this blog post, we learn how to write a C program to check if a number is positive, negative, or zero using bit operators?. We will write the C program which takes input any number from the user and check whether the given number is positive or negative using the bitwise Operators and ternary operators.

Input: 10
Output: 10 is positive

Input: -10
Output: -10 is negative

Input: 0
Output: 0 is zero

 

 

How to determine a number is positive or negative using bitwise operators?

As we know that number is positive or negative it decides by the MSB. So,

  • An integer number n is said negative if MSB  of n is 1.
  • An integer numbern is said positive if MSB  of n is 0.

 

There are several ways to check whether given integer number is negative or positive number using bitwise operators and ternary operators. Here I have mentioned few of them.

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

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.

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

#include <stdio.h>

// Total bits required to represent integer
#define BITS sizeof(int) * 8


/*
C function to check whether
a number is positive
*/
int isPositive(int n)
{
    return (!((n & (1 << (BITS -1))) | (!n)));
}


int main()
{
    int num;
    //Get numbers from user
    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

Code Explanation:

The expression #define BITS sizeof(int) * 8 gives the total number of bits in an integer.

The expression ( n & (1 << (BITS -1 )) is to check MSB bit and gives 1 if the number is negative.

The expression !n is to check zero number it gives 1 if the number is zero.

 

2. C program to find positive or negative using bitwise operators and if-else:

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 the if-else statement.

#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,numType;

    printf("Enter any number: ");
    scanf("%d", &num);

    numType = isPositive(num);

    if(numType && num)
    {
        printf("Positive number\n");
    }
    else if (num)
    {
        printf("Negative number\n");
    }
    else
    {
        printf("Zero\n");
    }

    return 0;
}

Output:

Enter any number: -10
Negative number

Enter any number: 10
Positive number

Enter any number: 0
Zero

 

3. C program to find positive, negative or zero using Comparison operators and switch-case:

 

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

#include <stdio.h>

// Total bits required to represent integer
#define BITS sizeof(int) * 8


/*
C function to check whether
a number is positive, negative or zero
*/
int checkNumber(int n)
{
    return (n > 0) - (n < 0);
}


int main()
{
    int num;
    //Get numbers from user
    printf("Enter any number: ");
    scanf("%d", &num);

    int value = checkNumber(num);
    switch (value)
    {
    case 1:
        printf("%d is a positive number.\n", num);
        break;
    case -1:
        printf("%d is a negative number.\n", num);
        break;
    case 0:
        printf("The number is zero.\n");
        break;
    }

    return 0;
}

 

Code Explanation: The function checkNumber() returns 1, 0 or -1 if integer number is positive, negative or zero. The given switch statement prints the corresponding message based on the value which is return by the function.

 

4. Check sign of the given number if it comes from the command line argument.

In C, command-line arguments are passed as strings. That means your C code get the command-line arguments through the main function in form of array of strings (char *argv[]). Here argv is an array that contains strings.

For better understanding consider an example. Suppose your C program name is print_name. If you execute print_name with arguments like this:

[aticleworld@centos]$ ./print_name 20

These arguments are received in the argv array as strings and  argv contains the following at different index.

  1. argv[0] will contain “./print_name”.
  2. argv[1] will contain “20”.

Now it’s time to see the C code the check whether enter number at command argument line is positive or negative.

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

#include <stdio.h>
#include <stdlib.h>

/*
C function to check whether
a number is positive, negative or zero
*/
int checkNumber(int n)
{
    return (n > 0) - (n < 0);
}


int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        printf("Enter valid arguments\n");
        return -1;
    }

    /*
    The atoi function convert the initial
    portion of the string pointed to by argv[1]
    to int,*/
    int num = atoi(argv[1]);

    //now check sign of number
    int value = checkNumber(num);
    switch (value)
    {
    case 1:
        printf("%d is a positive number.\n", num);
        break;
    case -1:
        printf("%d is a negative number.\n", num);
        break;
    case 0:
        printf("The number is zero.\n");
        break;
    }

    return 0;
}

Output:

[aticleworld@centos]$ ./print_name 20

20 is a positive number.

 

Recommended Post: