Boolean VS Number Variable

Post » Mon Jan 15, 2018 4:01 pm

Is there any difference in performance if I use a number variable with value 0 and 1 versus a boolean? What are some advantages of using boolean instead of number variable? Thanks.
B
19
S
6
G
2
Posts: 76
Reputation: 2,389

Post » Mon Jan 15, 2018 7:06 pm

The boolean variable only needs to store one byte of information. So a boolean variable is slightly better for storage and performance. But only very, VERY slightly. So unless you have literally a million variables, it won't make a difference.

I would recommend using a boolean variable, just because it lets you do things, like toggle the boolean. Which is surprisingly difficult to achieve with a number variable.
B
25
S
4
Posts: 74
Reputation: 1,346

Post » Tue Jan 16, 2018 10:40 am

You can toggle a number variable which is either 0 or 1 using,

abs(variable -1) or 1-variable
B
15
S
4
G
1
Posts: 86
Reputation: 1,178

Post » Tue Jan 16, 2018 4:43 pm

Wai wrote:What are some advantages of using boolean instead of number variable?


For better code readability and debugging. Doesn't affect machine execution if you are comparing it against 1 and 0. But :
Code: Select all
playerJumping == true

is more human readable than say :
Code: Select all
 playerJumping == 1
B
101
S
35
G
12
Posts: 296
Reputation: 13,686


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 4 guests