Raised This Month: $ Target: $400
 0% 

Getting one bit from a bitsum


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 03-27-2009 , 00:16   Getting one bit from a bitsum
Reply With Quote #1

Code:
new bitsum = (1<<0)|(1<<1)|(1<<2)|(1<<4);
I need to get a random single bit from it. Such as (1<<0) or (1<<2)
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-27-2009 , 00:26   Re: Getting one bit from a bitsum
Reply With Quote #2

PHP Code:
//Param 1 = the bitsum\variable
//Param 2 = the bit to manipulate

#define SetBit(%1,%2)        ( %1 |= ( 1 << (%2) ) )
#define ClearBit(%1,%2)    ( %1 &= ~( 1 << (%2) ) )
#define CheckBit(%1,%2)    ( %1 & ( 1 << (%2) ) )

iRandBit random_num)
if ( 
CheckBitbitsum iRandBit  )  )
{
    
//bit set

__________________

Last edited by Bugsy; 03-27-2009 at 00:34.
Bugsy is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 03-27-2009 , 00:34   Re: Getting one bit from a bitsum
Reply With Quote #3

Ya I was thinking maybe I could do something such as:
Code:
new randbit;
do{
   randbit = ( 1 << random_num(0, 32) )
}
while( !( bitsum & randbit ) )
Would that work?
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-27-2009 , 00:39   Re: Getting one bit from a bitsum
Reply With Quote #4

Quote:
Originally Posted by Emp` View Post
Ya I was thinking maybe I could do something such as:
Code:
new randbit;
do{
   randbit = ( 1 << random_num(0, 32) )
}
while( !( bitsum & randbit ) )
Would that work?
Shifting left 32 bits will cause an error. Valid shifts on a 4 byte variable (32-bits) are 0 through 31. If you are looking to use bits as bool values for player id's you can use:

PHP Code:
#define AddFlag(%1,%2)        ( %1 |= ( 1 << (%2-1) ) )
#define RemoveFlag(%1,%2)    ( %1 &= ~( 1 << (%2-1) ) )
#define CheckFlag(%1,%2)    ( %1 & ( 1 << (%2-1) ) ) 
Your way will work but change the 32 to a 31.

PHP Code:
new randbit;

do
{
    
randbit random_num31 );
}
while( !
CheckBitbitsum randbit ) ); 
__________________

Last edited by Bugsy; 03-27-2009 at 00:45.
Bugsy is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 03-27-2009 , 00:49   Re: Getting one bit from a bitsum
Reply With Quote #5

I'm guessing I have my terminology wrong, I'll show more example code:
Code:
#define P_FLAG_A        (1<<0)        /* flag "a" */
#define P_FLAG_B        (1<<1)        /* flag "b" */
#define P_FLAG_C        (1<<2)        /* flag "c" */
#define P_FLAG_D        (1<<3)        /* flag "d" */
Code:
new bitsum = P_FLAG_A|P_FLAG_B|P_FLAG_D;
If I wanted to get P_FLAG_* would I use my way or your way.

It looks like to me that with your way randbit would be filled with 0,1,2, or 3. But I want (1<<0),(1<<1),(1<<2), or (1<<3).

Correct me if I'm wrong.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
jim_yang
Veteran Member
Join Date: Aug 2006
Old 03-27-2009 , 00:57   Re: Getting one bit from a bitsum
Reply With Quote #6

new randbit = 1<<random(4)
Is this what you want ?
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-27-2009 , 00:58   Re: Getting one bit from a bitsum
Reply With Quote #7

Quote:
Originally Posted by Emp` View Post
I'm guessing I have my terminology wrong, I'll show more example code:
Code:
#define P_FLAG_A        (1<<0)        /* flag "a" */
#define P_FLAG_B        (1<<1)        /* flag "b" */
#define P_FLAG_C        (1<<2)        /* flag "c" */
#define P_FLAG_D        (1<<3)        /* flag "d" */
Code:
new bitsum = P_FLAG_A|P_FLAG_B|P_FLAG_D;
If I wanted to get P_FLAG_* would I use my way or your way.

It looks like to me that with your way randbit would be filled with 0,1,2, or 3. But I want (1<<0),(1<<1),(1<<2), or (1<<3).

Correct me if I'm wrong.
Edit: My macro does the shifting so you do not need to pass a bit-shifted variable. You just pass the variable and the number of bit-shift left to check.

If you want your way, just do:

PHP Code:
if ( bitsum P_FLAG_* ) 
With my macro:

PHP Code:
#define P_FLAG_A        0     /* flag "a" */
#define P_FLAG_B        1     /* flag "b" */
#define P_FLAG_C        2     /* flag "c" */
#define P_FLAG_D        3     /* flag "d" */

if ( CheckBitbitsum P_FLAG_* ) ) 
__________________

Last edited by Bugsy; 03-27-2009 at 01:04.
Bugsy is offline
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 03-27-2009 , 06:04   Re: Getting one bit from a bitsum
Reply With Quote #8

@Bugsy: I think you've misunderstood what Emp' wants. He wants to get a random bit and store it into a variable. He doesn't want to know if a certain bit is in a bitsum. He wants to get a random bit for that bitsum.
Therefore, checking if the random bit exists in the bitsum would be useless, as he can just limit the randomization to the number of bits in the bitsum.

So jim_yang is most correct.
__________________
O o
/Ż________________________
| IMMA FIRIN' MAH LAZOR!!!
\_ŻŻŻ
Dores is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 03-27-2009 , 07:44   Re: Getting one bit from a bitsum
Reply With Quote #9

I think you are wrong, he wants :

There is an existing variable that contains some bits, he want to pick up random bits from this variable.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 03-27-2009 , 07:52   Re: Getting one bit from a bitsum
Reply With Quote #10

Quote:
E=Emp`;789957]
Code:
new bitsum = (1<<0)|(1<<1)|(1<<2)|(1<<4);
I need to get a random single bit from it. Such as (1<<0) or (1<<2)
jim_yang is right. The same using : rand = power( 2, random(4) )
Arkshine is offline
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 08:56.


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