![]() |
Forum Index : Microcontroller and PC projects : CMM2 right shift is unsigned?
Author | Message | ||||
epsilon![]() Senior Member ![]() Joined: 30/07/2020 Location: BelgiumPosts: 255 |
Hi, I was doing some fixed point arithmetic experiments and I noticed that right shift appears to be implemented as an unsigned shift even though the document says it's signed: Documentation: On 5.06 FW: > PRINT HEX$(&H8000000000000000>>1) 4000000000000000 Is there way to make it signed? Thanks, Ruben/Epsilon. Epsilon CMM2 projects |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5091 |
Hi Epsilon, Normally all shifts are unsigned since they are binary operations. They don't care if the content of a memory location is a character, or an integer number, or a float. If you want to shift to the right and treat it as a numeric, then you have to do a numeric operation on it. So a divide by 2 replacing the right shift. PicomiteVGA PETSCII ROBOTS |
||||
jirsoft![]() Guru ![]() Joined: 18/09/2020 Location: Czech RepublicPosts: 533 |
You can by-self protect the highest bit: DIM INTEGER a a = 1 << 63 and then when you need shift right: DIM INTEGER b = a AND (1 << 63) a = (a >> 1) OR b Jiri Napoleon Commander and SimplEd for CMM2 (GitHub), CMM2.fun |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10315 |
Sorry didn't see the original post. This is either a manual error or a bug depending on what is preferred. The origin of this is that on the PIC32 all shift right operations on 64-bit numbers are signed - there is no choice even though Geoff's C source asks for unsigned The CMM2 uses the same C source but the STM32 implements it properly so it gives an unsigned answer. I can either change the source to match the documentation or visa-versa Thoughts? |
||||
epsilon![]() Senior Member ![]() Joined: 30/07/2020 Location: BelgiumPosts: 255 |
Although I would prefer a signed shift, since MMBasic integers are signed, correcting the documentation is probably the best option at this point. Changing the source would break compatibility. In Java >>> is unsigned and >> is signed. Maybe you can do it the other way around in MMBasic? Or maybe add an ASR function? Epsilon CMM2 projects |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10315 |
I can do that - watch for 5.07.00b2 |
||||
epsilon![]() Senior Member ![]() Joined: 30/07/2020 Location: BelgiumPosts: 255 |
That's great! Thank you! Epsilon CMM2 projects |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |