Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Simple C language verification

Simple C language verification
Thread Tools
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
May 21, 2001, 07:29 PM
 
Hello ...

Correct me if I'm wrong about the way these logical expressions are handled:

this & that: Compiler evaluates "this" and "that", then the logical conjunction of the results.
this && that: Compiler evaluates "this", but doesn't bother to evaluate "that" if "this" is false (since "FALSE and <anything>" = FALSE).

Similarly, "this | that" versus "this || that": the former computes both components of the disjunction, then the disjunction; the latter doesn't compute "that" if "this" is already TRUE.

Right?

I've written little test programs that bear out the properties described above; but, then again, the tests were designed with those expected behaviours in mind, so I wanted to check here to make sure I wasn't missing some more subtle aspect. My trusty, dusty C reference book only discusses "&&" and "||", as do the handful of on-line references I've consulted.


Thanks and regards,

DayLateDon
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
May 21, 2001, 08:46 PM
 
& and | (And ^, ~, >>, <<, ~) are bitwise operators. They work with individual bits, and give you the result of the binary operation for each bit.

AND (& takes each corresponding bit from the two values, compares it, and the result is the boolean AND value.

i.e.

01111
& 11100
= 01100 (15 & 28 = 12)

The OR (|) works much in the same way (except ORs the bits).
The XOR (^) works the same way, but XORs the bits.
The one's compliment (~) operator flips all bits in a value.

The shifting operators (>> and << ) shift the bits to the right and left.

i.e.

2 << 2 =
0010b << 2 =
1000b = 8.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
May 21, 2001, 10:01 PM
 
Thanks, Parallax!

So, I'm right (after a fashion), since by thinking in terms of logical expressions, I was tacitly restricting the "this" and "that" of my discussion to the single-bit values "0" and "1".

Of course, I see that, since C allows any non-zero value to represent "true", the blurring of bitwise and logical operators can be fatal. (Luckily, none of my code falls victim to the potential problems.) I'm glad I got that cleared up.

In fact, I'm suddenly aware that bitwise operators may be just what I need to streamline some other aspects of my code. Hmmmm ... back to work!


Thanks again ...

DayLateDon


     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:12 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2