Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.
The use of the ~ operator instead of ! means, in my interpretation, that you take the 1's compliment of X and, if that is non-zero, then perform the assignment.
To my mind, the danger in using ~ is because of how C treats 0 as false, but anything non-zero as true.
Let's say X is 0x01 which C would treat as boolean true (as it is non-zero). !X would therefore be treated as false (because C would see this as 'not true'). However ~X would be 0xFE which C would also see as 'true'.
Therefore the expression you show will ONLY NOT be true when X is 0xff (assuming 8-bit values - extend as required for more bits), and would be clearer if written as:
if(x!=0xff) y=z;
Susan
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.