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: CanadaPosts: 1082 |
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 StatesPosts: 286 |
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: AustraliaPosts: 3165 |
Not on the Micromite. 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: CanadaPosts: 1082 |
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 KingdomPosts: 8566 |
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: CanadaPosts: 1082 |
> ? 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: GermanyPosts: 1133 |
@vegipete ?? Peter said: Hmm. Do I miss something? Michael |
||||
vegipete Guru Joined: 29/01/2013 Location: CanadaPosts: 1082 |
<<< 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: AustraliaPosts: 5879 |
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 |