You are here

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?

 

 

Comments

Bob Marlowe's picture

When studying the datasheet for the pin-component (why should I, its easy: a pin is a pin) I stumbled to my astonishment over the sentence "this is a PORT-component". So, when several pins (more than one!) on the same port are combined in this component ALL values are returned by a Pin_Read().
Seemingly the Tilde-guys are right, returned is a bit pattern and so the operator should be a bit operator.

yfs's picture

I agree, Bob. If you want to toggle a true "flag" then the logical operator is perfectly functional and makes the code obvious. As soon as the entity you are working on is more than a flag, you have to go with the bit-wise operator.

pkx's picture

Is it possible to add a footnote to the video to explain that there is another way to accomplish the inversion?

Soniya Singh's picture

Such a amazing information if you are looking for fallout shelter hack online and free of cost. You can get fallout shelter hack from here without downloading any spam file which may harm your system.

ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.