-2

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?

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
Numberknot
  • 149
  • 1
  • 7
  • Your question doesn't make sense. Why would you use a bitwise operator to print numbers from 1 to 100? – Klas Lindbäck Aug 03 '15 at 08:06
  • 1
    doesn't make any sense?i don't think so. – Numberknot Aug 03 '15 at 08:09
  • 3
    @KlasLindbäck there is no practical purpose for this. No sane person would use bitwise operators to do such a simple thing. The purpose for this is to see whether the person knows about bitwise operators and what they do. Most probably this is some sort of the interview question. – Salvador Dali Aug 03 '15 at 08:12
  • 2
    I think this is an interesting question. – FreelanceConsultant Aug 03 '15 at 08:15
  • "I believe it is possible ..." followed by "send me ur codez". Can I say "I believe it is possible to predict tomorrow's lottery numbers" and ask for a listing of all methods as well? (I'd be very happy with just *one* that works.) – Jongware Aug 03 '15 at 09:03
  • Yes, this is surely interview/assignment garbage. It might be interesting, but I can't see how it's any kind of useful addition to a knowledge repo. I'm gonna downClose. – Martin James Aug 03 '15 at 10:39

1 Answers1

4

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.

Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753