AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   variables (https://forums.alliedmods.net/showthread.php?t=223582)

FiFiX 08-15-2013 07:32

variables
 
Can someone explain to me how does variables like that works?

Definition:
Code:

#define VOTE_IN_PROGRESS    1
#define VOTE_FORCED            2
#define VOTE_IS_RUNOFF        4
#define VOTE_IS_OVER        8
#define VOTE_IS_EARLY        16
#define VOTE_HAS_EXPIRED    32
#define VOTE_OUTCOME        64

Usage:
check: g_voteStatus & VOTE_IS_RUNOFF
add: g_voteStatus |= VOTE_IS_RUNOFF
remove: g_voteStatus |= ~VOTE_IS_RUNOFF

f.e. If I'll add VOTE_OUTCOME to g_voteStatus it will be 64. It means that previous variables are already inside g_voteStatus?
g_voteStatus |= VOTE_IS_OUTCOME
and g_voteStatus & VOTE_IS_OVER will return true?

YamiKaitou 08-15-2013 07:34

Re: variables
 
Research bit values and bit comparisons

FiFiX 08-15-2013 08:49

Re: variables
 
It means if I'll use bitwise OR to add variable:
64 |= 32 will return 96
then while checking 96 & (16 || 8 || 4 || 2 || 1) will return 0, yes?
and 96 & (64 || 32) will not return 0.

If I'll use bitwise AND to add:
64 &= 32 it will return 32.

It also means that I can add one more variable
Code:

#define VOTE_NEW        128
And it won't affect any other, ye?

Black Rose 08-15-2013 16:11

Re: variables
 
You can add the last part without any problem.
64 &= 32 will return 0
If you want to add bits together you have to use bitwise operator |, not ||.

FiFiX 08-15-2013 16:46

Re: variables
 
Quote:

Originally Posted by Black Rose (Post 2014076)
64 &= 32 will return 0.

Why that will return 0?

YamiKaitou 08-15-2013 16:54

Re: variables
 
Quote:

Originally Posted by FiFiX (Post 2014117)
Why that will return 0?

Because
Code:

  1000000
& 0100000
= 0000000


Black Rose 08-15-2013 17:03

Re: variables
 
Because:
1. I tested it, it returned 0.
2. Bitwise AND compares what the two parts have in common.
For example, We have 64(bit 7) and 32(bit 6) If we were to use bitwise AND on these. The result would be as follows:
Code:

        64        32
1        0        0        =0
2        0        0        =0
4        0        0        =0
8        0        0        =0

16        0        0        =0
32        0        1        =0
64        1        0        =0
128        0        0        =0
                        =0

If on the other hand they would have something in common. Like 64(bit 7) and 96(bit 6 and 7) the result would be as follows:
Code:

        64        96
1        0        0        =0
2        0        0        =0
4        0        0        =0
8        0        0        =0

16        0        0        =0
32        0        1        =0
64        1        1        =64
128        0        0        =0
                        =64

Compare this to the bitwise OR which basically adds up all the 1's into one result.
Code:

        64        96
1        0        0        =0
2        0        0        =0
4        0        0        =0
8        0        0        =0

16        0        0        =0
32        0        1        =32
64        1        1        =64
128        0        0        =0
                        =96

This is why you add with |=. Both sides will combine all the activated(or whatever it's called? All the "1") bits into one larger array.
This is also why you check if a variable contains a certain bit with & because it will only return what both sides have in common.

FiFiX 08-15-2013 17:22

Re: variables
 
i see. Thanks for explanation :)


All times are GMT -4. The time now is 15:50.

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