QUESTION
Write a program to add one to a given number. You are not allowed to use operators like +, -, *, /, ++, etc.\n\nExamples:\nInput: 12\nOutput: 13\n\nInput: 6\nOutput: 7\n\nYes, you guessed it right, we can use bitwise operators to achieve this. Following are different methods to achieve same using bitwise operators.
ANSWER
#include<stdio.h>
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()
{
int x;
scanf("%d",&x);
printf("%d", addOne(x));
// getchar();
return 0;
}