Advertisement

! or XOR

Started by February 09, 2000 11:27 AM
11 comments, last by daBomb 24 years, 10 months ago
Hi All! I am just curious about something. Usually if I would need to flip a bool variable I would do the following: BOOL bVariable; bVariable = !bVariable; But the other day, I was looking over bitwise operators, and realized that the above statement could be re-written as: BOOL bVariable; bVariable = bVariable ^ 1; I know that these two statements are equivelant, but I was curious if one was more optimal than the other. If I had to guess, I would say that bitwise is better. But would there actually be any noticeable difference between the two, or would it just be preferance. Thanks :-)
You should use the ! for your logical negation. XOR will only work if your BOOL is 1, which would be non-portable. Some implementations of bool can be equal to -1. For example, if you use the return value a function that returns 0 on success and an error code otherwise !function() would mean something different from (function() ^ 1). Remember true values are simply non-zero ones.
Advertisement
portability isn''t necessarily a big issue unless you want your game to be an open source project...but of course, they all should! ;-)
----------------------------------------Look behind you! A three-headed monkey!
On this topic, I have been wondering something,
I''m pretty certain (correct me if I''m wrong) that
BOOL''s are one byte big, if this is the case -
when a BOOL is true, are all the bits set or just
the LSB?
In theory, If all the bit''s were set would it be
possible to do a ~ on the variable (one''s complement)
and that should change it''s state.

Not sure if I''m clear on that, but let us know....

-Mezz
Using MSVC 6 bool is one byte big, however BOOL (as declares somewhere in the windows headers...) is 4 bytes big.
For the first post, ! is a binary operation for integers too (iirc oc). However, it usually won''t flip it (like the other poster said). As for the last post, bools are usually byte-sized (at least Borland compilers implement a "longbool" type too, but I don''t know what its purpose is), and I think most compilers define true as 1 and false as 0.
Advertisement
Just a quick note on my previous post: all versions before Visual C++ 5.0 (<=4.2) implented bool as an integer (typedef). All versions after 4.2 is using bool as a built in data type..but, im off topic here...the question was concerning BOOL, not bool.
Actually Borland and Microsoft compilers define true differently. I think Borland uses all bits as one and Microsoft uses least significant bit as one. Interoperating between Borland and MS code you have to do silly things like if !MSBool == !BorlandBool. (You wouldn't believe how much time I wasted debugging before I found that out.) Whereas within a compiler you can usually do a direct comparison.

This refers only to the native "bool" types though.

Edited by - SiCrane on 2/9/00 2:47:52 PM
Hmmm. Interesting. On the Mac, all compilers treat true as -1. I think that''s because in mac assembly, you can use the Scc command (Set on condition code) to set a register to 0 or -1 (all 1''s).

E:cb woof!
E:cb woof!
You can''t assume that the return values of boolean type on a Mac are -1, though. GL calls (which finally got put on the darn machines) will still return 1 for true values.

This topic is closed to new replies.

Advertisement