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

[TUT] Bits, Bit-fields, and Bit-wise Operators


Post New Thread Reply   
 
Thread Tools Display Modes
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 11-26-2010 , 15:59   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #31

Hey, is it possible to use bits in double arrays to eliminate the player array?

Like instead if having player[33][32], making it just player[32]

I can't think of a way, and I'm not sure it is possible because bits are technically a single number, so you'd need multiple cells in order to store separate values. I just wanted to check though and see if maybe someone was creative enough to figure out a way.
__________________

PM me if you're interested in buying the Credits addition for Base Builder
Battlefield Rebirth [66% done]
Call of Duty: MW2 [100% done]
Base Builder [100% done]
Tirant is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-26-2010 , 16:04   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #32

Quote:
Originally Posted by Tirant View Post
Hey, is it possible to use bits in double arrays to eliminate the player array?

Like instead if having player[33][32], making it just player[32]

I can't think of a way, and I'm not sure it is possible because bits are technically a single number, so you'd need multiple cells in order to store separate values. I just wanted to check though and see if maybe someone was creative enough to figure out a way.
Nope. At least not a direct method (that I can think of).
__________________
fysiks is offline
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 11-26-2010 , 16:18   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #33

Man that sucks. Would've been awesome to get rid of a array that consumes like 32^2 or 32^3 bits of memory.
__________________

PM me if you're interested in buying the Credits addition for Base Builder
Battlefield Rebirth [66% done]
Call of Duty: MW2 [100% done]
Base Builder [100% done]
Tirant is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-26-2010 , 18:10   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #34

Show an example of the player[33][32] array.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 11-26-2010 , 19:00   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #35

Well, that was just a random example. I was just thinking of trying to assign them integer/float values. In one part of my code for example, it caches their player models using [id][string], I was just seeing if there was a way to make it [string] only, but that can't work because you need to store separate non-corresponding values for everyone.
__________________

PM me if you're interested in buying the Credits addition for Base Builder
Battlefield Rebirth [66% done]
Call of Duty: MW2 [100% done]
Base Builder [100% done]
Tirant is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 11-26-2010 , 19:54   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #36

You can do some hackery and store two shorts in one cell, but the loss of readability wouldn't be worth the small optimization.
Seta00 is offline
Old 11-26-2010, 21:57
Bugsy
This message has been deleted by Bugsy. Reason: nvm
itimeheke
Junior Member
Join Date: Dec 2010
Old 01-04-2011 , 10:19   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #37

Hi! Great tut!

But i'm wondering is there any way to check how many bits are set in bitfield? I mean better way than checking every bit individually and counting results.
itimeheke is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-05-2011 , 23:28   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #38

Quote:
Originally Posted by itimeheke View Post
Hi! Great tut!

But i'm wondering is there any way to check how many bits are set in bitfield? I mean better way than checking every bit individually and counting results.
PHP Code:
BitCountiVal )
{
    
iVal iVal - ((iVal >> 1) & 0x55555555);                    
    
iVal = (iVal 0x33333333) + ((iVal >> 2) & 0x33333333);     
    return ((
iVal + (iVal >> 4) & 0xF0F0F0F) * 0x1010101) >> 24

This page has a lot of interesting bit hacks.
__________________

Last edited by Bugsy; 01-05-2011 at 23:38.
Bugsy is offline
RemaKe`
New Member
Join Date: Jun 2009
Old 01-07-2011 , 06:06   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #39

Great tutorial!

Just a question:
PHP Code:
new g_IsAdmin//Only need a single cell of memory

public client_putinserverid )
{
    if ( 
is_user_adminid ) ) 
            
g_IsAdmin |= ( << ( id 31 ) );


Why is "id & 31" required if the result is "id" anyways?
RemaKe` is offline
itimeheke
Junior Member
Join Date: Dec 2010
Old 01-07-2011 , 07:21   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #40

That's because bit shifting starts form zero (1 << 0) but player id's from one. This (id & 31) puts this first bit in use by assigning player 32 data to first bit, cause (32 & 31) = 0. Otherwise, player 32's bit bill not be set, cause you're out of memory range(amx variables are limited to 32bit), and by using (1 << 32) you're actually trying to set bit 33, witch will not work. Hope you got it;)
itimeheke 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 07:14.


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