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

[SOLVED]How to compare bitstrings(like (1<<1))


Post New Thread Reply   
 
Thread Tools Display Modes
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-26-2014 , 12:38   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #11

ok, I'm understanding bitstrings that I think I know why a plugin I coded isn't working with bitstrings right.
So tell me if this is wrong: (5<<1)
and is this the right way: (1<<5)

Last edited by WildCard65; 03-26-2014 at 12:45.
WildCard65 is offline
Doodil
Senior Member
Join Date: Mar 2012
Old 03-26-2014 , 14:03   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #12

5 = 101
(5<<1) = 1010 = 10

1 = 001
(1<<5) = 100000 = 32

it depends on your plugin and what you were trying to do, but you usually want to shift 1 bit 5 places to the left, so yes, (1<<5) would've been the right choice for that
Doodil is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-26-2014 , 14:08   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #13

Well I made this logger plugin for myself(not going to release it because well I'm still quite newbish at sourcepawn + it would also be redundant(maybe)) that uses bitstrings and this was original way I was doing it:
PHP Code:
enum LogFlags
{
    
LogFlags_Unknown 0,
    
LogFlags_Silent = (1<<1),
    
LogFlags_File = (2<<1),
    
LogFlags_Console = (3<<1),
    
LogFlags_KeyValue = (4<<1),
    
LogFlags_Debug = (5<<1)

But it was causing major issues so I was wondering if I was using wrong way of them cause I couldn't find any documentation on them and had to go on inc files that had them(Cause I couldn't find any plugins who's source I had access to(I think) that utilized them.
WildCard65 is offline
h3bus
AlliedModders Donor
Join Date: Nov 2013
Old 03-26-2014 , 14:19   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #14

Yep wrong way.
Bit string flag needs to be one hot encoded to be correctly segregated.

Last edited by h3bus; 03-26-2014 at 17:22.
h3bus is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-26-2014 , 15:41   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #15

Quote:
Originally Posted by Doodil View Post
correct me if I'm wrong, but in other programing languages (1|2|3) does not mean "set bit 1, 2 and 3" but "set the bits that are used to represent the numbers 1, 2 and 3". And that means:

1= 001
2= 010
3= 011

and going with this logic (1|2|3) = 011 and (1|2) = 011, so (1|2|3) == (1|2) equals true.


If you want to set bit number X (starting at 0) then you have to left-shift the first bit by X:

(1<<0) = 001 //equals 1
(1<<1) = 010 //equals 2
(1<<2) = 100 //equals 4, not 3



Please correct me if this is any different in sourcemod.
You're right, I should have used left shifts there. Whoops.

Incidentally, using bits directly is a bad idea anyway, use an enum set up like this:

PHP Code:
enum LogFlags
{
    
LogFlags_Unknown 0// 0
    
LogFlags_Silent = (1<<0), // 1
    
LogFlags_File = (1<<1), // 2
    
LogFlags_Console = (1<<2), // 4
    
LogFlags_KeyValue = (1<<3), // 8
    
LogFlags_Debug = (1<<4// 16

Be aware that 0 should only be used for the generic "nothing else is set" as you can't combine it with other values.

(Incidentally, menus.inc has examples of enums)
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 03-26-2014 at 15:56.
Powerlord is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-26-2014 , 15:54   Re: [SOLVED]How to compare bitstrings(like (1<<1))
Reply With Quote #16

k, thx every.
WildCard65 is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 03-27-2014 , 08:08   Re: [SOLVED]How to compare bitstrings(like (1<<1))
Reply With Quote #17

If you don't have a proper calculator, you could just use like ... windows calc, set it to programmer to get integer division, and bitwise operations.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-27-2014 , 11:56   Re: [SOLVED]How to compare bitstrings(like (1<<1))
Reply With Quote #18

I know how bits work but didn't even knew that there was a left shift and right shift part of them.
WildCard65 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 01:21.


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