Error codes

E0293: bitwise operator on a value with a unit

In a terminal: auk explain E0293 prints this page.

On this page

&, |, ^, ~, << and >> (and their &=-style assignments) work on the bits of a plain int. A unit of measure describes a quantity, and its bits mean nothing on their own: masking a length in metres or shifting a duration is almost certainly a bug. Strip the unit with int(x) when the bit pattern really is what you want.

Wrong#

auk
fn flags(n: int<B>): int = n & 0xff

Fixed#

auk
fn flags(n: int<B>): int = int(n) & 0xff

Common causes#

  • A byte count or ID carrying a unit used as a bit field: convert it with int(x) first.
  • Scaling by a power of two with <<: multiply instead (n * 4), which keeps the unit.

Stuck on a word? Try the glossary. Got a compiler error? Look up its code. Improve this page