Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 07:08 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2 right shift is unsigned?

Author Message
epsilon

Senior Member

Joined: 30/07/2020
Location: Belgium
Posts: 255
Posted: 12:27pm 04 Jan 2021
Copy link to clipboard 
Print this post

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:
  Quote  
These operate in a special way. << means that the value returned
will be the value of x shifted by y bits to the left while >> means the
same only right shifted. They are integer functions and any bits
shifted off are discarded. For a right shift any bits introduced are set
to the value of the top bit (bit 63). For a left shift any bits introduced
are set to zero.


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: Netherlands
Posts: 5091
Posted: 12:46pm 04 Jan 2021
Copy link to clipboard 
Print this post

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 Republic
Posts: 533
Posted: 12:55pm 04 Jan 2021
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 10315
Posted: 01:36pm 04 Jan 2021
Copy link to clipboard 
Print this post

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

  Quote  void op_shiftright(void) {
   iret = (long long int)((unsigned long long int)iarg1 >> (long long int)iarg2);
}


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: Belgium
Posts: 255
Posted: 01:46pm 04 Jan 2021
Copy link to clipboard 
Print this post

  matherp said  I can either change the source to match the documentation or visa-versa
Thoughts?


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 Kingdom
Posts: 10315
Posted: 02:12pm 04 Jan 2021
Copy link to clipboard 
Print this post

  Quote  In Java >>> is unsigned and >> is signed. Maybe you can do it the other way around in MMBasic?


I can do that - watch for 5.07.00b2
 
epsilon

Senior Member

Joined: 30/07/2020
Location: Belgium
Posts: 255
Posted: 02:25pm 04 Jan 2021
Copy link to clipboard 
Print this post

That's great! Thank you!
Epsilon CMM2 projects
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025