5 Ways to Check if Two Integers have Opposite Signs

check sign of integer

In this blog post, I will teach you how to write C/C++ program to check if two integers have opposite signs or not. After reading this post you able to check whether given two integers have opposite signs or not.

But before writing the C program to how to check whether two given integers have opposite sign; let’s first understand how to know the sign of given integer. The sign of integer number you can easily check with their MSB (most significant bit).  If the MSB is set that means integer is negative otherwise positive.

So that means two integers are opposite in sign if their MSB is different. Using the ex-or operator, you can check the sign of the two integers.

Now you are thinking how?

Don’t worry, I will explain first see the below table.

E.g.

              BIT1                BIT2  BIT1  ^  BIT2
                1                   1                       0
                0                   0                       0
                1                   0                       1
                0                   1                       1

The above table is explained that the exclusive OR (XOR) operation on same bits produce 0 and for different 1.

When two integers have opposite signs, their MSB (sign bit) will be different. So, we will use the same concept to check the MSB of two integers and easily we will know that they have different sign or same.

 

Let’s see the problem statements for this question.

Problem Statement: Given two signed integers, check whether these two integers have different sign or same.

Disclaimer: Try to solve the problem yourself first otherwise it would be not worth solving this problem.

 

5 Ways to Detect if two integers have the opposite signs:

 

1. Using Bitwise XOR (^) operation:

This is one of the most popular ways to check sign of two integers. Let the given integers are "a” and “b”. The EX-OR of sign bit (MSB) of “a” and “b” will be 1 if the sign bit of “a” is different from the sign bit of “b”. In other words, you can say, EX-OR of “a” and “b” will be negative if “a” and “b” have the opposite signs. In the below code, I have used the same logic.

/*
C program to Check if Two Integers have
Opposite Signs Using Bitwise XOR (^) operation:.
*/
#include<stdio.h>
int checkOppositeSign(int a, int b)
{
    // true if a and b have opposite signs
    const int ret = ((a ^ b) < 0);
    return ret;
}

int main()
{
    int a = 0,b=0;
    //ENTER THE VALUE OF a & b
    printf("Enter the Value of a = ");
    scanf("%d",&a);

    printf("\nEnter the Value of b = ");
    scanf("%d",&b);

    // check signs of a & b
    const int isOppositeSign = checkOppositeSign(a, b);
    if (isOppositeSign)
    {
        printf ("\nIntegers have the opposite sign\n\n");
    }
    else
    {
        printf ("\nInteger have the same sign\n\n");
    }
    return 0;
}

OutPut 1:

 

 

 

Output 2:

 

 

 

 

2. Using Comparision and ternary operator:

Using the ternary operator, you can also detect signs of two integers. Let the given integers are “a” and “b”. In the below expression if both number “a” and “b” have the same sign then ternary operator return 0 otherwise return 1.

/*
C program to Check if Two Integers have
Opposite Signs Using return operator:.
*/
#include<stdio.h>

int main()
{
    int a = 0,b = 0;
    //ENTER THE VALUE OF a & b
    printf("\nEnter the Value of a = ");
    scanf("%d",&a);
    printf("\nEnter the Value of b = ");
    scanf("%d",&b);
    // return 1 if a and b have opposite signs
    const int isOppositeSign = (a < 0)? (b >= 0): (b < 0);
    if (isOppositeSign)
    {
        printf ("\nIntegers have the opposite sign\n\n");
    }
    else
    {
        printf ("\nInteger have the same sign\n\n");
    }
    return 0;
}

Output 1:

 

 

 

Output 2:

 

 

 

 

3. Using X-OR and shifting operator:

In this approach you don’t need to use any comparison operator. You only need to use the ex-or operator along with shifting operator.

The below expression basically checks sign bit of (a^b) using bitwise operator ‘>>’. If the sign of both integers is the same, then the MSB bit of value which evaluate by the expression (a^b)  will be 0 otherwise 1.

