Checking whether a number is positive or negative using bitwise operators
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In two's complement representation (used by virtually all modern CPUs), the most significant bit (MSB) is the sign bit: 0 for non-negative numbers and 1 for negative numbers. To check if a number is negative using bitwise operators, right-shift it by the number of bits minus one (n >> 31 for 32-bit integers) and check if the result is -1 or 1. Alternatively, AND the number with the sign bit mask (n & (1 << 31)). These bitwise checks are marginally faster than comparison operators but are primarily useful in low-level programming, embedded systems, and interview questions.
Two's Complement Representation
In a 32-bit signed integer, bit 31 (the leftmost) is the sign bit. Positive numbers have 0 as the MSB, negative numbers have 1. Zero has a sign bit of 0, so bitwise checks classify it as non-negative.
Method 1: Right Shift
n >> 31 performs an arithmetic right shift, filling new bits with the sign bit. For negative numbers, the result is all 1s (which is -1 in two's complement). ANDing with 1 isolates the sign bit.
Method 2: AND with Sign Bit Mask
1 << 31 creates a mask with only bit 31 set (10000000...0). ANDing with this mask extracts the sign bit. If non-zero, the number is negative.
Method 3: XOR-Based Sign Comparison
XOR produces a 1 in each bit position where the inputs differ. If two numbers have different signs, their MSBs differ, making the XOR result negative.
Implementation in Multiple Languages
Branchless Absolute Value
The mask is 0x00000000 for non-negative numbers (no effect) and 0xFFFFFFFF for negative numbers (inverts all bits). This is a classic branchless optimization used in performance-critical code.
Branchless Min/Max Using Sign Bit
Common Pitfalls
- Zero is classified as non-negative: Zero's sign bit is
0, so(0 >> 31) & 1returns0. If your logic needs to distinguish positive, negative, and zero as three categories, add a separate zero check:n == 0. - Undefined behavior with signed right shift in C: The C standard does not guarantee arithmetic right shift for signed integers — it is implementation-defined. Most compilers (GCC, Clang, MSVC) use arithmetic right shift, but portable code should avoid relying on this. Use
(n & (1 << 31)) != 0instead. - Integer overflow in subtraction:
a - boverflows whenaandbhave large opposite signs (e.g.,INT_MAX - INT_MIN). Branchless min/max using subtraction fails in these edge cases. Check for overflow or cast to a wider type first. - Python integers have arbitrary precision: Python
intis not fixed-width, son >> 31does not isolate the sign bit for numbers larger than 32 bits. Negative numbers in Python have infinite leading 1s conceptually. Usen < 0for sign checking in Python. - Logical vs arithmetic right shift: In Java,
>>is arithmetic (sign-extending) and>>>is logical (zero-filling).(-1 >>> 31)returns1, while(-1 >> 31)returns-1. In C, there is only>>and its behavior depends on the implementation for signed types.
Summary
- The sign bit (MSB) is
0for non-negative and1for negative in two's complement - Use
(n >> 31) & 1to extract the sign bit (assumes arithmetic right shift) - Use
n & (1 << 31)for a portable sign bit check in C - XOR of two numbers reveals whether they have different signs
- Branchless absolute value uses
mask = n >> 31; (n ^ mask) - mask - These techniques are useful in embedded systems and performance-critical code, but prefer
n < 0for general-purpose readability

