Log In  

@zep:

Is this really the expected behavior?

> ?shl(1,32)
1
> ?shr(1,32)
1

I'd have expected 0 in both cases.

It works correctly for lesser shifts that push the 1 off of either end:

> ?shl(1,31)
0
> ?shr(1,31)
0

Seems like you're doing a modulo of the shift with 32:

> ?shl(1,33)
2
> ?shr(1,33)
0.5

Doesn't feel like the right behavior to me. Like, in assembly language, maybe, but not in a higher language.

PS: It would be nice to have bith arithmetic and logical shifts right, btw. :)

P#44828 2017-10-01 16:38 ( Edited 2017-11-17 02:51)

I kind of like this behavior since it lets you catch bits that would otherwise fall off right?
Am I understanding that correctly?

My only frame of reference is 6502 asm. There, bits fall off onto a Carry flag (single bit register) and then wrap if you rotate/shift again. You can use this for different effects. And at least on an 8 bit register it comes in handy a lot.

With your examples it could all be a coincidence though so maybe it is straight up taking the argument as a mod of the limit for shifting. Do other values follow this behavior too?

Edit: Yeah, after testing it out a bit I realized what I was thinking wouldn't make sense here. After testing with some larger numbers, I'm not 100% what it is doing, exactly. But here are some tests for smarter people to analyze:

