Raised This Month: $ Target: $400
 0% 

flags/math help [2, 4, 8, 16, 32, etc]


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sgtbane
Member
Join Date: Feb 2006
Old 12-31-2007 , 01:54   flags/math help [2, 4, 8, 16, 32, etc]
Reply With Quote #1

ok, this has been bothering me for a while so im just gonna ask on here. The reason I want this is not exactly directly amxx related, but related to many forms of code.
In AMXX there is the player flags. a-u, and I am assuming they use the values 1,2,4,8,16,32,64,128,256,512,1024,2048, etc...
(If this is not the case then please help me anyways because as I said i have been wondering for a while.)

when you have something like this you can have a variety of different options selected and store it as a single value because no combination of them will ever equal another.

so how do you read it?
if i had the number 69, how do i know that its 64, 4, and 1? There has to be a mathematical formula to figure it out but i can't get it.
again I know that this is not directly connected with AMXX although I am sure it is used in some way or form, and I know when writing I have often wondered how it is done.

If anyone can help thankyou very much. If this is removed since it does not apply to this "Support / Help - Find help for general AMXX issues. This does not include support for individual 3rd party plugins." then thanks anyways..
__________________
[Add|Community]

~SgtBane

Last edited by sgtbane; 01-02-2008 at 17:14.
sgtbane is offline
Send a message via MSN to sgtbane
Old 12-31-2007, 03:26
FoxboyJT
This message has been deleted by FoxboyJT. Reason: I'm a tard.
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 12-31-2007 , 20:46   Re: help?
Reply With Quote #2

The more serious issue, that this thread would be deleted regarding, is your choice of "help?" as a thread topic. Please fix that before responding again.

Generally you wouldn't be asking which numbers is 69 comprised of. Instead you'd be asking, is x in y? You'd do that in either an if/endif or switch/case statement.
__________________
Brad is offline
sgtbane
Member
Join Date: Feb 2006
Old 01-02-2008 , 17:20   Re: flags/math help [2, 4, 8, 16, 32, etc]
Reply With Quote #3

thanks. i changed the title
also i discovered a method to get it done, but i was wondering if there is a simpler way, since with this you have to count down backwards through them till you find the one you want. Just wondering if there is a more direct way.

anyways this is what I am doing:
Code:
    Dim tmpcntInt As Integer, tmpInt As Integer, max As Long, i As Integer
    tmpInt = ReadInteger(ptrPlayer + 3088, pid)
    tmpcntInt = tmpInt
    max = 268435456
    For i = 0 To 28
        If tmpcntInt >= max And tmpcntInt <= (max * 2 - 1) Then
            tmpcntInt = tmpcntInt - max
            StorMove(28 - i) = True
        Else
            StorMove(28 - i) = False
        End If
        max = (max / 2)
    Next i
thats in VB 6.0 o.0... i know its old, please spare me the nagging
anyways I am getting the value from memory via pointer+offset, then making a copy of it to make it easyer to make changes to the value later.

