Pling(!) or Tilde(~)? | Cypress Semiconductor
Pling(!) or Tilde(~)?
If you are following the PSoC Creator 101 video series you will have seen, in today's lesson 4, a way of toggling a PSoC pin from firmware. The code does a read of the pin's current state, inverts it, and writes the new state back. It looks like this:
LED_Write( ! LED_Read() );
Pretty simple, huh? Well we thought so until the video got reviewed by our Applications team. Even though the code works perfectly, some of those guys felt that the right way to do this was with a bitwise operator:
LED_Write( ~ LED_Read() );
This started a good old-fashioned turf war. Which is the right operator to use; pling(!) or tilde(~)? Note that the Write API expects a uint8 (char) argument and the Read API returns the same type.
The bitwise proponents claimed that ~ is the true inverting operation because it flips all bits in the byte. It is commutative. Most of us had trouble spelling and pronouncing that word but we all agreed that it did sound rather fancy.
The logical guys claimed that it is a true/false operation (the state of the bit) and so the ! operator is more readable . This just made the tilde guys bit-ter because the pling argument was unfairly reusing the word logical to refer to their position as well as the operator. It was getting feisty! Or was it feisty~?
We never really resolved the argument but one bright spark did point out that the pin component is actually a pins component and can support multiple pins from a single instance. This messed everything up because, in that situation, the bitwise operator is definitely the right one to use. If you use pling then the first write will clear all the top bits permanently. That would constitute a real code defect, as opposed to a philosophical issue.
So, what's your take? Both methods pass my if it ain't broke, don't fix it criteria and, since the videos are about PSoC rather than C coding, we elected not to re-record the video. Should we?