bitwise invert/complement?

mnfisher
Valued Contributor
Posts: 399
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 49 times
Been thanked: 227 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: 1009
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 243 times
Been thanked: 334 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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel

mnfisher
Valued Contributor
Posts: 399
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 49 times
Been thanked: 227 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