Xiija Posted March 16, 2021 Share Posted March 16, 2021 so, I have several flags/toggles , and would like to check their state. Using code from these forums, i can type a number on channel 2 and toggle bitflags. integer ttl_Bits; default { state_entry() { llListen(2, "", llGetOwner(), ""); } listen(integer c, string n, key i, string m) { ttl_Bits = ttl_Bits ^ (1 << (integer)m); llOwnerSay( "\nBit-flag: " + m + " is toggled " + llList2String(["OFF","ON"], (ttl_Bits >> (integer)m) & 0x1) + "\nTotal of Bits: " + (string)ttl_Bits + "\n" ); } } If I toggle flags 1,2,3 & 4 on - the ttl is 30. If I toggle flags 1,2, 4 on - the ttl is 22. is there any way to use the ttl number to decode which flags are on? Thanx for any ... Link to comment Share on other sites More sharing options...
KT Kingsley Posted March 16, 2021 Share Posted March 16, 2021 Bitwise and & (as opposed to logical and &&). Set the testing value to whichever bit you want to check (either by assigning a value directly, e.g. 1, 2, 4, 8, 16, 32... etc. or by assigning 1 to it and shifting as required) and bitwise and it with the value you want to test: if (test_value & value_to_be_tested) is TRUE then the corresponding bits in both text_value and value_to_be_tested are set, and if it's FALSE then the bit in value_to_be_tested is not set (the bit in test_value will, of course, be set because you set it yourself). And that pretty much sums up my knowledge of bitfields. Someone who knows more about this will be along soon... Link to comment Share on other sites More sharing options...
Madelaine McMasters Posted March 16, 2021 Share Posted March 16, 2021 (edited) integer Flag0 = 1; integer Flag1 = 2; integer Flag2 = 4; integer Flag3 = 8; integer Flag4 = 16; integer Flag5 = 32; integer Flag6 = 64; integer Flag7 = 128; integer ttl; ttl = Flag1 | Flag2 | Flag3 | Flag4; /ttl = 30 ttl = Flag1 | Flag1 | Flag4; /ttl = 22 Set flags. ttl = ttl | Flag1 | Flag4; Clear flags. ttl = ttl & (!Flag1) & (!Flag4); Toggle flags. ttl = ttl ^ (Flag1 | Flag4); Test for any specified flags set. if( ttl & (Flag0 | Flag4) ) () Test for all specified flags set. if ( (ttl & Flag0) && (ttl & Flag4) ) () Been a while since I've coded and I don't think I've ever used this method in LSL, so caveat emptor. Edited March 16, 2021 by Madelaine McMasters Corrections and clarifications 1 Link to comment Share on other sites More sharing options...
Quistess Alpha Posted March 16, 2021 Share Posted March 16, 2021 Madelaine beat me to the punch. it's mostly built-in functions and events that use that sort of thing (control() for example) with language-defined constants. because LSL doesn't support user-defined constants, you're probably better off just using 32 separate integers and setting them to 1 or 0, unless the meaning of the bits is self-evident from the position (for example up to 32 sittargets and a bitfield representing "is an avatar on this sittarget") Link to comment Share on other sites More sharing options...
Xiija Posted March 16, 2021 Author Share Posted March 16, 2021 perfect, ty all so much Link to comment Share on other sites More sharing options...
Quistess Alpha Posted March 16, 2021 Share Posted March 16, 2021 Also, for the record, you can be a bit clever about an 'all of these bits are set' test: integer bitfield = 1<<0|1<<2|1<<3|1<<4; // 1<<n is 2 raised to the nth power, or equivalently, an index for the nth bit. integer test = 1<<2|1<<3; // we want to know if all of these bits are set. if( (bitfield&test) == test) //returns true iff all bits in test are on in the bitfield Link to comment Share on other sites More sharing options...
Wulfie Reanimator Posted March 16, 2021 Share Posted March 16, 2021 I prefer the bit-shift method, or just pre-calculating the value if there's a specific multi-bit condition I want to check. Link to comment Share on other sites More sharing options...
Mollymews Posted March 17, 2021 Share Posted March 17, 2021 given the coding style to encode the flags then for code consistency/clarity suggest to use the same style thru out example: flagRaise(integer m) { // if is lowered then raise else is already raised ttl_bits = ttl_bits | (1 << m); } flagLower(integer m) { // if is raised then lower else is already lowered ttl_bits = ttl_bits & ~(1 << m); } flagToggle(integer m) { // if is raised then lower else if is lowered then raise ttl_bits = ttl_bits ^ (1 << m); } integer flagGet(integer m) { // return TRUE if is raised else return FALSE if is lowered return (ttl_bits >> m) & 1; } 1 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