bitwise invert/complement?

For general Flowcode discussion that does not belong in the other sections.
mnfisher
Valued Contributor
Posts: 938
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: bitwise invert/complement?

Post 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

BenR
Matrix Staff
Posts: 1707
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 435 times
Been thanked: 598 times

Re: bitwise invert/complement?

Post 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.

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: bitwise invert/complement?

Post 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

Post Reply