Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:46 03 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 : PicoMite - left shift (<<) operator - bugy ?!!

Author Message
homa

Guru

Joined: 05/11/2021
Location: Germany
Posts: 471
Posted: 09:20pm 15 Jan 2022
Copy link to clipboard 
Print this post

hello specialists,

I need an evaluation here.
Is the left shift (<<) function broken here?  Or do I have a mistake in my thinking? I found this while trying to store the RFID bitstream from the Manchester code in a 64 bit integer.
Look at the code and the output:

PicoMite MMBasic Version 5.07.01
Copyright 2011-2021 Geoff Graham
Copyright 2016-2021 Peter Mather

> list
' test shift - bug ?!!
Dim b=&hFFFFFFFFFFFFFFFF
Print Bin$(b),b
b=&b1
Print Bin$(b),b
b=b<<1 Xor 1
Print Bin$(b),b
b=b<<1 Xor 0
Print Bin$(b),b
b=0
Print Bin$(b),b
For i=1 To 65
  b=b<<1 Xor 1
  Print i, Bin$(b)
Next i
> run
1111111111111111111111111111111111111111111111111111111111111111        -1
1        1
11       3
110      6
0        0
1      1
2      11
3      111
4      1111
5      11111
6      111111
7      1111111
8      11111111
9      111111111
10     1111111111
11     11111111111
12     111111111111
13     1111111111111
14     11111111111111
15     111111111111111
16     1111111111111111
17     11111111111111111
18     111111111111111111
19     1111111111111111111
20     11111111111111111111
21     111111111111111111111
22     1111111111111111111111
23     11111111111111111111111
24     111111111111111111111111
25     1111111111111111111111111
26     11111111111111111111111111
27     111111111111111111111111111
28     1111111111111111111111111111
29     11111111111111111111111111111
30     111111111111111111111111111111
31     1111111111111111111111111111111
32     11111111111111111111111111111111
33     111111111111111111111111111111111
34     1111111111111111111111111111111111
35     11111111111111111111111111111111111
36     111111111111111111111111111111111111
37     1111111111111111111111111111111111111
38     11111111111111111111111111111111111111
39     111111111111111111111111111111111111111
40     1111111111111111111111111111111111111111
41     11111111111111111111111111111111111111111
42     111111111111111111111111111111111111111111
43     1111111111111111111111111111111111111111111
44     11111111111111111111111111111111111111111111
45     111111111111111111111111111111111111111111111
46     1111111111111111111111111111111111111111111111
47     11111111111111111111111111111111111111111111111
48     111111111111111111111111111111111111111111111111
49     1111111111111111111111111111111111111111111111111
50     11111111111111111111111111111111111111111111111111
51     111111111111111111111111111111111111111111111111111
52     1111111111111111111111111111111111111111111111111111
53     11111111111111111111111111111111111111111111111111111
54     1000000000000000000000000000000000000000000000000000000
55     10000000000000000000000000000000000000000000000000000000
56     100000000000000000000000000000000000000000000000000000000
57     1000000000000000000000000000000000000000000000000000000000
58     10000000000000000000000000000000000000000000000000000000000
59     100000000000000000000000000000000000000000000000000000000000
60     1000000000000000000000000000000000000000000000000000000000000
61     10000000000000000000000000000000000000000000000000000000000000
62     100000000000000000000000000000000000000000000000000000000000000
63     1000000000000000000000000000000000000000000000000000000000000000
64     1
65     11
>
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 09:35pm 15 Jan 2022
Copy link to clipboard 
Print this post

Dim integer b

You may also run into the sign bit causing odd results.
Edited 2022-01-16 07:36 by TassyJim
VK7JH
MMedit
 
homa

Guru

Joined: 05/11/2021
Location: Germany
Posts: 471
Posted: 10:09pm 15 Jan 2022
Copy link to clipboard 
Print this post

