 |
 |
Simple C language verification
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
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
|
|
& 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 =
00 10b << 2 =
1000b = 8.
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|