Advertisement

Another Which is Faster

Started by November 29, 2000 12:30 PM
11 comments, last by MicahJon 24 years, 1 month ago
Magmai,

My C++ book says that a bool is a two state type. It can only have true or false. When converting a bool to an int, true equates to 1, false to 0. When converting an int to a bool, zero equates to false, non-zero to true. A quick look at the disassembly shows that Borland converts ints to bools by doing a TEST, SETNZ, and an AND, rather than just a MOV, ensuring that bool only has 1 or 0 as its values.

Thanks,
Micah
One reason to use if (!var) style rather than if (var == false):

Less errors because you avoid that double =''s, in which case you might only type in one.

Of course, thats only one reason, its your decision and style, but thats why i use ! style.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

The Micro$haft BSOD T-Shirt
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
Advertisement
ImmaGNUman,

I''ve heard that MSVC doesn''t have this feature, but Borland warns me when I use if(fred = 3) instead of if(fred == 3) by saying "Possibly incorrect assignment".

Otherwise I''d either use if(!x), or if(3 == fred), as no compiler should let you get away with if(3 = fred).

Thanks all
Micah.

This topic is closed to new replies.

Advertisement