Raised This Month: $ Target: $400
 0% 

true - false


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
KamiN
Senior Member
Join Date: Jun 2011
Old 06-16-2012 , 14:21   true - false
Reply With Quote #1

Code:
new test = 1
new test = true
Code:
new test2 = 0
new test2 = false
1 and true means the same thing like 0 and false, right?

I mean, you can wirte 1 instead of true and 0 instead of false, yes?

Last edited by KamiN; 06-16-2012 at 14:21.
KamiN is offline
<VeCo>
Veteran Member
Join Date: Jul 2009
Location: Bulgaria
Old 06-16-2012 , 14:28   Re: true - false
Reply With Quote #2

Basically every number except 0, casted to boolean, returns 1 (including negative numbers).
In your code, it will be the same. But usually boolean variables have "bool:" tag to show that they are booleans, not integers, but that isn't necessary.
__________________
<VeCo> is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-16-2012 , 14:41   Re: true - false
Reply With Quote #3

Bools and integers are the same. While bools are for better readability, integers can have more options than two, so the usage is situational. If you are sure you will only have the variable to describe a state ( true, false ), you can use bools. If you are however able to put more than two states in one variable, use integers. What I mean is:
PHP Code:
new boolg_LiveRound
new boolg_KnifeRound
new boolg_WarmupRound 
would be worse than
PHP Code:
// let's say 1 is live, 2 is warmup and 3 is knife
new g_Round 
since you can save some memory. If you use enumerations, your code will look even better:

PHP Code:

enum Rounds
{
   
Knife,
   
Warmup,
   
Live
}

new 
Roundsg_Round 

Last edited by Backstabnoob; 06-16-2012 at 14:42.
Backstabnoob is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-16-2012 , 15:38   Re: true - false
Reply With Quote #4

Quote:
Originally Posted by <VeCo> View Post
Basically every number except 0, casted to boolean, returns 1 (including negative numbers).
Not true. Casting an integer to boolean does not change its value, just the tag.
So, for example:
Code:
(bool:2 == bool:1) // false (bool:1 == true) // true (bool:0 == false) // true (bool:-1 == true) // false (!!(-1) == true) // true (!!(1) == true) // true (!!(2) == true) // true (!!(0) == false) // true

When you use !, it opposites the bits in the value, so any non-zero number becomes 0 and 0's become 1.

So:
Code:
!(-1) = 0 !(0) = 1 !(1) = 0

When you use it twice, it just does the operation twice.
The result is forcing a value to be a boolean constant.

Code:
!!(2) = !(0) = 1 = true !!(0) = !(1) = 0 = false
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 06-17-2012 at 02:55.
Exolent[jNr] is offline
<VeCo>
Veteran Member
Join Date: Jul 2009
Location: Bulgaria
Old 06-16-2012 , 15:48   Re: true - false
Reply With Quote #5

Ah, I think I got it now, thanks. I've always wondered what exactly is the purpose of "!!".
__________________

Last edited by <VeCo>; 06-16-2012 at 15:48.
<VeCo> is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-16-2012 , 17:33   Re: true - false
Reply With Quote #6

Quote:
Originally Posted by Exolent[jNr] View Post
Code:
(!!(0) == true) // true

Code:
!!(0) = !(1) = 0 = false
You should probably fix the latter former.
__________________

Last edited by fysiks; 06-16-2012 at 23:13.
fysiks is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-16-2012 , 18:37   Re: true - false
Reply With Quote #7

Quote:
Originally Posted by fysiks View Post
You should probably fix the latter.
No, it meant that !!(0) = false, not that the statement wasn't correct, lol.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 06-16-2012 , 19:33   Re: true - false
Reply With Quote #8

Quote:
Originally Posted by Exolent[jNr] View Post
No, it meant that !!(0) = false, not that the statement wasn't correct, lol.
If you are saying that !!(0) means false, then how is this correct? (what fysiks was trying to point out)
Code:
(!!(0) == true) // true
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-16-2012 , 23:12   Re: true - false
Reply With Quote #9

Quote:
Originally Posted by Exolent[jNr] View Post
No, it meant that !!(0) = false, not that the statement wasn't correct, lol.
I meant to say "former" instead of "latter".
__________________

Last edited by fysiks; 06-16-2012 at 23:13.
fysiks is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-17-2012 , 02:55   Re: true - false
Reply With Quote #10

Quote:
Originally Posted by YamiKaitou View Post
If you are saying that !!(0) means false, then how is this correct? (what fysiks was trying to point out)
Code:
(!!(0) == true) // true
Quote:
Originally Posted by fysiks View Post
I meant to say "former" instead of "latter".
Sorry, its been fixed.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:07.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode