Jump to content

Implement a Bitmask in LSL


CodeDesignerLab
 Share

You are about to reply to a thread that has been inactive for 2325 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Because I can't figure out how to make a user space on the wiki, I'll post it here. (if anyone would explain that to me, that's be great) Anyway, this is the result of learning how to implement a bitmask in LSL. Here is it. A basic bitmask example to use for your custom option flags. which is usually taught fairly early in school but most self-taught programmers wait forever to learn it because it's intimidating 

 

integer rmask;
integer REPEATED = 1;
integer MONTHLY = 2;
integer WEEKLY = 4;
integer YEARLY = 8;
default
{
    state_entry()
    {
        rmask = rmask | REPEATED; // Set Repeated, Comment this whole line to see it not repeated.
        rmask = rmask | WEEKLY; // Set the interval
        string text = "The event is ";
        if (rmask & REPEATED)
        {
            text+="repeated ";
            if(rmask & MONTHLY)
            {
                text+="monthly.";
            }
            if(rmask & WEEKLY)
            {
                text+="weekly.";
            }
            if(rmask & YEARLY)
            {
                text+="yearly.";
            }
        }
        else text+="not repeated";
       
        llOwnerSay(text);
    }
}

 This will output "The event is repeated weekly."

Link to comment
Share on other sites

  • 1 month later...

I have seen this before, your example is an adaption / duplication in LSL from the code on this page. Everything that you would need to know about this particular bit of coding is explained on the page you got it from.

 

http://code.tutsplus.com/articles/understanding-bitwise-operators--active-11301

 

Original code is here :

 

public class PopupWindow extends Sprite{    public static const YES:int = 1;    public static const NO:int = 2;    public static const OKAY:int = 4;    public static const CANCEL:int = 8;     public static void showPopup(buttons:int)    {        if(buttons & YES)        {            // add YES button        }                 if(buttons & NO)        {            // add NO button        }       }}
Link to comment
Share on other sites

That bit of code is a pretty dirt-level basic example of how to use bit-wise logic to recognize a response.  Anyone could have written it.  The OP's example isn't much more advanced, but it actually demostrates how to use both & and (logical AND and OR ) to create a mask and then use it.  It doesn't look remotely like a ripoff to me.

Link to comment
Share on other sites

  • 4 months later...

By declaring your bits in global like that, you're adding an additional 4 bytes each per bit, which defeats the purpose of saving space by manipulating bits all together. I wrote about it here.

Doing it this way,  you knock off 16 bytes:

 

integer rmask;//integer REPEATED = 1;//integer MONTHLY = 2;//integer WEEKLY = 4;//integer YEARLY = 8;default{    state_entry()    {        rmask = rmask | 0x01; // Set Repeated, Comment this whole line to see it not repeated.        rmask = rmask | 0x04; // Set the interval        string text = "The event is ";        if (rmask & 0x01)        {            text+="repeated ";            if(rmask & 0x02) // MONTHLY = 0x02            {                text+="monthly.";            }            if(rmask & 0x04) // WEEKLY = 0x04            {                text+="weekly.";            }            if(rmask & 0x08) // YEARLY = 0x08            {                text+="yearly.";            }        }        else text+="not repeated";               llOwnerSay(text);    }}

 

I know that's marginal but 16 bytes of free space saved is better than 16 bytes of free space missed.

Now of course if you have a long list of bits (like 20 - 31) that need titles for the sake of simplicity...rather than declaring them as global integers each (which would fill up fast) it's better practice to just macro them for the titles:

Instead of integer YEARLY = 8; Macro define it like this #define YEARLY 0x08 no semi colon needed either. Your bits stay literal. Edit: Should've noted: A viewer that supports pre-processing (and lazy lists enabled) is needed otherwise #define is surly a no-go.

#define REPEATED    0x01#define MONTHLY     0x02#define WEEKLY      0x04#define YEARLY      0x08integer rmask;default{    state_entry()    {        rmask = rmask | REPEATED; // Set Repeated, Comment this whole line to see it not repeated.        rmask = rmask | WEEKLY; // Set the interval        string text = "The event is ";        if (rmask & REPEATED)        {            text+="repeated ";            if(rmask & MONTHLY)            {                text+="monthly.";            }            if(rmask & WEEKLY)            {                text+="weekly.";            }            if(rmask & YEARLY)            {                text+="yearly.";            }        }        else text+="not repeated";               llOwnerSay(text);    }}

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2325 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...