Continue to Site

Welcome to EDAboard.com

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.

what this line means: if (~xxx)

Status
Not open for further replies.

KingMoshe

Member level 2
Joined
Aug 10, 2021
Messages
48
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
362
can someone help with this line, what it is means?
if (~x) y=z;
 

if NOT x, then y=z

So if x is assigned 1 further up, under any condition where x=0 then the value of z will be assigned to y
 

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
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top