Now to check whether two integers have same sign or different you just need to check the MSB bit of value a^b with the help of right shift (>>) operator.

/*
C program to Check if Two Integers have
Opposite Signs Using ex-or and right shift operator:.
*/


//Number of bits in one char
#define BYTE_SIZE  8


//Number of bits in integer
#define  BITS_IN_INTEGER   (sizeof(int)* BYTE_SIZE)


int main()
{
    int a = 0,b = 0;
    printf("Enter the Value of a = ");
    scanf("%d",&a);

    printf("\nEnter the Value of b = ");
    scanf("%d",&b);

    //Get the MSB of (a^b) and check it
    const int isOppositeSign = ((a ^ b) >> (BITS_IN_INTEGER-1));
    if (isOppositeSign)
    {
        printf ("\nIntegers have the opposite sign\n\n");
    }
    else
    {
        printf ("\nInteger have the same sign\n\n");
    }
    
    return 0;
}

Output 1:

 

 

 

Output 2:

 

 

 

 

4. Using X-OR and comparison operator:

In this approach, expression (a >= 0) ^ (b < 0) check the signs of “a” and “b”, if the sign of both numbers is the same then result of the expression will be 1 otherwise 0.

/*
C program to Check if Two Integers have
Opposite Signs Using ex-or and relational operator:.
*/
#include<stdio.h>

#define  TRUE 1
int main()
{
    int a = 0,b = 0;

    printf("Enter the Value of a = ");
    scanf("%d",&a);
    
    printf("\nEnter the Value of b = ");
    scanf("%d",&b);
    
    //true when have the same sign
    const int isSameSign  =  ((a >= 0) ^ (b < 0)); 
    if (isSameSign)
    {
        printf ("\nInteger have the same sign\n\n");
    }
    else
    {
        printf ("\nIntegers have the opposite sign\n\n");
    }
    
    return 0;
}

Output 1:

 

 

 

Output 2:

 

 

5. Using Arithmetic Multiplication:

In the below code, function checkOppositeSign() do the arithmetic multiplication. If two numbers have opposite signs, their multiplication will be negative.

/*
C program to Check if Two Integers have
Opposite Signs Using Arithmetic Multiplication.
*/

#include<stdio.h>
int checkOppositeSign(int a, int b)
{
    // true if a and b have opposite signs
    const int ret = ((a * b) < 0);
    return ret;
}

int main()
{
    int a = 0,b=0;

    //ENTER THE VALUE OF a & b
    printf("Enter the Value of a = ");
    scanf("%d",&a);

    printf("\nEnter the Value of b = ");
    scanf("%d",&b);

    // check signs of a & b
    const int isOppositeSign = checkOppositeSign(a, b);
    if (isOppositeSign)
    {
        printf ("\nIntegers have the opposite sign\n\n");
    }
    else
    {
        printf ("\nInteger have the same sign\n\n");
    }
    return 0;
}

Output:

Enter the Value of a = -3

Enter the Value of b = 6

Integers have the opposite sign

 

6. Using nested if-else statement:

It is the easiest way to check the signs of integers “a” and “b” with the help of if-else statement.

/*
C program to Check if Two Integers have
Opposite Signs Using if-else statement.
*/
#include<stdio.h>

int checkOppositeSign(int a, int b)
{
    if(a < 0 && b >= 0 )
    {
        return 1;
    }
    else if( a >= 0 && b < 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}


int main()
{
    int a = 0,b = 0;

    //ENTER THE VALUE OF a & b
    printf("\nEnter the Value of a = ");
    scanf("%d",&a);

    printf("\nEnter the Value of b = ");
    scanf("%d",&b);

    // check signs of a & b
    const int isOppositeSign = checkOppositeSign(a, b);
    if (isOppositeSign)
    {
        printf ("\nIntegers have the opposite sign\n\n");
    }
    else
    {
        printf ("\nInteger have the same sign\n\n");
    }
    return 0;
}

 

Recommended Post for You



Leave a Reply

Your email address will not be published. Required fields are marked *