Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 11:44 19 Apr 2024 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 : MMBasic and Bit Shifts.

Author Message
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1082
Posted: 03:13am 01 Aug 2021
Copy link to clipboard 
Print this post

What would you expect a bit shift by a negative number to do?

Would you expect, for example, 64 << -2 to be the same as 64 >> 2?

As it stands, bit shifting by a negative gives 0. Could that be changed?
(CMM2, but maybe others too.)
Visit Vegipete's *Mite Library for cool programs.
 
SimpleSafeName

Senior Member

Joined: 28/07/2019
Location: United States
Posts: 286
Posted: 03:50am 01 Aug 2021
Copy link to clipboard 
Print this post

Would it be up to the user not to wrap around into negative territory, or does that matter?

Say 16 << -6 for example.
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 03:53am 01 Aug 2021
Copy link to clipboard 
Print this post

  vegipete said  CMM2, but maybe others too.

Not on the Micromite.

  vegipete said  Could that be changed?

This is one for Peter but I suspect that it is a "feature" of the ARM architecture.

Geoff
Geoff Graham - http://geoffg.net
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1082
Posted: 05:48am 01 Aug 2021
Copy link to clipboard 
Print this post

  SimpleSafeName said  Would it be up to the user not to wrap around into negative territory, or does that matter?

I'd say it's up the user to keep track of shift size. And said user better have a good idea what's going on if shifting negative integers.

There is the >>> operator which does treat negative integers sensibly. Although I haven't determined what it does with a negative shift amount.

The effect of <<< is slightly weird - maybe a left shift of nothing, followed by a less than comparison, giving a result of 0 or 1.
Visit Vegipete's *Mite Library for cool programs.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8566
Posted: 07:28am 01 Aug 2021
Copy link to clipboard 
Print this post

  Quote  As it stands, bit shifting by a negative gives 0. Could that be changed?


The CMM2 code is identical to all other MMBasic implementations so this must be what the ARM H/W does

The only thing I could do is trap negative shifts as an error.

The difference between >> and >>> is described on page 56 of the 5.07.00 manual
<<< makes no sense and does not exist
Edited 2021-08-01 17:28 by matherp
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1082
Posted: 06:15pm 01 Aug 2021
Copy link to clipboard 
Print this post

> ? 64 >> 2        ' #1
16
> ? 64 >>> 2       ' #2
16
> ? -64 >> 2       ' #3
4611686018427387888
> ? -64 >>> 2      ' #4
-16
> ? 64 >>> -2      ' #5
0
> ? -64 >>> -2     ' #6
-4294967296
> ? 16 << 2        ' #7
64
> ? 16 <<< 2       ' #8
0
> ? 1 << 6         ' #9
64
> ? 1 <<< 6        ' #10
1

#1 to #5 : as expected
#6       : don't know
#7, #9   : as expected
#8, #10  : parsing error?

My original question of whether n >> -s could be treated the same as n << s (and by extension, n << -r same as n >> r) comes about from some pixel/bit map shifting code I'm fiddling with. As it stands, I need different pieces of code for left and right shifts.
Visit Vegipete's *Mite Library for cool programs.
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 07:10pm 01 Aug 2021
Copy link to clipboard 
Print this post

  vegipete said  
...
> ? 16 <<< 2       ' #8
0
> ? 1 << 6         ' #9
64
> ? 1 <<< 6        ' #10
1


#8, #10  : parsing error?

@vegipete
??
Peter said:
  matherp said  <<< makes no sense and does not exist


Hmm. Do I miss something?

Michael
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1082
Posted: 11:01pm 01 Aug 2021
Copy link to clipboard 
Print this post

<<< does not throw an error. Instead, it returns slightly unexplainable results. As a operator, it doesn't exist. But as 3 less than signs in a row, it does exist when perhaps it should be an error.
Visit Vegipete's *Mite Library for cool programs.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 5879
Posted: 11:12pm 01 Aug 2021
Copy link to clipboard 
Print this post

This is how the interpreter reads it:

> print 16 << < 2
0
> print 1 << < 6
1
>


It seems to skip over the << or treat it as 'x << 0' reducing the code to:
> print 16 < 2
0
> print 1 < 6
1
>


Not all errors get found by the interpreter.
Edited 2021-08-02 09:18 by TassyJim
VK7JH
MMedit   MMBasic Help
 
Print this page


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

© JAQ Software 2024