# Check if a number is positive, negative or zero using bit operators

Given a number N, check if it is positive, negative or zero without using conditional statements.

Examples:

```Input : 30
Output : 30 is positive

Input : -20
Output : -20 is negative

Input: 0
Output: 0 is zero
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The signed shift n>>31 converts every negative number into -1 and every other into 0.
When we do a -n>>31, if it is a positive number then it will return -1 as we are doing -n>>31 and the vice versa when we do for a negative number.
But when we do for 0 then n>>31 and -n>>31 both returns 0, so we get a formula:

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

..1) when n is negative :
1 +(-1)-0=0

..2) when n is positive:
1+0-(-1)=2

..3) when n is 0:
1+0-0=1

So we know it returns 0 when it is a negative number, it returns 1 when it is zero, returns 2 when it is a positive number.

So to not use if statements we store at 0th index “negative”, 1st index “zero” and at 2nd index “positive”, and print according to it.

## 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 ` `?> `

Output:

```30 is positive
-20 is negative
0 is zero
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : jit_t

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.