26

When I see a coworker do this:

if( recValues[0] == '0' )
{
recValues[0] = '1';
}
else if( recValues[0] == '1' )
{
recValues[0] = '0';
}

I replace it with this:

recValues[0] ^= 0x1;

Note: The recValues[0] is guaranteed to contain only '0' or '1'

Comments
  • 4
    Or you can write:
    recValues[0]=(recValues[0]+1)%2;
  • 8
    what kind of devil language is this
  • 14
    If 0 and 1 are considered booleans in that language you could just negate them:

    recvalues[0] = ! recvalues[0];
  • 2
    recvalues[0] = Math.Abs(recvalues[0]-1);
    Math is everything
  • 2
    @matyjb recvalues[0] = 1 - recvalues[0]. That's how I'm used to see it anyway :)
  • 0
    I could agree with you, but typing 0x1 instead of just 1 (recValues ^= 1) is just to make it look unreadable on purpose, isn't it?
  • 0
    @Huuugo am I the only one that thinks this is a good answer?
  • 7
    Oh wait. It just hit me. The array contains chars! WTF.
    '0' ! = 0
    None of the answers above are correct (including mine), are they?

    If so,
    x = x == '0' ? '1' : '0'
    Would be the easiest
  • 1
    recValues [0] = (recValues [0]==='0') ? '1' : '0';
  • 2
    @Joserc87 More out of habit. I'm a Computer Engineer, so I use hex a lot.
    I also use bitwise operators a lot, so that's where the solution comes from.
    In the actual implementation I wrapped 0x1 in a macro with an easy-to-read name so nobody wonders what the 0x1 does.
  • 0
    I mean, yes: Using bitwise operators IS a little bit more unreadable. But the speedup in compiled code is significant -- especially if it gets executed inside a loop. Usually when I use bitwise operators I put a comment in.
  • 0
    If the performance matters then yes, totally, get rid of the conditional jumps!
    But then again, if it really matters and it's in an algorithm you can bundle that array into a byte or a word? I'm curious about what this actually does
  • 1
    @Joserc87 Switches a '0' to a '1' and vice versa.
    The actual code was part of a file I/O program. The actual program is meant to toggle a GPIO pin, on or off.
    So the program has to: Read the bit, change it, then write it back.
  • 2
    rectValue[0]= 1- rectValue[0]
    Simple enough
  • 2
    At first I really thought the program was wrong because of the char to int jump.. but it's right. I'm an amateur embedded systems engineer from the CS world. It took me a bit to realize you are just flipping the last bit of the character. Duh!
  • 2
    Why not x = !x ?

    Or, funnier:

    x = |x - 1|

    x = cos(M_PI_2 * x)

    Ok that's it :P
  • 0
    I compiled the top-suggested alternative solutions. The conditional assignment is only a little bit faster than if/else. Two assembly instructions fewer, to be exact.
    The bitwise operation was 30% faster than the conditional assignment.
  • 0
    Why does the bitwise operator work there? The values are char, so just arbitrary ASCII bit patterns... Seems like a coincidence.
  • 0
    '0' is 0x30 and '1' is 0x31 in ASCII
    The ASCII characters were arranged so you can do things like iterate through them in ascending order. Other cool things too:
    'a' is 0x61
    'A' is 0x41
    so to convert between lower case and upper case just add or subtract 0x20.
  • 0
    in binary:
    '1' is 0011 0000
    '0' is 0011 0001

    so Exclusive-Or either with 1 flips bit 0 and only bit zero
Add Comment