0

my code is having a bug here and i don't know how to fix it, so if you could please help me that'd be great.

Here's my code:

unsigned reverse(unsigned value)
{
    unsigned res;
    int l_mask, r_mask;
    l_mask = 0x00000002, r_mask = 0x40000000;
    for(res = 0; r_mask != 0x00000001; r_mask >>=1, l_mask <<= 1)
        l_mask & value == 0 ? res &= ~r_mask : res |= r_mask;
    return res;
}

The error is:

lvalue required as left operand of assignment

I've seen another posts and questions but nothing seems to be related to the problem that I'm having.

If anyone could help me I would be very appreciated

  • Always tag with the programming language you're using. The tags you've chosen have a grand total of 3 followers. (It would also help to include the full error message, including which line the error is on). – Wooble Apr 11 '14 at 18:42

1 Answers1

1

To fix this problem do that:

 l_mask & value == 0 ? (res &= ~r_mask) : (res |= r_mask);

Precedence of the ternary condition (?:) is higher than |= and &=. http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

nni6
  • 990
  • 6
  • 13