使用位运算符检查数字是否为正、负或零

给定一个数字N,在不使用条件语句的情况下检查它是正、负还是零。 例如:

null
Input : 30Output : 30 is positiveInput : -20Output : -20 is negativeInput: 0Output: 0 is zero

符号移位n>>31将每个负数转换为-1,将每个负数转换为0。 当我们做-n>>31时,如果它是一个正数,那么它会像我们做-n>>31时一样返回-1,反之亦然,当我们做一个负数时。 但是当我们为0计算时,n>>31和-n>>31都返回0,所以我们得到一个公式:

1+(n>>31)–(-n>>31)

..1) 当n为负时 : 1 +(-1)-0=0 ..2) 当n为正时: 1+0-(-1)=2 ..3) 当n为0时: 1+0-0=1 所以我们知道当它是负数时返回0,当它是零时返回1,当它是正数时返回2。 所以不要使用我们存储在第0个索引“负”、第1个索引“零”和第2个索引“正”的if语句,并根据它打印。

CPP

// CPP program to find if a number is
// positive, negative or zero using
// bit wise operators.
#include <iostream>
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

<?php
// PHP 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
function index( $i )
{
return 1 + ( $i >> 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 ], "" ;
}
// Driver Code
check(30);
check(-20);
check(0);
// This code is contributed by Ajit
?>


Javascript

<script>
// JavaScript 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
function index(i)
{
return 1 + (i >> 31) - (-i >> 31);
}
function check(n)
{
// string array to store all kinds of number
let s = [ "negative" , "zero" , "positive" ];
// function call to check the sign of number
let val = index(n);
document.write(n + " is " + s[val] + "<br>" );
}
// driver program to test the above function
check(30);
check(-20);
check(0);
// This code is contributed by Surbhi Tyagi
</script>


输出:

30 is positive-20 is negative0 is zero

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享