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.

boolean negation with multibit operand

Status
Not open for further replies.

Mahammad

Member level 3
Member level 3
Joined
Jul 8, 2011
Messages
56
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,632
I came across this
If z=!x where ! Is boolean negation operator and x=1111 then z=0
Can any tell me how it is so .
And what would be z if x= 1010
 

The boolean negation operator, "!", is not a bit-wise operation. It deals with only two logical values, TRUE and FALSE. The in C language, FALSE is implemented with an integer value of 0 (zero). But TRUE could be implemented with any non-zero integer value. It could be 00000001 or it could be 11111111. So in your example, if x=1111, z = 0. And if x=1010, z also = 0. The only time z will not be zero is if x is zero. And then z might have any non-zero value, although most C compilers implement TRUE as the integer value "1". So if x is 0, z will probably be 1.

The safest way to treat boolean variables implemented as integers is never to examine the actual values, but only use those variables in logical expressions involving operators like "!", "&&", and "||". Otherwise you can get something really strange like this:

Code:
if( booleanA == booleanB )  doSomething();   //..Bad code!!!

Even if both booleanA and booleanB are true, this code might not call doSomething(), for example if booleanA=4 and booleanB=5. The only safe way to do what the above code is trying to do is to do this:
Code:
if((booleanA && booleanB) || (!booleanA && !booleanB))
    doSomething();
 

    V

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top