Last active
May 13, 2019 09:54
-
-
Save chhanganivarun/55a25e66e5e80ed509d0cdc188c46c01 to your computer and use it in GitHub Desktop.
increment and decrement without basic math operators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int increment(int *c) | |
{ | |
int x=1; | |
while((*c)&x) | |
{ | |
*c^=x; | |
x<<=1; | |
} | |
*c^=x; | |
return *c; | |
} | |
int decrement(int *c) | |
{ | |
int x=1; | |
while(!((*c)&x)&&x!=0) | |
{ | |
*c^=x; | |
x<<=1; | |
} | |
*c^=x; | |
return *c; | |
} | |
int main() | |
{ | |
for(int i = -2;i<=16;) | |
printf("%d\n",increment(&i)); | |
for(int i=16;i>=-2;) | |
printf("%d\n",decrement(&i)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment