# Add 1 to a given number

Write a program to add one to a given number. The use of operators like ‘+’, ‘-‘, ‘*’, ‘/’, ‘++’, ‘–‘ …etc are not allowed.
Examples:

```Input:  12
Output: 13

Input:  6
Output: 7
```

This question can be approached by using some bit magic. Following are different methods to achieve the same using bitwise operators.

Method 1
To add 1 to a number x (say 0011000111), flip all the bits after the rightmost 0 bit (we get 0011000000). Finally, flip the rightmost 0 bit also (we get 0011001000) to get the answer.

## C++

 `// C++ code to add add  ` `// one to a given number  ` `#include ` `using` `namespace` `std; ` ` `  `int` `addOne(``int` `x)  ` `{  ` `    ``int` `m = 1;  ` `     `  `    ``// Flip all the set bits  ` `    ``// until we find a 0  ` `    ``while``( x & m )  ` `    ``{  ` `        ``x = x ^ m;  ` `        ``m <<= 1;  ` `    ``}  ` `     `  `    ``// flip the rightmost 0 bit  ` `    ``x = x ^ m;  ` `    ``return` `x;  ` `}  ` ` `  `/* Driver program to test above functions*/` `int` `main()  ` `{  ` `    ``cout<

## C

 `// C++ code to add add ` `// one to a given number  ` `#include ` ` `  `int` `addOne(``int` `x) ` `{ ` `    ``int` `m = 1; ` `     `  `    ``// Flip all the set bits  ` `    ``// until we find a 0  ` `    ``while``( x & m ) ` `    ``{ ` `        ``x = x ^ m; ` `        ``m <<= 1; ` `    ``} ` `     `  `    ``// flip the rightmost 0 bit  ` `    ``x = x ^ m; ` `    ``return` `x; ` `} ` ` `  `/* Driver program to test above functions*/` `int` `main() ` `{ ` `    ``printf``(``"%d"``, addOne(13)); ` `    ``getchar``(); ` `    ``return` `0; ` `} `

## Java

 `// Java code to add add ` `// one to a given number  ` `class` `GFG { ` ` `  `    ``static` `int` `addOne(``int` `x) ` `    ``{ ` `        ``int` `m = ``1``; ` `         `  `        ``// Flip all the set bits  ` `        ``// until we find a 0  ` `        ``while``( (``int``)(x & m) >= ``1``) ` `        ``{ ` `            ``x = x ^ m; ` `            ``m <<= ``1``; ` `        ``} ` `     `  `        ``// flip the rightmost 0 bit  ` `        ``x = x ^ m; ` `        ``return` `x; ` `    ``} ` `     `  `    ``/* Driver program to test above functions*/` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``System.out.println(addOne(``13``)); ` `    ``} ` `} ` ` `  `// This code is contributed by prerna saini. `

## Python3

 `# Python3 code to add 1 ` `# one to a given number  ` `def` `addOne(x) : ` `     `  `    ``m ``=` `1``; ` `    ``# Flip all the set bits ` `    ``# until we find a 0  ` `    ``while``(x & m): ` `        ``x ``=` `x ^ m ` `        ``m <<``=` `1` `     `  `    ``# flip the rightmost  ` `    ``# 0 bit  ` `    ``x ``=` `x ^ m ` `    ``return` `x ` ` `  `# Driver program ` `n ``=` `13` `print` `addOne(n) ` ` `  `# This code is contributed by Prerna Saini. `

## C#

 `// C# code to add one ` `// to a given number  ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``static` `int` `addOne(``int` `x) ` `    ``{ ` `        ``int` `m = 1; ` `         `  `        ``// Flip all the set bits  ` `        ``// until we find a 0  ` `        ``while``( (``int``)(x & m) == 1) ` `        ``{ ` `            ``x = x ^ m; ` `            ``m <<= 1; ` `        ``} ` `     `  `        ``// flip the rightmost 0 bit  ` `        ``x = x ^ m; ` `        ``return` `x; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``Console.WriteLine(addOne(13)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

`14`

Method 2
We know that the negative number is represented in 2’s complement form on most of the architectures. We have the following lemma hold for 2’s complement representation of signed numbers.

Say, x is numerical value of a number, then

~x = -(x+1) [ ~ is for bitwise complement ]

(x + 1) is due to addition of 1 in 2’s complement conversion

To get (x + 1) apply negation once again. So, the final expression becomes (-(~x)).

## C++

 `#include ` `using` `namespace` `std; ` ` `  `int` `addOne(``int` `x)  ` `{  ` `    ``return` `(-(~x));  ` `}  ` ` `  `/* Driver code*/` `int` `main()  ` `{  ` `    ``cout<

## C

 `#include ` ` `  `int` `addOne(``int` `x) ` `{ ` `   ``return` `(-(~x)); ` `} ` ` `  `/* Driver program to test above functions*/` `int` `main() ` `{ ` `  ``printf``(``"%d"``, addOne(13)); ` `  ``getchar``(); ` `  ``return` `0; ` `} `

## Java

 `// Java code to Add 1 to a given number ` `class` `GFG ` `{ ` `    ``static` `int` `addOne(``int` `x) ` `    ``{ ` `         ``return` `(-(~x)); ` `    ``} ` `     `  `    ``// Driver program ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``System.out.printf(``"%d"``, addOne(``13``)); ` `    ``} ` `} ` ` `  `// This code is contributed  ` `// by Smitha Dinesh Semwal `

## Python3

 `# Python3 code to add 1 to a given number ` ` `  `def` `addOne(x): ` `    ``return` `(``-``(~x)); ` ` `  ` `  `# Driver program  ` `print``(addOne(``13``)) ` ` `  `# This code is contributed by Smitha Dinesh Semwal `

## C#

 `// C# code to Add 1  ` `// to a given number ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``static` `int` `addOne(``int` `x) ` `    ``{ ` `        ``return` `(-(~x)); ` `    ``} ` `     `  `    ``// Driver program ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``Console.WriteLine(addOne(13)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

`14`

Example :

```Assume the machine word length is one *nibble* for simplicity.
And x = 2 (0010),
~x = ~2 = 1101 (13 numerical)
-~x = -1101
```

Interpreting bits 1101 in 2’s complement form yields numerical value as -(2^4 – 13) = -3. Applying ‘-‘ on the result leaves 3. Same analogy holds for decrement. Note that this method works only if the numbers are stored in 2’s complement form.

Thanks to Venki for suggesting this method.

Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem

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

Improved By : vt_m, rathbhupendra, mfkasim

Article Tags :
Practice Tags :

12

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