[gdal-dev] Using bitwise & with muparser VRT expressions

Daniel Baston dbaston at gmail.com
Wed Dec 3 12:07:42 PST 2025


Hugh,

My read of this discussion [1] is that bitwise operators are not supported
and we would need to add them as a GDAL extension. This is pretty easy to
do: see [2]. Alternatively, bitwise operators are probably supported by the
ExprTk expression parser [3] (supported by GDAL) or this pull request [4].

Dan

[1]
https://github.com/beltoforion/muparser/discussions/154#discussioncomment-13104418
[2] https://github.com/OSGeo/gdal/pull/13187/files
[3] https://www.partow.net/programming/exprtk/
[4] https://github.com/OSGeo/gdal/pull/13470

On Wed, Dec 3, 2025 at 2:58 PM Hugh Graham via gdal-dev <
gdal-dev at lists.osgeo.org> wrote:

> Hi,
>
> I am using VRT muparser expressions for bitwise operations when creating
> masks and wanted to understand why I can't get the *bitwise & *operator
> working? My current solution is to use fmod which is fine (and I think
> equivalent) but using "&" would be convenient and a bit easier to read...
>
> To illustrate, the following file works:
>
> https://gist.github.com/h-a-graham/6db16e70e064ae8173f1052537c59761#file-fmask-fmodmask-vrt
>
> the expression used here is:"(fmod(Fmask, 16) >= 8) || (fmod(Fmask, 8) >=
> 4) ? 0 : 1"
>
> ```
> gdal convert "/vsicurl/
> https://gist.githubusercontent.com/h-a-graham/6db16e70e064ae8173f1052537c59761/raw/c48b15852bcc7c5252cae4b906c14b9a3205a33d/fmask-fmodmask.vrt"
> "test.tif"
> ```
>
> However, the following file fails:
>
> https://gist.github.com/h-a-graham/6db16e70e064ae8173f1052537c59761#file-fmask-bitmask-vrt
> the expression parsed as: "((Fmask & 8) > 0 || (Fmask & 4) >
> 0) ? 0 : 1" in the XML but provided as (to my xml parser):
>
> "((Fmask & 8) > 0 || (Fmask & 4) > 0) ? 0 : 1"
>
> ```
> gdal convert "/vsicurl/
> https://gist.githubusercontent.com/h-a-graham/6db16e70e064ae8173f1052537c59761/raw/c48b15852bcc7c5252cae4b906c14b9a3205a33d/fmask-bitmask.vrt"
> "test.tif"
> 0...10...20..ERROR 1: Unexpected token "& 8) > 0 || (Fmask & 4) > 0) ? 0 :
> 1 " found at position 8.
> ```
>
> So, is using "&" possible, or should I just stick with fmod and not worry
> about it?
>
> Any help would be much appreciated.
>
> Hugh
>
>
>
>
>
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20251203/628544c5/attachment.htm>


More information about the gdal-dev mailing list