# Add 1 to a given number using Java

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.

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. The same analogy holds for decrement. Note that this method works only if the numbers are stored in 2’s complement form. Like addition, Write a function Add() that returns sum of two integers. To subtract 1 from a number x (say 0011001000), flip all the bits after the rightmost 1 bit (we get 0011001 111). Finally, flip the rightmost 1 bit also (we get 0011000111) to get the answer. The function should not use any of the arithmetic operators (+, ++, –, -, … etc). Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result. The idea is to use subtractor logic. Write a program to subtract one from a given number. The idea is to use bitwise operators. Like addition, Write a function Add() that returns sum of two integers. The function should not use any of the arithmetic operators (+, ++, –, -, … etc). Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result. The idea is to use subtractor logic. The use of operators like ‘+’, ‘-‘, ‘*’, ‘/’, ‘++’, ‘–‘ …etc. are not allowed.

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.

`// 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` `));`

` ` `}`

`}`

Output:

14