Bitwise operations in numeric constants

Some of my bit constants are getting out of hand, so I’d like to simplify and reduce errors.

However I can’t seem to create object constants using “1 << 0”, “1 << 1” & “1 << 2” is there a way to do this in Xojo, other than “&b1”, “&b10”, “&b100” ?

1 Like

When declaring a constant, I don’t think so.

When defining constants in a method, yes.

const kShift1 as UInt64 = 2^1
const kShift2 as UInt64 = 2^2

1 Like

Thanks Kem, sadly these constants are used in various places throughout the application, so they need to be defined as object constants.

I’ve created a note in the object where it contains the constant name, bitwise value 1<<N and the result.

It’s one the biggest missing features in Xojo. Unbelievable that this not been added yet.

2 Likes

@Sam_Rowlands - have you tried Binary Enums?

Well… Sigh… You, Xojo, I, half the forum and probably even newbies that have been in the forum long enough all know why QOL features like this have not been implemented.

I haven’t, I will try to find the time to poke at 'em.

They only exist in modules and they render as a class, but they do have Operator_ methods for the logic methods and have a Contains method that’s the equivalent of

Dim a as Boolean = (X and 12 = 12)

The documentation is pretty scant on binary enums and my tests seem to confirm that they don’t do what I want.

Basically I want some form of global constant that is declared by shifting a bit left. I am currently storing 21 booleans in a single Uint32.

valueA = 1<<0
valueB = 1<<1
valueC = 1<<2
valueD = 1<<3

etc. This way I can reduce the likelihood of entering in a slightly incorrect value and screwing up other settings, i.e. 1,048,577 alters two bits instead of just one.

I am sure that this isn’t the most efficient way of storing these values, but it’s already in a shipping application.

So if you make a binary enum, just put

Value1
Value2
Value3
Value4

It’ll automatically assign 1, 2, 4, 8

If you put in

Value5 = 32

It’ll count up from there. You can also put in values that are combinations:

Value5_1 = 33

Then in your code, you can use

MyEnum.Value1 or MyEnum.Value4

I’m not sure from your description why these don’t match your requirements. Internally they’re stored in an integer, and they won’t let you accidentally add two of the same item which it seems that your constants might of you accidentally used + instead of Or.

1 Like

Thanks @Greg_O , I was too busy trying to set each enum value to notice that I didn’t have to do so.

I couldn’t get it to count up from the previous value though.

Screen Shot 2023-02-12 at 11.41.15 AM

When I tested the value while the app was running, they’re as they’re shown in the IDE.

That’s not what I remember, but my recollection may be wrong.

Adding any “custom” values are excluded from the auto increment.
Even the auto generated name skips custom values
Screenshot 2023-02-11 at 11.27.53 PM

1 Like

Yeah, I misremembered that, although there was probably a good reason, I can’t tell you what it was. But the editor doesn’t expect you to keep the default names. They’re just placeholders as enum names should be meaningful.

1 Like