AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Getting one bit from a bitsum (https://forums.alliedmods.net/showthread.php?t=88597)

Emp` 03-27-2009 00:16

Getting one bit from a bitsum
 
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)

Bugsy 03-27-2009 00:26

Re: Getting one bit from a bitsum
 
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



Emp` 03-27-2009 00:34

Re: Getting one bit from a bitsum
 
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?

Bugsy 03-27-2009 00:39

Re: Getting one bit from a bitsum
 
Quote:

Originally Posted by Emp` (Post 789961)
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 ) ); 


Emp` 03-27-2009 00:49

Re: Getting one bit from a bitsum
 
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.

jim_yang 03-27-2009 00:57

Re: Getting one bit from a bitsum
 
new randbit = 1<<random(4)
Is this what you want ?

Bugsy 03-27-2009 00:58

Re: Getting one bit from a bitsum
 
Quote:

Originally Posted by Emp` (Post 789966)
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_* ) ) 


Dores 03-27-2009 06:04

Re: Getting one bit from a bitsum
 
@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.

ConnorMcLeod 03-27-2009 07:44

Re: Getting one bit from a bitsum
 
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.

Arkshine 03-27-2009 07:52

Re: Getting one bit from a bitsum
 
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) )


All times are GMT -4. The time now is 08:56.

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