Using bitwise not(~) gives syntax error -> Can't clear multiple bits at once


I’ve written a PRU program which manipulates bits in one of the registers. The program does not compile with PASM when I use the ~ operator.

Setting multiple bits works:

or      r3.b0, r3.b0, 1<<3    // Set bit 3

but clearing multiple bits does not:

and     r3.b0, r3.b0, ~(1<<3)    // Clear bit 3

The exact compiler error is " Error: Syntax error in parameter 3". This should work since it comes straight from the TI wiki on the PRU:

Is this a bug or am I doing something wrong?



Have you tested masking the ~(1<<3) to 8-bits ? It's possible that the assembler sees a 32-bit constant where only an 8-bit value is allowed.

-- Bas

Hi Bas,

You are right! The following works:
AND r3.b0, r3.b0, (~(1<<3) & 0xff) // Clear bit 3

It’s strange that (1<<3) is interpreted as 8-bit, but ~(1<<3) is interpreted as 32-bit. It appears to be an undocumented quirk of using this operator? Thanks for your help!


Look at it this way: The data type is always 32 bits. (1<<3) or 0x00000008 can be used as 8-bit number without data loss. ~(1<<3) or 0xFFFFFFF7 can't be used as an 8-bit number without data loss. If you keep this in mind when coding you'll be safe.

The assembler is rather simple in parsing the input. It probably just compares the parsed 32-bit result with the allowed range (0-255).

-- Bas

That makes perfect sense. Thanks Bas!