Use Bitwise operator to add 1 to binary representation
Check the each bit and flip it using bitwise operator
Here, number is input and output both
int one = 1;
/* Flip all the set bits until we find a 0 */
while((number & one)!=0 ) {
number = number^one;
one <<= 1;
}
/* flip the rightmost 0 bit */
number = number^one;
No comments:
Post a Comment
Note: only a member of this blog may post a comment.