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.


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;
  printf("%d", addOne(x));
 // getchar();
  return 0;
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO