📌  相关文章
📜  使用位运算符检查数字是正数,负数还是零

📅  最后修改于: 2021-04-23 07:03:35             🧑  作者: Mango

给定数字N,不使用条件语句就检查它是正数,负数还是零。
例子:

Input : 30
Output : 30 is positive

Input : -20
Output : -20 is negative

Input: 0
Output: 0 is zero

有符号移位n >> 31将每个负数转换为-1,并将每个负数转换为0。
当我们执行-n >> 31时,如果它是正数,则它将像执行-n >> 31一样返回-1,反之亦然。
但是,当我们对0进行运算时,n >> 31和-n >> 31都返回0,因此我们得到一个公式:

..1)当n为负数时
1 +(-1)-0 = 0
..2)当n为正数时:
1 + 0-(-1)= 2
..3)当n为0时:
1 + 0-0 = 1
因此我们知道,当它为负数时,它返回0;当它为零时,它返回1;当它为正数时,它返回2。
因此,为了不使用if语句,我们将索引存储在第0个索引“负”,第一个索引“零”和第2个索引“正”,并根据它们进行打印。

CPP
// CPP program to find if a number is
// positive, negative or zero using
// bit wise operators.
#include 
using namespace std;
 
// function to return 1 if it is zero
// returns 0 if it is negative
// returns 2 if it is positive
int index(int i)
{
    return 1 + (i >> 31) - (-i >> 31);
}
 
void check(int n)
{
    // string array to store all kinds of number
    string s[] = { "negative", "zero", "positive" };
 
    // function call to check the sign of number
    int val = index(n);
 
    cout << n << " is " << s[val] << endl;
}
 
// driver program to test the above function
int main()
{
    check(30);
    check(-20);
    check(0);
    return 0;
}


Java
// Java program to find if a number is
// positive, negative or zero using
// bit wise operators.
class GFG {
     
    // function to return 1 if it is zero
    // returns 0 if it is negative
    // returns 2 if it is positive
    static int index(int i)
    {
        return 1 + (i >> 31) - (-i >> 31);
    }
 
    static void check(int n)
    {
         
        // string array to store all kinds
        // of number
        String s[] = { "negative", "zero",
                              "positive" };
 
        // function call to check the sign
        // of number
        int val = index(n);
 
        System.out.println(n + " is " + s[val]);
    }
     
    // Driver code
    public static void main(String[] args)
    {
        check(30);
        check(-20);
        check(0);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python 3 program to
# find if a number is
# positive, negative
# or zero using
# bit wise operators.
 
# function to return 1 if it is zero
# returns 0 if it is negative
# returns 2 if it is positive
def index(i):
 
    return 1 + (i >> 31) - (-i >> 31)
 
 
def check(n):
 
    # string array to store all kinds of number
    s = "negative", "zero", "positive"
 
    # function call to check the sign of number
    val = index(n)
 
    print(n,"is",s[val])
 
 
# driver program to
# test the above function
check(30)
check(-20)
check(0)
     
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program to find if a number is
// positive, negative or zero using
// bit wise operators.
using System;
 
class GFG {
     
    // function to return 1 if it is zero
    // returns 0 if it is negative
    // returns 2 if it is positive
    static int index(int i)
    {
        return 1 + (i >> 31) - (-i >> 31);
    }
      
    static void check(int n)
    {
         
        // string array to store all kinds of number
        String []s = { "negative", "zero", "positive" };
      
        // function call to check the sign of number
        int val = index(n);
      
        Console.WriteLine(n + " is " + s[val]);
    }
     
    //Driver code
    public static void Main()
    {
        check(30);
        check(-20);
        check(0);
    }
}
 
// This code is contributed by Anant Agarwal.


PHP
> 31) -
               (-$i >> 31);
}
 
function check($n)
{
     
    // string array to store
    // all kinds of number
    $s = array("negative", "zero", "positive" );
 
    // function call to check
    // the sign of number
    $val = index($n);
 
    echo $n, " is ", $s[$val], "\n";
}
 
    // Driver Code
    check(30);
    check(-20);
    check(0);
 
// This code is contributed by Ajit
?>


Javascript


输出:

30 is positive
-20 is negative
0 is zero