English Amiga Board

English Amiga Board (https://eab.abime.net/index.php)
-   Coders. Blitz Basic (https://eab.abime.net/forumdisplay.php?f=126)
-   -   Unsigned bytes (https://eab.abime.net/showthread.php?t=96393)

MickGyver 19 February 2019 11:40

Unsigned bytes
 
Is it possible to have a byte (.b) be handled as unsigned rather than signed?

I'm reading a tilemap as a binary file with one byte representing one tile. The values in the tilemap are unsigned (0-255). I read the tilemap into a byte array in Blitz Basic and the values become signed when reading from the array (all values after 128 become negative). Can I overcome this without using a word (.w) array?

MickGyver 19 February 2019 13:13

I just figured out one way to do it. If you look at the example:

Code:

; Byte variables in BB2 are signed ( -127..128)

; So let's set the value of a byte variable to 200
variable.b=200

; If we now read the variable it will have a value of -56, since values above 128 are negative for a signed byte.
Print variable ; -56 is printed, which is NOT what we want

; If we use bitwise AND on the variable with 255 (%11111111), we get the correct value
Print (variable & 255) ; 200 is printed, which IS what we want

Any better ways?

Daedalus 20 February 2019 21:53

Yeah, all variables are signed in Blitz unfortunately. The simplest way around it would be to use a Word array, with the obvious downside of doubled memory usage. It shouldn't significantly slow things down however, given the 16-bit bus on even the lowest of Amigas.

MickGyver 21 February 2019 11:44

Quote:

Originally Posted by Daedalus (Post 1305980)
Yeah, all variables are signed in Blitz unfortunately. The simplest way around it would be to use a Word array, with the obvious downside of doubled memory usage. It shouldn't significantly slow things down however, given the 16-bit bus on even the lowest of Amigas.

Thanks! I will move to using a word array if the bitwise AND trick will show to be too slow. I would save some memory by using bytes though. With a 16-bit bus, is there actually ANY speed difference with using 8-bit or 16-bit variables?

Daedalus 21 February 2019 16:09

Probably not. If the compiler was clever and saw that you were loading multiple 8-bit values from adjacent locations (such as a string or an array), it might be able to load two at once, but I don't think the Blitz compiler is all that smart when you're dealing with each value separately. It would be an interesting benchmark to test, but I can't see there being any difference at all.

idrougge 23 March 2019 02:21

You can turn off overflow errors in the compiler settings if you know what you're doing.

Amiga1992 23 March 2021 18:49

Well hellooooo, it's me going nuts again over some undecipherable bug, and it turns out, it's Blitz pulling the rug from under my feet again and not me doing something wrong :mad

This "all variables are signed" shit is mental.
Now I need to do this with a Word variable which was throwing negative values after 65KB :banghead:banghead:banghead

Thanks MickGyver and Daedalus for the light thrown on this.

E-Penguin 10 April 2021 21:00

Quote:

Originally Posted by Akira (Post 1472520)
This "all variables are signed" shit is mental.

Same in Java - I just end up pretending everything is unsigned and apply bit masks where needed.


All times are GMT +2. The time now is 19:42.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.

Page generated in 0.05033 seconds with 11 queries