Page 2 of 2

Re: bitwise invert/complement?

Posted: Fri May 13, 2022 1:15 pm
by mnfisher
Thanks Ben,

We've encountered this before with simulation... However, I'd tested on an Arduino too - where I would have expected it to work okay. I maybe had a typo in the numbers - I'll have another play....

An oddity - as the byte should be unsigned - when I print to UART on Arduino it displayed as -103 which means the extension to long is incorrect?

Martin

Re: bitwise invert/complement?

Posted: Fri May 13, 2022 3:40 pm
by BenR
Hi Martin,

The Arduino will do the same as the simulation and treat the ~value2 as a 16-bit calc, this is typical behaviour of C on an 8-bit device through to a 64-bit device.

This is also the reason for generating your negative number as the most significant bit will be set.

Re: bitwise invert/complement?

Posted: Fri May 13, 2022 6:16 pm
by mnfisher
Thanks Ben,

Yes - it's just slightly unintuitive - if I use / compare a 8 bit variable I'm expecting it to just use or compare the 8 bits - expanding a 8 bit value to 16 or 32 bits - I would expect the 8 bit value to be used (not - as seems to be the case here a 16 bit intermediate value) - bit 7 is set in both values (153 and -103 - but 153 is displayed correctly so it the type promotion is playing the trick here..)

I'm sure it's all part of the C standard though - so we just have to live with it.

Martin