Difference between revisions of "Boolean"
From MSX Game Library
(Created page with "In {{MSXGL}}, boolean can be defined using the type <tt>bool</tt>. While booleans are supposed to have only two values (true or false), here we use an 8-bit unsigned integer...") |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
In {{MSXGL}}, boolean can be defined using the type <tt>bool</tt>. | In {{MSXGL}}, boolean can be defined using the type <tt>bool</tt>. | ||
− | While booleans are supposed to have only two values (true or false), here we use an 8-bit unsigned integer to store this value.<br/> | + | While booleans are supposed to have only two values (''true'' or ''false''), here we use an 8-bit unsigned integer to store this value.<br/> |
Its value can therefore go from 0 to 255.<br/> | Its value can therefore go from 0 to 255.<br/> | ||
By convention, we use : | By convention, we use : | ||
* <tt>FALSE</tt> = 0 | * <tt>FALSE</tt> = 0 | ||
− | * <tt>TRUE</tt> = 1 (some engines use <tt>0xFF</tt> value) | + | * <tt>TRUE</tt> = 1 ''(some engines use <tt>0xFF</tt> value)'' |
⚠️ However, while the value <tt>FALSE</tt> (0) is guaranteed on all boolean function returns, '''the value <tt>TRUE</tt> (1) is never guaranteed''' : Any value other than <tt>FALSE</tt> (0) should be interpret as <tt>TRUE</tt>.<br/> | ⚠️ However, while the value <tt>FALSE</tt> (0) is guaranteed on all boolean function returns, '''the value <tt>TRUE</tt> (1) is never guaranteed''' : Any value other than <tt>FALSE</tt> (0) should be interpret as <tt>TRUE</tt>.<br/> | ||
This choice was made for performance reasons both when returning the value and also when testing the returned value. | This choice was made for performance reasons both when returning the value and also when testing the returned value. | ||
− | How to test that a boolean '<tt>b</tt>' is true: | + | How to test that a boolean '<tt>b</tt>' is ''true'': |
− | + | <syntaxhighlight lang="c"> | |
− | + | if(b) // Ok! | |
− | + | if(b != FALSE) // Ok! | |
− | + | if(b == TRUE) // Not Ok! (no guaranteed to work) | |
− | + | </syntaxhighlight> | |
+ | |||
+ | To test that it is ''false'': | ||
+ | <syntaxhighlight lang="c"> | ||
+ | if(!b) // Ok! | ||
+ | if(b == FALSE) // Ok! | ||
+ | if(b != TRUE) // Not Ok! (no guaranteed to work) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | In short, '''never use <tt>TRUE</tt> to check a boolean value'''. |
Latest revision as of 16:07, 24 March 2024
In MSXGL, boolean can be defined using the type bool.
While booleans are supposed to have only two values (true or false), here we use an 8-bit unsigned integer to store this value.
Its value can therefore go from 0 to 255.
By convention, we use :
- FALSE = 0
- TRUE = 1 (some engines use 0xFF value)
⚠️ However, while the value FALSE (0) is guaranteed on all boolean function returns, the value TRUE (1) is never guaranteed : Any value other than FALSE (0) should be interpret as TRUE.
This choice was made for performance reasons both when returning the value and also when testing the returned value.
How to test that a boolean 'b' is true:
if(b) // Ok! if(b != FALSE) // Ok! if(b == TRUE) // Not Ok! (no guaranteed to work)
To test that it is false:
if(!b) // Ok! if(b == FALSE) // Ok! if(b != TRUE) // Not Ok! (no guaranteed to work)
In short, never use TRUE to check a boolean value.