- How to check whether a number is negative without using
if - How to identify negative, zero, and positive values
- How the arithmetic right-shift operator works on signed integers
- How to use an array index to select a result
- Why bitwise sign tests can be implementation-dependent in C
How to check a number without 'if'
A number can be negative, zero, or positive. Normally, we use an if...else statement to test these cases. In this assignment, we avoid if by calculating an index and using it to select a label from an array.
const char *str[] = { "Negative", "Zero", "Positive" };
The array has three labels: index 0 is "Negative", index 1 is "Zero", and index 2 is "Positive". The program calculates which index to use from the input number.
- Arithmetic right-shift method:
numType = (n >> (BITS - 1)) - (-n >> (BITS - 1)) + 1; - Sign-bit mask method:
numType = ((n >> (BITS - 1)) & 1) + (n != 0);
1, while the sign bit of zero or a positive number is 0; shifting and arithmetic can turn that information into an array index.
Method 1 — arithmetic right shift
On a typical two’s-complement system, right-shifting a negative signed integer by BITS - 1 produces -1, while right-shifting zero or a positive integer produces 0. This behavior is commonly used in bitwise sign tests, but C does not guarantee it for every implementation.
Trace with n = -10:
| Step | Operation | Result | Note |
|---|---|---|---|
| 0 | Initial value | n = -10 | The input is negative |
| 1 | n >> (BITS - 1) | -1 | Arithmetic right shift preserves the sign |
| 2 | -n >> (BITS - 1) | 0 | 10 is positive, so the shifted result is 0 |
| 3 | 1 + (-1) - 0 | 0 | Index 0 selects "Negative" |
Therefore, the program prints -10 is Negative.
Method 2 — sign-bit mask
A more portable approach is to inspect the sign bit directly. First, convert the signed integer to an unsigned integer, then shift the sign bit into the lowest-bit position and mask it with 1.
unsigned int sign = ((unsigned int)n >> (BITS - 1)) & 1;
If sign is 1, the number is negative. If it is 0, the number is either zero or positive. We then distinguish zero from positive using the expression n != 0, which evaluates to 1 for nonzero values and 0 for zero.
Trace with n = 25:
| Step | Operation | Result | Note |
|---|---|---|---|
| 0 | Initial value | n = 25 | The input is positive |
| 1 | ((unsigned int)n >> (BITS - 1)) & 1 | 0 | The sign bit is 0 |
| 2 | n != 0 | 1 | 25 is not zero |
| 3 | 0 + 1 | 1 | Index 1 selects "Zero" in the original array |
To make this method select the correct label, use the array order { "Zero", "Positive", "Negative" }, or adjust the index formula accordingly.
Complete C Programs
Program 1 — using arithmetic right shift:
Program 1: Check negative, zero, or positive using shift
#include <stdio.h>
#define BITS (sizeof(int) * 8)
int main(void) {
int n;
int numType;
const char *str[] = { "Negative", "Zero", "Positive" };
printf("Enter any number: ");
scanf("%d", &n);
numType = (n >> (BITS - 1)) - (-n >> (BITS - 1)) + 1;
printf("%d is %s\n", n, str[numType]);
return 0;
}
Program 2 — using a portable sign-bit mask:
Program 2: Check negative, zero, or positive using sign-bit mask
#include <stdio.h>
#include <limits.h>
#define BITS (sizeof(int) * CHAR_BIT)
int main(void) {
int n;
unsigned int sign;
int numType;
const char *str[] = { "Negative", "Zero", "Positive" };
printf("Enter any number: ");
scanf("%d", &n);
sign = ((unsigned int)n >> (BITS - 1)) & 1;
numType = sign + (n != 0);
printf("%d is %s\n", n, str[numType]);
return 0;
}
Program 1 is shorter and matches the classic bitwise puzzle. Program 2 is more portable because it does not depend on the behavior of right-shifting a negative signed integer.
Program Output
Running Program 1 with -10:
Enter any number: -10
-10 is Negative
The arithmetic right shift produces -1 for a negative value, so the calculated index becomes 0.
Running Program 1 with 25:
Enter any number: 25
25 is Positive
For a positive number, the first shift produces 0 and the second produces -1, so the calculated index becomes 2.
Running Program 1 with 0:
Enter any number: 0
0 is Zero
For zero, both shifted values are 0, so the calculated index becomes 1.
Step-by-Step Explanation
1. Reading input: scanf("%d", &n) reads an integer from the user and stores it in n. The & operator gives scanf() the address of n.
2. Why the shift method works: BITS is the number of bits in an int, commonly 32. Shifting a negative signed value right by BITS - 1 fills the result with sign bits, producing -1 on typical two’s-complement systems.
3. Why the formula produces 0, 1, and 2: For a negative number, n >> (BITS - 1) is -1 and -n >> (BITS - 1) is 0, giving 1 + (-1) - 0 = 0. For zero, both are 0, giving 1. For a positive number, the first is 0 and the second is -1, giving 1 + 0 - (-1) = 2.
4. Why the array works: The array str[] stores the labels in the exact order needed by the calculated index: "Negative" at index 0, "Zero" at index 1, and "Positive" at index 2.
5. Why the sign-bit method is safer: Converting the value to unsigned int before shifting makes the right shift well-defined. The mask & 1 extracts only the sign bit, avoiding reliance on implementation-defined arithmetic shift behavior.
Warnings and caveats
Before using bitwise sign tests, understand their limitations:
- Implementation-defined shift. The result of right-shifting a negative signed integer is implementation-defined, so Program 1 may not behave identically on every compiler or platform.
- Overflow in
-n. IfnisINT_MIN, then-noverflows because its positive equivalent cannot be represented in anint. - Array bounds. The calculated index must always be 0, 1, or 2. An incorrect formula can access outside the array and cause undefined behavior.
- Readability. Bit manipulation is compact but harder to understand than a simple
if...elsestatement. - Production code. For real applications, use
if (n < 0),else if (n == 0), andelse. It is clearer, safer, and easier to maintain.
The bitwise method is excellent for learning how signed integers and shift operators work, but it should not be preferred over straightforward conditional statements in normal application code.
Frequently Asked Questions
1. Can we check a negative number without using if?
Yes. Use bitwise shifting and an array index. For example, calculate an index of 0 for negative, 1 for zero, and 2 for positive, then print the matching label.
2. What does n >> (BITS - 1) do?
It shifts the number right by one less than its total bit width. On many systems, this leaves only the sign information: -1 for a negative signed value and 0 for zero or a positive value.
3. Why is the result implementation-defined?
Because C does not specify whether right-shifting a negative signed integer performs an arithmetic shift or a logical shift. The result depends on the compiler and platform.
4. Does this program work for zero?
Yes. Zero is neither negative nor positive. The program assigns it index 1 and prints Zero.
5. Which version should be used in real projects?
Use a simple if...else statement for real projects. It is easier to read, avoids implementation-defined behavior, and clearly expresses the intended logic.
💡 Tip: Test the program with -1, 0, 1, INT_MAX, and INT_MIN. The INT_MIN case is especially important because negating it causes signed integer overflow.