I don't think so, check the manual on page 21:
BUT there the variable in the example is marked %, so I changed the test programme that way. See there!
So I think it is not a direct error. The variable is by default a floating point number with no suffix.
:-( What a guessing error. ESPECIALLY BECAUSE THE FIRST LINES WORKED!

64-bit Unsigned Integers
MMBasic on the PicoMite supports 64-bit signed integers. This means that there are 63 bits for holding the
number and one bit (the most significant bit) which is used to indicate the sign (positive or negative). However
it is possible to use full 64-bit unsigned numbers as long as you do not do any arithmetic on the numbers.
64-bit unsigned numbers can be created using the &H, &O or &B prefixes to a number and these numbers can
be stored in an integer variable. You then have a limited range of operations that you can perform on these.
They are << (shift left), >> (shift right), AND (bitwise and), OR (bitwise or), XOR (bitwise exclusive or), INV
(bitwise inversion), = (equal to) and <> (not equal to). Arithmetic operators such as division or addition may
be confused by a 64-bit unsigned number and could return nonsense results.
Note that shift right is a signed operation. This means that if the top bit is a one (a negative signed number) and
you shift right then it will shift in ones to maintain the sign.
To display 64-bit unsigned numbers you should use the HEX$(), OCT$() or BIN$() functions.
For example, the following 64-bit unsigned operation will return the expected results:
X% = &HFFFF0000FFFF0044
Y% = &H800FFFFFFFFFFFFF
X% = X% AND Y%
PRINT HEX$(X%, 16)
Will display "800F0000FFFF0044"


> list
' test shift - bug ?!!
Dim b%=&hFFFFFFFFFFFFFFFF
Print Bin$(b%),b%
b%=&b1
Print Bin$(b%),b%
b%=b%<<1 Xor 1
Print Bin$(b%),b%
b%=b%<<1 Xor 0
Print Bin$(b%),b%
b%=0
Print Bin$(b%),b%
For i=1 To 65
  b%=b%<<1 Xor 1
  Print i, Bin$(b%)
Next i
> run
1111111111111111111111111111111111111111111111111111111111111111        -1
1        1
11       3
110      6
0        0
1      1
2      11
3      111
4      1111
5      11111
6      111111
7      1111111
8      11111111
9      111111111
10     1111111111
11     11111111111
12     111111111111
13     1111111111111
14     11111111111111
15     111111111111111
16     1111111111111111
17     11111111111111111
18     111111111111111111
19     1111111111111111111
20     11111111111111111111
21     111111111111111111111
22     1111111111111111111111
23     11111111111111111111111
24     111111111111111111111111
25     1111111111111111111111111
26     11111111111111111111111111
27     111111111111111111111111111
28     1111111111111111111111111111
29     11111111111111111111111111111
30     111111111111111111111111111111
31     1111111111111111111111111111111
32     11111111111111111111111111111111
33     111111111111111111111111111111111
34     1111111111111111111111111111111111
35     11111111111111111111111111111111111
36     111111111111111111111111111111111111
37     1111111111111111111111111111111111111
38     11111111111111111111111111111111111111
39     111111111111111111111111111111111111111
40     1111111111111111111111111111111111111111
41     11111111111111111111111111111111111111111
42     111111111111111111111111111111111111111111
43     1111111111111111111111111111111111111111111
44     11111111111111111111111111111111111111111111
45     111111111111111111111111111111111111111111111
46     1111111111111111111111111111111111111111111111
47     11111111111111111111111111111111111111111111111
48     111111111111111111111111111111111111111111111111
49     1111111111111111111111111111111111111111111111111
50     11111111111111111111111111111111111111111111111111
51     111111111111111111111111111111111111111111111111111
52     1111111111111111111111111111111111111111111111111111
53     11111111111111111111111111111111111111111111111111111
54     111111111111111111111111111111111111111111111111111111
55     1111111111111111111111111111111111111111111111111111111
56     11111111111111111111111111111111111111111111111111111111
57     111111111111111111111111111111111111111111111111111111111
58     1111111111111111111111111111111111111111111111111111111111
59     11111111111111111111111111111111111111111111111111111111111
60     111111111111111111111111111111111111111111111111111111111111
61     1111111111111111111111111111111111111111111111111111111111111
62     11111111111111111111111111111111111111111111111111111111111111
63     111111111111111111111111111111111111111111111111111111111111111
64     1111111111111111111111111111111111111111111111111111111111111111
65     1111111111111111111111111111111111111111111111111111111111111111
>

Edited 2022-01-16 08:38 by homa
 
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