Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitwise operations in expressions for flag checking #13181

Open
5andr0 opened this issue May 20, 2024 · 0 comments
Open

Bitwise operations in expressions for flag checking #13181

5andr0 opened this issue May 20, 2024 · 0 comments

Comments

@5andr0
Copy link

5andr0 commented May 20, 2024

Motivation

With machine generated geojson it's more space saving to store up to 32 flags in a single integer value.

Design Alternatives

The only alternative is to store boolean properties for each flag, which creates unnecessary overhead and requires us to write a boolean check expression for each flag instead of checking for multiple flags only once with an and operation.

"properties": {
   "type1": true,
   "type2": false,
   "type3": true,
  ...
}

Right now for each flag I want to check inside an integer I have to create an expression, which shifts the bit to the right with a division by powers of 2 and modulo 2 to extract the bit for the flag == 1 check
['==', ['%', ['floor', ['/', ["get", "location-type"], 1<<flagIndex]], 2], 1]

Design

With bitwise operators we could do a check for multiple flags within a simple and operation:
['&', ["get", "location-type"], 0b10101]

Implementation

So for the flag checking use case it would be enough to have the & operator expression or labelled as 'hasFlags' expression.
But you could also just add all bitwise operators to the math expressions as

  • '&' and
  • 'xor' because ^ is already in use
  • 'not' because ! is already in use
  • '|' or
  • '<<' left shift
  • '>>' right shift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant