I believe it is possible to generate the numbers 1 to 100 using bitwise operations or bit manipulation, rather than by the traditional increment instruction.
What are the possible methods of doing this?
I believe it is possible to generate the numbers 1 to 100 using bitwise operations or bit manipulation, rather than by the traditional increment instruction.
What are the possible methods of doing this?
I believe there are a couple of ways to achieve it. One of them is to use tilde operator ~x
which is a bitwise complement and equal to -(x+1)
int increase(int x){
return (-(~x));
}
So increment the value with the above-mentioned function and print it.
Apart from my own answer, I found another way to make an addition with bitwise operators.
int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add( x ^ y, (x & y) << 1);
}
Just substitute y with 1 and you get another way of incrementing the value.