as you can see I had to hard code the max value of what I wanted to find (in this case I went up to 28 flags, and at 28 it happens to equal what the hard coded max value is.
the boolean: StorMove(i) is where I store the flag values.

if there is a simpler method please let me know. or if this is how its supposed to be done then please also let me know.
__________________
[Add|Community]

~SgtBane
sgtbane is offline
Send a message via MSN to sgtbane
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 01-02-2008 , 18:34   Re: flags/math help [2, 4, 8, 16, 32, etc]
Reply With Quote #4

Have you ever heard about binary numeral system?

To convert your decimal number to binary number use this:
PHP Code:
//pseudocode
number your_value
0
while (number 0){
    
flags[i] = number mod 2
    number 
number div 2
    i
++
}
//now you must invert flags table 
To convert binary number to decimal number use this:
PHP Code:
//pseudocode
number 0
for (new ii<=maxi++)
    
number number flags[i
__________________
Impossible is Nothing

Last edited by Sylwester; 01-02-2008 at 18:37.
Sylwester is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 01-02-2008 , 22:16   Re: flags/math help [2, 4, 8, 16, 32, etc]
Reply With Quote #5

Rather than..

Code:
If StarMove(n) Then doStuff
..which requires your above snippet, simply use..

Code:
Const ONE As Integer = 1
Const TWO As Integer = 2
Const FOUR As Integer = 4
Const EIGHT As Integer = 8
Const SIXTEEN As Integer = 16
Const THIRTY_TWO As Integer = 32
Const SIXTY_FOUR As Integer = 64

Dim bitPattern as Integer = 69

If bitPattern And SIXTY_FOUR Then
    Rem bitPattern must 'contain' 64
End If
Why Microsoft considered bit shifting operators unnecessary syntactic sugar until VB.NET 2003 (i.e. they still did in VB.NET 2002) is a mystery to me.

Last edited by Lee; 01-02-2008 at 22:25.
Lee is offline
Roach
Writes love letters to sawce Daily
Join Date: Jul 2006
Location: Internet
Old 01-03-2008 , 22:29   Re: flags/math help [2, 4, 8, 16, 32, etc]
Reply With Quote #6

Just bumping this down to scripting help...since its kinda odd being in General AMXx issues.
__________________
Quote:
Originally Posted by Brad View Post
That sounds like a really good idea!
Now replace the word "good" with "dumb".
What was your rationale for proposing such a thing?
Roach is offline
alien
Senior Member
Join Date: Aug 2005
Location: London || Slovakia
Old 01-04-2008 , 06:00   Re: flags/math help [2, 4, 8, 16, 32, etc]
Reply With Quote #7

I'll just copy & paste what I'm using in my plugin.
First you'll define flags you want to store:

Code:
#define FLAG_LOGGINGIN		1 << 0
#define FLAG_USER		1 << 1
#define FLAG_CLIMBING		1 << 2
#define FLAG_SCOUT		1 << 3
#define FLAG_STORECP		1 << 4
#define FLAG_BHOPSCRIPT		1 << 5
#define FLAG_LONGJUMP		1 << 6
#define FLAG_CHECKPOINT		1 << 7
#define FLAG_CHEATWALLS		1 << 8
#define FLAG_CHEATDEATH		1 << 9
#define FLAG_CHEATGRAVITY	1 << 10
#define FLAG_CHEATSPEED		1 << 11
#define FLAG_NORESPAWN		1 << 12
#define FLAG_RESPAWNING		1 << 13
#define FLAG_LJSTATS		1 << 14
#define FLAG_LJMARKS		1 << 15
#define FLAG_VOTEDMAP		1 << 16
#define FLAG_SLOT		1 << 17
#define FLAG_RTV		1 << 18
#define FLAG_CHEATHOOK		1 << 19
Then you have two functions:

Code:
stock bool:get_flag(flagset, flag)   {     return bool:(flagset & flag);   } stock set_flag(&flagset, flag, bool:status)   {     if (status)       flagset |= flag;     else       flagset &= ~flag;     return;   }

You can use it like this:

Code:
set_flag(some_integer, FLAG_BHOPSCRIPT, true); if (get_flag(some_integer, FLAG_BHOPSCRIPT))   {     // This will be executed, because FLAG_BHOPSCRIPT is true.   }

Hope it helps. It's also very fast this way.
__________________

Last edited by alien; 01-04-2008 at 06:04.
alien is offline
Send a message via ICQ to alien
purple_pixie
Veteran Member
Join Date: Jun 2007
Location: Winchester, England
Old 01-07-2008 , 10:35   Re: flags/math help [2, 4, 8, 16, 32, etc]
Reply With Quote #8

Just one teeny point ...

This is a little much effort:
Code:
#define FLAG_LOGGINGIN		1 << 0
#define FLAG_USER		1 << 1
#define FLAG_CLIMBING		1 << 2
#define FLAG_SCOUT		1 << 3
...
When you could just do:
Code:
enum <<=1
{
  FLAG_LOGGINGIN=1,
  FLAG_USER,
  FLAG_CLIMBING,
  FLAG_SCOUT,
...
}
purple_pixie is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 01-07-2008 , 12:43   Re: flags/math help [2, 4, 8, 16, 32, etc]
Reply With Quote #9

You can't use either in VB6.

Last edited by Lee; 01-07-2008 at 12:46.
Lee is offline
purple_pixie
Veteran Member
Join Date: Jun 2007
Location: Winchester, England
Old 01-08-2008 , 05:49   Re: flags/math help [2, 4, 8, 16, 32, etc]
Reply With Quote #10

Ah. I forgot that this post in Scripting Help was actually aboot VB ...

I would have posted something relevant, but if I see any more VB I shall either cry or shoot something.

It's such an ugly language. Euch.
purple_pixie is offline
Reply


Thread Tools
Display Modes

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 11:03.


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