Hokay. After a lot of trial and error, and heavy use of decimal2binary(), I think I finally have an alright grasp of how the operators work!
For anybody else who might stumble accross this thread, I'll leave the practice script I ended up with by this point. Personally I learn the fastest when I have a consistent base that I use as an example, so with slight modifications, this is how I ended up using the examples by all of you.
Some extra formatting for readability, too. (But the comments don't look quite as good without color-coding.)
// CREATE INITIAL STRUCTUREinteger db_record(integer class, integer faction, integer outfit, integer gender){ class = (class & 0x7) <<6; // 0x7 = binary 111xxxxxx (6 = width of GENDER and OUTFIT and FACTION) faction = (faction & 0x7) <<3; // 0x7 = binary 111xxx (3 = width of GENDER and OUTFIT) outfit = (outfit & 0x3) <<1; // 0x3 = binary 11x (1 = width of GENDER) gender = (gender & 0x1); // 0x1 = binary 1 (No shifting because it's the right-most) // // x's replaced by 0 when shifted, then replaced by next value return class | faction | outfit | gender;}// GET VALUEinteger get_flag(integer w, integer x){ // x = Amount of bits to shift in the x "axis." check db_record for x. return ( (db >>x) & ((1<<w)-1) );}// SET VALUEinteger set_flag(integer i, integer w, integer x){ // i = integer to set db = db & ~( (1<<w)-1 <<x); // Mask return db = db | (i & (1<<w)-1) <<x;}integer db;default{ state_entry() { //class 5, faction 7, fully clothed, male db = db_record(5, 7, 3, 1); set_flag(1, 3, 6); // 0x7 = 111 set_flag(1, 3, 3); // 0x7 = 111 set_flag(1, 2, 1); // 0x3 = 11 set_flag(0, 1, 0); // 0x1 = 1 llOwnerSay("current flags: " +(string)get_flag(3, 6)+" " +(string)get_flag(3, 3)+" " +(string)get_flag(2, 1)+" " +(string)get_flag(1, 0)); }}
I also wrote this down because it was by far the most helpful set of sentences in the whole thread for me:
//It would be handy to have a way to create that mask of 0x7.//If we shift a solitary one bit left by 3 places, we get 8.//The expression (1<<w)-1 produces a field of ones w bits wide.// 1 = 0001// 0001 <<3 = 1000 (8)// 8-1 = 7 = 0111 (0x7)