(255<--0):255
(255<--1):510
(255<--2):1020
(255<--3):2040
(255<--4):4080
(255<--5):8160
(255<--6):16320
(255<--7):32640
(255<--8):-256
(255<--9):-512
(255<--10):-1024
(255<--11):-2048
(255<--12):-4096
(255<--13):-8192
(255<--14):-16384
(255<--15):-32768
(255<--16):0
(255<--17):0
(255<--18):0
(255<--19):0
(255<--20):0
(255<--21):0
(255<--22):0
(255<--23):0
(255<--24):0
(255<--25):0
(255<--26):0
(255<--27):0
(255<--28):0
(255<--29):0
(255<--30):0
(255<--31):0
(255<--32):255
(255-->0):255
(255-->1):127.5
(255-->2):63.75
(255-->3):31.875
(255-->4):15.938
(255-->5):7.969
(255-->6):3.984
(255-->7):1.992
(255-->8):0.9961
(255-->9):0.4981
(255-->10):0.249
(255-->11):0.1245
(255-->12):0.06226
(255-->13):0.03114
(255-->14):0.01557
(255-->15):0.00779
(255-->16):0.003899
(255-->17):0.001945
(255-->18):0.0009689
(255-->19):0.0004807
(255-->20):0.0002365
(255-->21):0.0001144
(255-->22):5.341e-005
(255-->23):2.289e-005
(255-->24):0
(255-->25):0
(255-->26):0
(255-->27):0
(255-->28):0
(255-->29):0
(255-->30):0
(255-->31):0
(255-->32):255
(1<--0):1
(1<--1):2
(1<--2):4
(1<--3):8
(1<--4):16
(1<--5):32
(1<--6):64
(1<--7):128
(1<--8):256
(1<--9):512
(1<--10):1024
(1<--11):2048
(1<--12):4096
(1<--13):8192
(1<--14):16384
(1<--15):-32768
(1<--16):0
(1<--17):0
(1<--18):0
(1<--19):0
(1<--20):0
(1<--21):0
(1<--22):0
(1<--23):0
(1<--24):0
(1<--25):0
(1<--26):0
(1<--27):0
(1<--28):0
(1<--29):0
(1<--30):0
(1<--31):0
(1<--32):1
(1-->0):1
(1-->1):0.5
(1-->2):0.25
(1-->3):0.125
(1-->4):0.06251
(1-->5):0.03126
(1-->6):0.01563
(1-->7):0.00782
(1-->8):0.003914
(1-->9):0.001961
(1-->10):0.0009842
(1-->11):0.0004959
(1-->12):0.0002518
(1-->13):0.0001297
(1-->14):6.866e-005
(1-->15):3.815e-005
(1-->16):2.289e-005
(1-->17):0
(1-->18):0
(1-->19):0
(1-->20):0
(1-->21):0
(1-->22):0
(1-->23):0
(1-->24):0
(1-->25):0
(1-->26):0
(1-->27):0
(1-->28):0
(1-->29):0
(1-->30):0
(1-->31):0
(1-->32):1
(2<--0):2
(2<--1):4
(2<--2):8
(2<--3):16
(2<--4):32
(2<--5):64
(2<--6):128
(2<--7):256
(2<--8):512
(2<--9):1024
(2<--10):2048
(2<--11):4096
(2<--12):8192
(2<--13):16384
(2<--14):-32768
(2<--15):0
(2<--16):0
(2<--17):0
(2<--18):0
(2<--19):0
(2<--20):0
(2<--21):0
(2<--22):0
(2<--23):0
(2<--24):0
(2<--25):0
(2<--26):0
(2<--27):0
(2<--28):0
(2<--29):0
(2<--30):0
(2<--31):0
(2<--32):2
(2-->0):2
(2-->1):1
(2-->2):0.5
(2-->3):0.25
(2-->4):0.125
(2-->5):0.06251
(2-->6):0.03126
(2-->7):0.01563
(2-->8):0.00782
(2-->9):0.003914
(2-->10):0.001961
(2-->11):0.0009842
(2-->12):0.0004959
(2-->13):0.0002518
(2-->14):0.0001297
(2-->15):6.866e-005
(2-->16):3.815e-005
(2-->17):2.289e-005
(2-->18):0
(2-->19):0
(2-->20):0
(2-->21):0
(2-->22):0
(2-->23):0
(2-->24):0
(2-->25):0
(2-->26):0
(2-->27):0
(2-->28):0
(2-->29):0
(2-->30):0
(2-->31):0
(2-->32):2
(5<--0):5
(5<--1):10
(5<--2):20
(5<--3):40
(5<--4):80
(5<--5):160
(5<--6):320
(5<--7):640
(5<--8):1280
(5<--9):2560
(5<--10):5120
(5<--11):10240
(5<--12):20480
(5<--13):-24576
(5<--14):16384
(5<--15):-32768
(5<--16):0
(5<--17):0
(5<--18):0
(5<--19):0
(5<--20):0
(5<--21):0
(5<--22):0
(5<--23):0
(5<--24):0
(5<--25):0
(5<--26):0
(5<--27):0
(5<--28):0
(5<--29):0
(5<--30):0
(5<--31):0
(5<--32):5
(5-->0):5
(5-->1):2.5
(5-->2):1.25
(5-->3):0.625
(5-->4):0.3125
(5-->5):0.1563
(5-->6):0.07813
(5-->7):0.03907
(5-->8):0.01954
(5-->9):0.009773
(5-->10):0.00489
(5-->11):0.002449
(5-->12):0.001228
(5-->13):0.000618
(5-->14):0.0003128
(5-->15):0.0001602
(5-->16):8.392e-005
(5-->17):3.815e-005
(5-->18):2.289e-005
(5-->19):0
(5-->20):0
(5-->21):0
(5-->22):0
(5-->23):0
(5-->24):0
(5-->25):0
(5-->26):0
(5-->27):0
(5-->28):0
(5-->29):0
(5-->30):0
(5-->31):0
(5-->32):5
P#44874 2017-10-02 21:14 ( Edited 2017-10-03 02:32)

I think what it comes down to is that it doesn't work the way you'd expect it to, so that's ultimately what needs fixing, regardless of what's actually wrong under the hood.

What's not certain is whether this is an issue in lua itself or in pico-8's custom lua intrinsics.

P#44877 2017-10-02 23:59 ( Edited 2017-10-03 03:59)

It's not a problem with the lua language itself. In other implementations, the Lua bitshift operations work like you would expect.

Here's some fun with Lua.org's lua demo :

for ii=1,256 do
  print( ii .. " : " .. (1 << ii) )
end

This produces the expected values until ii=63. and ii>=64 all come up with zero.


1 : 2
2 : 4
3 : 8
4 : 16
5 : 32
6 : 64
7 : 128
8 : 256
9 : 512
10 : 1024
11 : 2048
12 : 4096
13 : 8192
14 : 16384
15 : 32768
16 : 65536
17 : 131072
18 : 262144
19 : 524288
20 : 1048576
21 : 2097152
22 : 4194304
23 : 8388608
24 : 16777216
25 : 33554432
26 : 67108864
27 : 134217728
28 : 268435456
29 : 536870912
30 : 1073741824
31 : 2147483648
32 : 4294967296
33 : 8589934592
34 : 17179869184
35 : 34359738368
36 : 68719476736
37 : 137438953472
38 : 274877906944
39 : 549755813888
40 : 1099511627776
41 : 2199023255552
42 : 4398046511104
43 : 8796093022208
44 : 17592186044416
45 : 35184372088832
46 : 70368744177664
47 : 140737488355328
48 : 281474976710656
49 : 562949953421312
50 : 1125899906842624
51 : 2251799813685248
52 : 4503599627370496
53 : 9007199254740992
54 : 18014398509481984
55 : 36028797018963968
56 : 72057594037927936
57 : 144115188075855872
58 : 288230376151711744
59 : 576460752303423488
60 : 1152921504606846976
61 : 2305843009213693952
62 : 4611686018427387904
63 : -9223372036854775808
64 : 0
65 : 0
66 : 0
67 : 0
68 : 0
69 : 0
70 : 0
71 : 0
72 : 0
73 : 0
74 : 0
75 : 0
76 : 0
77 : 0
78 : 0
79 : 0
80 : 0
81 : 0
82 : 0
83 : 0
84 : 0
85 : 0
86 : 0
87 : 0
88 : 0
89 : 0
90 : 0
91 : 0
92 : 0
93 : 0
94 : 0
95 : 0
96 : 0
97 : 0
98 : 0
99 : 0
100 : 0
101 : 0
102 : 0
103 : 0
104 : 0
105 : 0
106 : 0
107 : 0
108 : 0
109 : 0
110 : 0
111 : 0
112 : 0
113 : 0
114 : 0
115 : 0
116 : 0
117 : 0
118 : 0
119 : 0
120 : 0
121 : 0
122 : 0
123 : 0
124 : 0
125 : 0
126 : 0
127 : 0
128 : 0
129 : 0
130 : 0
131 : 0
132 : 0
133 : 0
134 : 0
135 : 0
136 : 0
137 : 0
138 : 0
139 : 0
140 : 0
141 : 0
142 : 0
143 : 0
144 : 0
145 : 0
146 : 0
147 : 0
148 : 0
149 : 0
150 : 0
151 : 0
152 : 0
153 : 0
154 : 0
155 : 0
156 : 0
157 : 0
158 : 0
159 : 0
160 : 0
161 : 0
162 : 0
163 : 0
164 : 0
165 : 0
166 : 0
167 : 0
168 : 0
169 : 0
170 : 0
171 : 0
172 : 0
173 : 0
174 : 0
175 : 0
176 : 0
177 : 0
178 : 0
179 : 0
180 : 0
181 : 0
182 : 0
183 : 0
184 : 0
185 : 0
186 : 0
187 : 0
188 : 0
189 : 0
190 : 0
191 : 0
192 : 0
193 : 0
194 : 0
195 : 0
196 : 0
197 : 0
198 : 0
199 : 0
200 : 0
201 : 0
202 : 0
203 : 0
204 : 0
205 : 0
206 : 0
207 : 0
208 : 0
209 : 0
210 : 0
211 : 0
212 : 0
213 : 0
214 : 0
215 : 0
216 : 0
217 : 0
218 : 0
219 : 0
220 : 0
221 : 0
222 : 0
223 : 0
224 : 0
225 : 0
226 : 0
227 : 0
228 : 0
229 : 0
230 : 0
231 : 0
232 : 0
233 : 0
234 : 0
235 : 0
236 : 0
237 : 0
238 : 0
239 : 0
240 : 0
241 : 0
242 : 0
243 : 0
244 : 0
245 : 0
246 : 0
247 : 0
248 : 0
249 : 0
250 : 0
251 : 0
252 : 0
253 : 0
254 : 0
255 : 0
256 : 0


I'll bet the bug is exactly as you say : A modulus where there shouldn't be one.

P#44902 2017-10-04 10:00 ( Edited 2017-10-04 14:00)

I just ran into this bug - will check to see if maybe it has been fixed. In my case I'm finding shr(75,5) == 2.344... what??? It should equal 2. :-(

==update==
After searching the support pages I read that it is expected that shr returns a fraction because of the internal 16.16 fixed point representation. The more you know... :-)

https://www.lexaloffle.com/bbs/?tid=2369

P#46397 2017-11-16 21:51 ( Edited 2017-11-17 02:58)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 22:01:35 | 0.008s | Q:17