Raised This Month: $32 Target: $400
 8% 

Solved Whats the Meaning of "!!" in SP?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DJPlaya
Senior Member
Join Date: Nov 2014
Location: Germany
Old 11-26-2019 , 15:23   Whats the Meaning of "!!" in SP?
Reply With Quote #1

I recently stumbled upon a strange Expression in a Plugin:
Code:
return !!(GetClientListeningFlags(iClient) & VOICE_MUTED);
For me, the Meaning would be clear if the Exclamation Marks would be missing or just "!(Get...".
I tried to remove it but then the Plugin spits out a tag mismatch.

I really cant think of its Usage since it cannot have something todo with true/false Statements, can it?
I mean, "!" reverts the Statement, "!!" wont revert it twice since that makes no Sense.
__________________
My biggest Projects: Kigen AC Redux, Forlix Floodcheck Redux

Last edited by DJPlaya; 11-27-2019 at 11:24.
DJPlaya is offline
Send a message via Skype™ to DJPlaya
cristian20042
AlliedModders Donor
Join Date: Jun 2016
Location: Romania
Old 11-26-2019 , 15:37   Re: Whats the Meaning of "!!" in SP?
Reply With Quote #2

Damn, I'm now interested to know too
__________________
Steam - https://steamcommunity.com/id/sniperheroofficialu/
Discord - SniperHero#8545
cristian20042 is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 11-26-2019 , 15:46   Re: Whats the Meaning of "!!" in SP?
Reply With Quote #3

PHP Code:
(GetClientListeningFlags(iClient) & VOICE_MUTED
Above is a bitfield, the resulting type after evaluation is int. When doing bit-wise AND, the resulting expression will either be equal to the VOICE_MUTED flag or it will be zero.

The "proper" way of evaluating the expression as a boolean one would be the following

PHP Code:
(GetClientListeningFlags(iClient) & VOICE_MUTED) == VOICE_MUTED 

However, true-ness of an expression is only determined based off of whether or not the expression is zero. If the expression is equal to zero, then it is considered false, if it is non-zero, it is considered true.

When you do the following
PHP Code:
return !(GetClientListeningFlags(iClient) & VOICE_MUTED); 
All you're doing is converting the expression to a bool, but you're negating the value. If you use the logical not operator (!) again, you negate it back.

All it is is a quick and dirty way to explicitly convert an integer expression to a boolean one.

If we do some pseudocode here
PHP Code:
int a 54;
bool b = !a// b stores false since a was "true"
= !b// negate again, now it stores true again, but we have a boolean. This matches the original "truth-ness" of a
// the equivalent to what is above is this
= !!a;


// lets try with a false expression
int x 0;
bool y = !x// y contains true, since x is "false"
= !y// negate it again, y is false. This matches the original "truth-ness" of x
// the equivalent to what is above is this
= !!x
That's basically it, I hope what I explained was digestible. Feel free to jump in the discord, too, and some other people might be better at explaining it.

Last edited by headline; 11-26-2019 at 15:56.
headline is offline
cristian20042
AlliedModders Donor
Join Date: Jun 2016
Location: Romania
Old 11-26-2019 , 15:54   Re: Whats the Meaning of "!!" in SP?
Reply With Quote #4

That is nice explaining headline.
__________________
Steam - https://steamcommunity.com/id/sniperheroofficialu/
Discord - SniperHero#8545
cristian20042 is offline
DJPlaya
Senior Member
Join Date: Nov 2014
Location: Germany
Old 11-27-2019 , 11:22   Re: Whats the Meaning of "!!" in SP?
Reply With Quote #5

Quote:
Originally Posted by headline View Post
...
__________________
My biggest Projects: Kigen AC Redux, Forlix Floodcheck Redux

Last edited by DJPlaya; 11-27-2019 at 11:22.
DJPlaya is offline
Send a message via Skype™ to DJPlaya
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 09:14.


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