Al: Bitwise Flag Options

Created on 14 Mar 2018  路  5Comments  路  Source: microsoft/AL

It would be useful if there were options that could be used as bitwise flags.

````CSharp
optionA = 1 = 0000 0001
optionB = 2 = 0000 0010
optionC = 4 = 0000 0100

3 = optionB and OptionC = 0000 0011
5 = optionA and OptionC = 0000 0101
6 = optionB and optionC = 0000 0110
7 = optionA and optionB and optionC = 0000 0111

optionD = 8 = 0000 1000

and so on......
````

al-language suggestion

Most helpful comment

Before we have that, we would need a proper bitwise OR operator in AL.
Wouldn't it be nice, if we could write:
SalesHeader.PostAction := SalesHeader.PostAction::Ship | SalesHeader.PostAction::Invoice

All 5 comments

Curiously, what would you use those for?

Before we have that, we would need a proper bitwise OR operator in AL.
Wouldn't it be nice, if we could write:
SalesHeader.PostAction := SalesHeader.PostAction::Ship | SalesHeader.PostAction::Invoice

@yrest I'm building such flags mostly with byte. Doing this manually in code units or manipulating the database or objects is annoying. I use them quite often (as in C# or c++). For example, for Id's that have different meanings or to mark something with flags. It is also interesting for queries because you can compare bitwise.

@vytjak has the perfect example ^^

It would probably make sense to add support for other bit operations, not only OR, at the same time.

@yrest I also see it this way: OR, XOR, AND, NOR plus negation and of course bitshifting would be nice << and >> for left and right shift.

Was this page helpful?
0 / 5 - 0 ratings