CodeDesignerLab Posted March 8, 2016 Share Posted March 8, 2016 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 More sharing options...
Darkchie Posted May 7, 2016 Share Posted May 7, 2016 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 More sharing options...
Rolig Loon Posted May 7, 2016 Share Posted May 7, 2016 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 More sharing options...
CodeDesignerLab Posted May 12, 2016 Author Share Posted May 12, 2016 Correct, I've never seen the page that the other poster mentioned. The example I posted was literally taken from and scaled down from a project I coded from scratch. 1 Link to comment Share on other sites More sharing options...
wherorangi Posted May 15, 2016 Share Posted May 15, 2016 sometimes we can end up reinventing stuff that we never knew others know already at the time. This is a good thing and I am very encouraging of people who walk the path of rediscovery and reinvention Link to comment Share on other sites More sharing options...
Malinda Quartz Posted September 18, 2016 Share Posted September 18, 2016 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 More sharing options...
Recommended Posts
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