1289. Add 1 to a Number Without + Operator

EasyBit ManipulationBit ManipulationTwo's ComplementArithmetic

Given a non-negative integer n, return n + 1 without using the addition operator. In two's-complement arithmetic, n + 1 equals -(~n), i.e. the negation of the bitwise complement of n.

Input: A JSON object {"n": <non-negative integer>}.

Output: Return n + 1 as an integer.

Examples

Example 1
Input: {"n":7}
Output: 8
Explanation: ~7 = -8, and -(-8) = 8 = 7 + 1.
Example 2
Input: {"n":0}
Output: 1
Explanation: ~0 = -1, and -(-1) = 1.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →