1

Working on an assignment that asks to rewrite all basic arithmetic operations(add/subtract/multiply) only using the bit-wise operators. One of the incomplete methods has the following description " returns the integer whose binary representation is the same as that of i in reverse. " I've successfully rewritten the add(),subtract() and multiply functions, but need suggestions as to how approach this one. As of right now this is all I have..

 unsigned int reverse(unsigned int i)
{
    {
        int reverse;
        while (i != 0)
        {
            reverse = reverse * 10;
            reverse = reverse + i%10;
            i = i/10;
        }
        return i;
     }
}
Roma Dan
  • 23
  • 4
  • Why have `i` as unsigned and `reverse` as signed? Also you return `i`; don't you want to return `reverse`? – MicroVirus Sep 23 '14 at 16:10
  • 1
    If you are supposed to be working with binary representation, then you shouldn't multiply and divide by 10 (which would be for decimal representation). – interjay Sep 23 '14 at 16:11
  • Maybe this is your answer: http://stackoverflow.com/questions/845772/how-to-check-if-the-binary-representation-of-an-integer-is-a-palindrome – Alexxx Sep 23 '14 at 16:13
  • You did things for decimal. For binary, replace all `10`s with `2`s and make some corrections in your code as per comments above. – Mohit Jain Sep 23 '14 at 16:16
  • I think "reverse" isn't well-defined. Could mean "reversed as represented in base B" (where B could be 10 or 2 or anything else) or "the binary representation is reversed" (ignoring endianness etc). – mafso Sep 23 '14 at 16:23
  • http://stackoverflow.com/questions/63776/bit-reversal-of-an-integer-ignoring-integer-size-and-endianness?rq=1 http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer?rq=1 http://stackoverflow.com/questions/12903359/c-reverse-binary?lq=1 – phuclv Sep 23 '14 at 17:50

1 Answers1

1

If you are looking to build a new binary representation, you should use binary operations:

  • Use << to multiply by 2
  • Use & 1 to determine the value of the least significant bit
  • Use bitwise OR | instead of addition to set the least significant bit
  • Use >> to divide by 2

Once you rewrite your code with these changes, you would get the expected results.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • we shouldn't be doing other peoples home work.... and he only has a rep of 3, so it's even more pointless (so to speak). – AnthonyLambert Sep 23 '14 at 16:17
  • and they can't even be bothered to search for the answers given last time the homework was set? Whose homework is it? Some Uni? – AnthonyLambert Sep 23 '14 at 16:19
  • @AnthonyLambert Giving hints is OK. It's writing other people's code that's not OK. I think OP is reasonably close. Once he understands what's wrong with his code, he should be able to fix it using these hints. I don't think there's anything wrong with it: I did not realize that shifting has anything to do with multiplying or dividing by 2, and it took me several minutes to convince myself that that's what is going on after somebody has pointed it out to me. – Sergey Kalinichenko Sep 23 '14 at 16:20
  • I think your hint is about right.... but this place is increasingly full of people asking about homework or questions where they clearly haven't even picked up a programming book and I don't like it. – AnthonyLambert Sep 24 '14 at 09:30
  • @anthonylambert the book my professor uses did not cover this and I could not find tools online so I asked for a hint, you don't have to respond if you don't want to. – Roma Dan Sep 24 '14 at 21:05