1290. Subtract 1 from a Number Without - Operator

EasyBit ManipulationBit ManipulationTwo's ComplementArithmetic

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

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

Output: Return n - 1 as an integer.

Examples

Example 1
Input: {"n":7}
Output: 6
Explanation: ~(-7) = 6 = 7 - 1.
Example 2
Input: {"n":1}
Output: 0
Explanation: ~(-1) = 0 = 1 - 1.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →