Raised This Month: $12 Target: $400
 3% 

Bits stuff


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Location: Siiiiiiiiuu
Old 09-13-2015 , 16:15   Bits stuff
Reply With Quote #1

Hey, i need to store some numbers and i think bits is the way
PHP Code:
new g_iStore[33]

g_iStore[id] = ??? 
Then also how to check if it has some specific number.

So basickly, i am doing sort of shop plugin. The id of the item is generated when it's created.
I must store the id of that item into g_iStore[id] when the item is bought and check later if that item was already bought or not.
__________________
Jhob94 is offline
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 09-13-2015 , 16:24   Re: Bits stuff
Reply With Quote #2

PHP Code:
enum
{
  
ItemA << 0// 1
  
ItemB << 1// 2
  
ItemC << 2// 4
  
ItemD << 3// 8
  
ItemE << 4// 16

  
ItemZ << 31 // Last Entry (a value which is at most 1 << 31 - never exceed this)
}

new 
g_Store[MAX_PLAYERS 1]

// Add
g_Store[This] |= Item_B

// Remove
g_Store[This] &= ~Item_B

// Check
g_Store[This] & Item_B 

Last edited by claudiuhks; 09-13-2015 at 16:25.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
Old 09-13-2015, 16:35
Jhob94
This message has been deleted by Jhob94. Reason: Thanks
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-13-2015 , 17:57   Re: Bits stuff
Reply With Quote #3

You can also define the enum like this. You can add items as need, but as claudiuhks said, do not exceed 32 items.
PHP Code:
enum ( <<= )
{
    
ItemA 1,
    
ItemB,
    
ItemC,
    
ItemD,
    
ItemE

Use the below to clear all bits:
PHP Code:
g_Store[This] = 
Does this solve your problem/question?
__________________

Last edited by Bugsy; 09-13-2015 at 19:05.
Bugsy is offline
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Location: Siiiiiiiiuu
Old 09-17-2015 , 16:48   Re: Bits stuff
Reply With Quote #4

Now i understand why 32 is the limit. Tried to "cheat" it using last * 2 and only could make 32 entries, after that, i get this value -2147483648

Anyway, still nice, i guess there is no other way but no problem at all. No one should reach 32 anyway..
__________________
Jhob94 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 09-17-2015 , 17:53   Re: Bits stuff
Reply With Quote #5

There are a few useful macros in Bugsy's tutorial that let you "cheat" around the 32 bits limitation.
Although, that method ("bit array") is probably a few instructions slower than classical array method. If you ever need more than 32 "entries", just use a regular array, memory usage is usually not a concern in such small plugins.
klippy 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 13:39.


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