Wish Unsigned Byte

Roger Taylor

Member
Licensed User
Longtime User
I second this motion, but in B4A as well. I find it astonishing that through the life of the Basic4 compilers that an unsigned byte is not even thought of. Is this a rebellious choice of the authors? It's certainly painful knowing that I've started a serious project only to hit a road block this ridiculous.

Simply put, I need to have a large array of bytes and actually use each byte as I need as an unsigned byte.
 

emexes

Expert
Licensed User
an unsigned byte is not even thought of. Is this a rebellious choice of the authors?

It is a victim at the intersection of Rue de Computer Science and Reality Way, and a carry-over from Java, similar to 0x for hex literals rather than &H.

I am thankful that B4X Bytes at least accept values of 128..255, even if they won't give 'em back.

While we're here: the easiest way I know of to convert them from signed back to unsigned is Bit.And(SignedByte, 0xFF)
(rather than the If convolutions that are sometimes bandied about)

It's certainly painful knowing that I've started a serious project only to hit a road block this ridiculous.

More of a pot hole than a road block 🤣 but I share your pain 🍻 it irritates me daily too, and I would be be quite happy if there was a UByte type, or an option to default Bytes to unsigned. Although that then raises the question of: what also about Shorts, and even Ints and Longs?

edit: actually, I like UByte better, because that way doesn't break existing code like invoking an unsigned default option would
 
Last edited:

Roger Taylor

Member
Licensed User
Longtime User
I was able to get temporary relief by doing bit.and(0xff, bytevar) but bit.and(bytevar, 0xff) doesn't seem to work... and I'm just using the result in a quick byte*16 lookup table. Later I'll need to work a lot with CPU bytes in my retro emulator and it looks like I'm going to have to do an insane amount of conversions. We'll see. Yes, major pot hole in the road.

I mean, just because Java uses signed bytes doesn't mean a Basic compiler (to Java) can't offer unsigned and deal with it.
 

emexes

Expert
Licensed User
I was able to get temporary relief by doing bit.and(0xff, bytevar) but bit.and(bytevar, 0xff) doesn't seem to work...

That anomaly seems highly unlikely. More likely to be a misreading during the heat of a programming battle long ago, and you've just stuck ever since with what you know works.

Unless somehow a 0xFF was interpreted as a Byte (-1) which would be 0xFFFFFFFF as an Int, and thus not mask anything. But I'm pretty sure both arguments to Bit.And are Ints ie 32 bits (signed, not that it matters...)
 

kimstudio

Active Member
Licensed User
Longtime User
temporary relief by doing bit.and(0xff, bytevar) but bit.and(bytevar, 0xff) doesn't seem to work...
Is this true? 😮

Question: If B4X will provide unsigned byte, internally it will still use Bit.And() to convert (?) so there will be no performance raise right?
 

emexes

Expert
Licensed User
Question: If B4X will provide unsigned byte, internally it will still use Bit.And() to convert (?) so there will be no performance raise right?

Currently (I think) B4X promotes Bytes to (usually) Ints for calculation, and a UByte type would probably convert just as fast, but stuff the top 24 bits with zeroes rather than the high bit (sign bit) of the byte.

Nope, you're (probably) right. There is a JVM instruction bipush "push a byte onto the stack as an integer value" which presumably means Java (signed) bytes. Unisgned bytes would require an extra instruction or two.

There would be a programmer performance raise, though. 🍻
 
Last edited:

emexes

Expert
Licensed User
Not if the result should be, or is okay to be, an integer.
I need an uns8 * 16 to get a correct integer offset into my font array (bytes)

Offset into 4096 byte font table is Bit.ShiftLeft(Bit.And(CharByte, 0xFF), 4) ?

Actually, now that I'm on a lunch break, I should just pull my finger out and check that Bit.And(bytevar, 0xFF) and Bit.And(0xFF, bytevar) are equivalent. 🙃
 

emexes

Expert
Licensed User
and check that Bit.And(bytevar, 0xFF) and Bit.And(0xFF, bytevar) are equivalent. 🙃

