Raised This Month: $32 Target: $400
 8% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-20-2014 , 10:57   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #71

claudiuhks: That post is over three years old.
__________________

Last edited by Bugsy; 05-20-2014 at 10:59.
Bugsy is offline
vitorrossi
Senior Member
Join Date: Apr 2012
Location: NY, USA
Old 05-13-2015 , 19:16   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #72

Stock that returns the number of SetBits in a bitfield, aka hamming weight or popcount:

PHP Code:
     NumberOfSetBits(iBitField)
    {
        
iBitField iBitField - ((iBitField >> 1) & 0x55555555);
        
iBitField = (iBitField 0x33333333) + ((iBitField >> 2) & 0x33333333);
        return (((
iBitField + (iBitField >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
    } 
Taken from here
vitorrossi is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 08-30-2016 , 02:00   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #73

If I have a variable equal to (1 << value) is it possible to get the value part of it?
PartialCloning is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-30-2016 , 06:03   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #74

I can't think of any efficient way, but what exactly are you trying to achieve? Having to find that value doesn't seem like a good thing to me.

EDIT:
This could work, but I don't like that it's a loop:
PHP Code:
getValue(variable) {
    for(new 
032i++) {
        if(
variable == (<< i)) {
            return 
i;
        }
    }
    
    
// Some invalid state I guess, variable has more than 1 bit set.
    
return -1;


Last edited by klippy; 08-30-2016 at 06:07.
klippy is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 08-30-2016 , 19:52   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #75

Alternative to KliPPy's:
PHP Code:
stock BitShiftCount)
{
    new 
i;
    while ( 
&& !( ) )
    {
        
>>= 1;
        
i++;
    }
    return 
i;


Last edited by Emp`; 08-31-2016 at 18:07. Reason: forgot this isn't the SourceMod section
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`
Old 08-30-2016, 22:31
Bugsy
This message has been deleted by Bugsy. Reason: nvm, misread
Bos93
Veteran Member
Join Date: Jul 2010
Old 08-31-2016 , 17:53   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #76

int i
__________________
Bos93 is offline
Send a message via ICQ to Bos93 Send a message via Skype™ to Bos93
Depresie
Veteran Member
Join Date: Nov 2013
Old 10-02-2016 , 21:35   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #77

Now this, this is, this is a fucking bad ass tutorial, these little fuckers can be extremely useful
I tried to learn about bits some time ago, but i couldn't understand even the basic things from what i found on google, but this man is golden when it comes to tutorials
Thanks Bugs
__________________
Depresie is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-06-2017 , 09:43   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #78

Can you do for example:

#define SetUserBool(%1,%2) ( %1 |= (1 << (%2 - 1)))

Because it will range from 0 to 31.

Last edited by eyal282; 06-06-2017 at 17:08.
eyal282 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-06-2017 , 09:52   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #79

SetUserBool(%1,%2) ( %1 |= (1 << (%2 - 1)))

You used |= instead of left shift and you should shift 1, not %1, but yea that will work as long as only 1 to 32 are inputted. Same logic as using ( %2 & 31 ).
__________________

Last edited by Bugsy; 06-06-2017 at 09:53.
Bugsy is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-06-2017 , 17:07   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #80

Quote:
Originally Posted by Bugsy View Post
SetUserBool(%1,%2) ( %1 |= (1 << (%2 - 1)))

You used |= instead of left shift and you should shift 1, not %1, but yea that will work as long as only 1 to 32 are inputted. Same logic as using ( %2 & 31 ).
Yeah, mistake.
eyal282 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 22:58.


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