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
bitwise invert/complement?
-
- Valued Contributor
- Posts: 1462
- http://meble-kuchenne.info.pl
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 136 times
- Been thanked: 713 times
-
- Matrix Staff
- Posts: 1926
- Joined: Mon Dec 07, 2020 10:06 am
- Has thanked: 503 times
- Been thanked: 686 times
Re: bitwise invert/complement?
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.
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 Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
-
- Valued Contributor
- Posts: 1462
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 136 times
- Been thanked: 713 times
Re: bitwise invert/complement?
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
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