B4X:
For I = 0 To 255
    Dim VarByte As Byte = I
   
    Dim OffsetVC As Int = Bit.ShiftLeft(Bit.And(VarByte, 0xFF), 4)
    Dim OffsetCV As Int = Bit.ShiftLeft(Bit.And(0xFF, VarByte), 4)
   
    If OffsetVC = OffsetCV Then
        Dim Flag As String = "ok"
    Else    
        Dim Flag As String = "WTF?"
    End If
   
    Log(                                   _
        I                          & TAB & _
        VarByte                    & TAB & _
        OffsetVC                   & TAB & _
        OffsetCV                   & TAB & _
        Flag                       & TAB & _
        Bit.ToHexString(OffsetVC)          _
    )
Next

Log output:
Waiting for debugger to connect...
Program started.
0    0    0    0    ok    0
1    1    16    16    ok    10
2    2    32    32    ok    20
3    3    48    48    ok    30
4    4    64    64    ok    40
5    5    80    80    ok    50
6    6    96    96    ok    60
7    7    112    112    ok    70
8    8    128    128    ok    80
9    9    144    144    ok    90
10    10    160    160    ok    a0
11    11    176    176    ok    b0
12    12    192    192    ok    c0
13    13    208    208    ok    d0
14    14    224    224    ok    e0
15    15    240    240    ok    f0
16    16    256    256    ok    100
17    17    272    272    ok    110
18    18    288    288    ok    120
19    19    304    304    ok    130
20    20    320    320    ok    140
21    21    336    336    ok    150
22    22    352    352    ok    160
23    23    368    368    ok    170
24    24    384    384    ok    180
25    25    400    400    ok    190
26    26    416    416    ok    1a0
27    27    432    432    ok    1b0
28    28    448    448    ok    1c0
29    29    464    464    ok    1d0
30    30    480    480    ok    1e0
31    31    496    496    ok    1f0
32    32    512    512    ok    200
33    33    528    528    ok    210
34    34    544    544    ok    220
35    35    560    560    ok    230
36    36    576    576    ok    240
37    37    592    592    ok    250
38    38    608    608    ok    260
39    39    624    624    ok    270
40    40    640    640    ok    280
41    41    656    656    ok    290
42    42    672    672    ok    2a0
43    43    688    688    ok    2b0
44    44    704    704    ok    2c0
45    45    720    720    ok    2d0
46    46    736    736    ok    2e0
47    47    752    752    ok    2f0
48    48    768    768    ok    300
49    49    784    784    ok    310
50    50    800    800    ok    320
51    51    816    816    ok    330
52    52    832    832    ok    340
53    53    848    848    ok    350
54    54    864    864    ok    360
55    55    880    880    ok    370
56    56    896    896    ok    380
57    57    912    912    ok    390
58    58    928    928    ok    3a0
59    59    944    944    ok    3b0
60    60    960    960    ok    3c0
61    61    976    976    ok    3d0
62    62    992    992    ok    3e0
63    63    1008    1008    ok    3f0
64    64    1024    1024    ok    400
65    65    1040    1040    ok    410
66    66    1056    1056    ok    420
67    67    1072    1072    ok    430
68    68    1088    1088    ok    440
69    69    1104    1104    ok    450
70    70    1120    1120    ok    460
71    71    1136    1136    ok    470
72    72    1152    1152    ok    480
73    73    1168    1168    ok    490
74    74    1184    1184    ok    4a0
75    75    1200    1200    ok    4b0
76    76    1216    1216    ok    4c0
77    77    1232    1232    ok    4d0
78    78    1248    1248    ok    4e0
79    79    1264    1264    ok    4f0
80    80    1280    1280    ok    500
81    81    1296    1296    ok    510
82    82    1312    1312    ok    520
83    83    1328    1328    ok    530
84    84    1344    1344    ok    540
85    85    1360    1360    ok    550
86    86    1376    1376    ok    560
87    87    1392    1392    ok    570
88    88    1408    1408    ok    580
89    89    1424    1424    ok    590
90    90    1440    1440    ok    5a0
91    91    1456    1456    ok    5b0
92    92    1472    1472    ok    5c0
93    93    1488    1488    ok    5d0
94    94    1504    1504    ok    5e0
95    95    1520    1520    ok    5f0
96    96    1536    1536    ok    600
97    97    1552    1552    ok    610
98    98    1568    1568    ok    620
99    99    1584    1584    ok    630
100    100    1600    1600    ok    640
101    101    1616    1616    ok    650
102    102    1632    1632    ok    660
103    103    1648    1648    ok    670
104    104    1664    1664    ok    680
105    105    1680    1680    ok    690
106    106    1696    1696    ok    6a0
107    107    1712    1712    ok    6b0
108    108    1728    1728    ok    6c0
109    109    1744    1744    ok    6d0
110    110    1760    1760    ok    6e0
111    111    1776    1776    ok    6f0
112    112    1792    1792    ok    700
113    113    1808    1808    ok    710
114    114    1824    1824    ok    720
115    115    1840    1840    ok    730
116    116    1856    1856    ok    740
117    117    1872    1872    ok    750
118    118    1888    1888    ok    760
119    119    1904    1904    ok    770
120    120    1920    1920    ok    780
121    121    1936    1936    ok    790
122    122    1952    1952    ok    7a0
123    123    1968    1968    ok    7b0
124    124    1984    1984    ok    7c0
125    125    2000    2000    ok    7d0
126    126    2016    2016    ok    7e0
127    127    2032    2032    ok    7f0
128    -128    2048    2048    ok    800
129    -127    2064    2064    ok    810
130    -126    2080    2080    ok    820
131    -125    2096    2096    ok    830
132    -124    2112    2112    ok    840
133    -123    2128    2128    ok    850
134    -122    2144    2144    ok    860
135    -121    2160    2160    ok    870
136    -120    2176    2176    ok    880
137    -119    2192    2192    ok    890
138    -118    2208    2208    ok    8a0
139    -117    2224    2224    ok    8b0
140    -116    2240    2240    ok    8c0
141    -115    2256    2256    ok    8d0
142    -114    2272    2272    ok    8e0
143    -113    2288    2288    ok    8f0
144    -112    2304    2304    ok    900
145    -111    2320    2320    ok    910
146    -110    2336    2336    ok    920
147    -109    2352    2352    ok    930
148    -108    2368    2368    ok    940
149    -107    2384    2384    ok    950
150    -106    2400    2400    ok    960
151    -105    2416    2416    ok    970
152    -104    2432    2432    ok    980
153    -103    2448    2448    ok    990
154    -102    2464    2464    ok    9a0
155    -101    2480    2480    ok    9b0
156    -100    2496    2496    ok    9c0
157    -99    2512    2512    ok    9d0
158    -98    2528    2528    ok    9e0
159    -97    2544    2544    ok    9f0
160    -96    2560    2560    ok    a00
161    -95    2576    2576    ok    a10
162    -94    2592    2592    ok    a20
163    -93    2608    2608    ok    a30
164    -92    2624    2624    ok    a40
165    -91    2640    2640    ok    a50
166    -90    2656    2656    ok    a60
167    -89    2672    2672    ok    a70
168    -88    2688    2688    ok    a80
169    -87    2704    2704    ok    a90
170    -86    2720    2720    ok    aa0
171    -85    2736    2736    ok    ab0
172    -84    2752    2752    ok    ac0
173    -83    2768    2768    ok    ad0
174    -82    2784    2784    ok    ae0
175    -81    2800    2800    ok    af0
176    -80    2816    2816    ok    b00
177    -79    2832    2832    ok    b10
178    -78    2848    2848    ok    b20
179    -77    2864    2864    ok    b30
180    -76    2880    2880    ok    b40
181    -75    2896    2896    ok    b50
182    -74    2912    2912    ok    b60
183    -73    2928    2928    ok    b70
184    -72    2944    2944    ok    b80
185    -71    2960    2960    ok    b90
186    -70    2976    2976    ok    ba0
187    -69    2992    2992    ok    bb0
188    -68    3008    3008    ok    bc0
189    -67    3024    3024    ok    bd0
190    -66    3040    3040    ok    be0
191    -65    3056    3056    ok    bf0
192    -64    3072    3072    ok    c00
193    -63    3088    3088    ok    c10
194    -62    3104    3104    ok    c20
195    -61    3120    3120    ok    c30
196    -60    3136    3136    ok    c40
197    -59    3152    3152    ok    c50
198    -58    3168    3168    ok    c60
199    -57    3184    3184    ok    c70
200    -56    3200    3200    ok    c80
201    -55    3216    3216    ok    c90
202    -54    3232    3232    ok    ca0
203    -53    3248    3248    ok    cb0
204    -52    3264    3264    ok    cc0
205    -51    3280    3280    ok    cd0
206    -50    3296    3296    ok    ce0
207    -49    3312    3312    ok    cf0
208    -48    3328    3328    ok    d00
209    -47    3344    3344    ok    d10
210    -46    3360    3360    ok    d20
211    -45    3376    3376    ok    d30
212    -44    3392    3392    ok    d40
213    -43    3408    3408    ok    d50
214    -42    3424    3424    ok    d60
215    -41    3440    3440    ok    d70
216    -40    3456    3456    ok    d80
217    -39    3472    3472    ok    d90
218    -38    3488    3488    ok    da0
219    -37    3504    3504    ok    db0
220    -36    3520    3520    ok    dc0
221    -35    3536    3536    ok    dd0
222    -34    3552    3552    ok    de0
223    -33    3568    3568    ok    df0
224    -32    3584    3584    ok    e00
225    -31    3600    3600    ok    e10
226    -30    3616    3616    ok    e20
227    -29    3632    3632    ok    e30
228    -28    3648    3648    ok    e40
229    -27    3664    3664    ok    e50
230    -26    3680    3680    ok    e60
231    -25    3696    3696    ok    e70
232    -24    3712    3712    ok    e80
233    -23    3728    3728    ok    e90
234    -22    3744    3744    ok    ea0
235    -21    3760    3760    ok    eb0
236    -20    3776    3776    ok    ec0
237    -19    3792    3792    ok    ed0
238    -18    3808    3808    ok    ee0
239    -17    3824    3824    ok    ef0
240    -16    3840    3840    ok    f00
241    -15    3856    3856    ok    f10
242    -14    3872    3872    ok    f20
243    -13    3888    3888    ok    f30
244    -12    3904    3904    ok    f40
245    -11    3920    3920    ok    f50
246    -10    3936    3936    ok    f60
247    -9    3952    3952    ok    f70
248    -8    3968    3968    ok    f80
249    -7    3984    3984    ok    f90
250    -6    4000    4000    ok    fa0
251    -5    4016    4016    ok    fb0
252    -4    4032    4032    ok    fc0
253    -3    4048    4048    ok    fd0
254    -2    4064    4064    ok    fe0
255    -1    4080    4080    ok    ff0
Program terminated (StartMessageLoop was not called).
 

Roger Taylor

Member
Licensed User
Longtime User
Then some other problem was happening at the time I assumed it was the ordering for Bit.And. I just swapped the order, knowing it was unlikely to have any effect, but my offsets were correct. Anyway....
I also assumed that a Multiply on these modern CPUs might be quicker than 4 Left Shifts. Additionally, the reason I haven't packed the functions together is because I've been trying to figure out where the problem was. I'll tighten and speed things up as I go.
 

agraham

Expert
Licensed User
Longtime User
I also assumed that a Multiply on these modern CPUs might be quicker than 4 Left Shifts
Unlikely, decades ago even our minicomputers had barrel shifters which could do any shift in a single CPU cycle. But unless you are programming in assembler this level of concern is irrelevant. You have a virtual machine and optimizing just in time compilers between your code and what the CPU sees so you can't be certain of anything low level.
 
Top