How do I use Bitflags

Arkless

New Member
Reaction score
31
I don't understand how to use bitflags (like in the GUI) within galaxy code.

Converting it to script (with the GUI), the type of my bitflag preset variable is integer. But using integers I can't use the given syntax for it (it says wrong type).
The syntax is the following:
MyBitflag = 0x00000001 | 0x00000002 | 0x00000004 | ... | 0x80000000; // for the different flags

if (MyBitflag == 0x00000001 | 0x00000008) {
DoSomething();
}



It's not hard to do that yourself, thought I prefer the version abouve.
My version would be:
ModI(Int/<2^(Flag-1)>,2) = 1 //== to check if a flag was set
Int = Int + <2^(Flag-1)> //== to set a flag
 

eXirrah

New Member
Reaction score
51
you must use Custom Script. The And operator is & and the Or operator
is |. To check if a flag is set in a range of flags use &.

For example you have flags:
flag 1, flag 2, flag 3

a function that will let you choose which flags to set and the variable
that holds the flags is called flags

if you want to check if flag 1 is set in flags then you do this
Code:
if (flags & flag 1) then
{
    the flag is set;
} else {
    the flag is not set;
};
I had the same problem yesterday and created a custom function


Code:
Compare Flags
    Options: Function
    Return Type: Boolean
    Parameters
        Flags = (Integer(Preset)) <Integer>
        Value = (Integer(Preset)) <Integer>
    Grammar Text: Compare Flags(Flags, Value)
    Hint Text: (None)
    Custom Script Code
    Local Variables
    Actions
        General - Custom Script: 
                if (lp_flags & lp_value) {
                    return true;
                };
                return false;

this function checks if Value flag is set in Flags flags.

Check out this library as well : Flag Operators Library
 

Arkless

New Member
Reaction score
31
My problem is something else ^^
I don't get why I can't use & | << >> ~ ^ operators at all. Whenever I do that I get a compiler error. I even tried to convert a GUI bitflag to galaxy script but it uses an integer as well.
 

eXirrah

New Member
Reaction score
51
Seems like bitwise operations in sc2 return integer, so you can't use
(My_Flags & 0x01 == true). You cannot compare integer to boolean but
seems like:
Code:
if (My_Flags & 0x01) {
    DoSomething()
}
works just fine.

I think that bitwise operators operate with integers and bitflags are presets.
You may need to convert bitflags to integers before performing operations on them.
 

Arkless

New Member
Reaction score
31
Seems like bitwise operations in sc2 return integer, so you can't use
(My_Flags & 0x01 == true). You cannot compare integer to boolean but
seems like:
Code:
if (My_Flags & 0x01) {
    DoSomething()
}
works just fine.
I know how bitmanipulation operators work, I just got a type error when I tried "if (Int == 0x00000001 | 0x00000008) {". It won't even let me gui lines, I get an error saying wrong type pointing on the line.
( | bit or)


edit:
Code:
if (My_Flags & 0x01) {
    DoSomething()
}

That works? Thats ridiculous, it should should return an integer as well:
3 & 5 = 1
0011 & 0101 = 1
 

Vestras

Retired
Reaction score
249
If you want to check if a bitwise flag is true, atleast in C#, you do this:
Code:
if ((comparisonVar & 0x10) != 0) {
 

eXirrah

New Member
Reaction score
51
You should check the library I posted link to in my first post.

There are functions and stuff that operate with bitflags, read the code
and you should understand how this works.
 

Arkless

New Member
Reaction score
31
You should check the library I posted link to in my first post.

There are functions and stuff that operate with bitflags, read the code
and you should understand how this works.

I did, it was nothing new to me, it still doesnt work. Gonna reinstall soon, the error is kinda weird (wrong type doesn't really fit there).
 

eXirrah

New Member
Reaction score
51
I get wrong type when I use (My_Flags == 0x01 | 0x02) too. It's not your
editor, it's just how galaxy script works. I started using similar statement
and then tested it all night wondering why it doesn't work until I found that
library and noticed that (My_Flags & 0x01) and (My_Flags & 0x02) is how it
must be in galaxy....

I think there is nothing you can do to change that...

If you do though, let me know.
 

Arkless

New Member
Reaction score
31
I get wrong type when I use (My_Flags == 0x01 | 0x02) too. It's not your
editor, it's just how galaxy script works. I started using similar statement
and then tested it all night wondering why it doesn't work until I found that
library and noticed that (My_Flags & 0x01) and (My_Flags & 0x02) is how it
must be in galaxy....

I think there is nothing you can do to change that...

If you do though, let me know.

k, thx, that's an answere that helped me ^^ (at least now I know that it was no error on my side :p)
 

Arkless

New Member
Reaction score
31
I just found the answere, the bitflag operators are slower than the ==.
That means that writing something like this:
(0x01 & 0x10 == 0x11)
Ends up like:
(0x01 & (0x10 == 0x11))

Resulting in:
(0x01 & (false))


I never used bitflags in C, but I guess in C the bitflag operators are faster, thus making this a blizzard fail (just a guess, enlighten me if i'm wrong).

So we need the right bracketing here.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top