neilsf / XC-BASIC

A compiling BASIC dialect for the Commodore-64
https://xc-basic.net/
MIT License
74 stars 15 forks source link

Unsigned integer #108

Closed mrg-75 closed 4 years ago

mrg-75 commented 4 years ago

Hello,

I've just been writing some code that pokes characters directly into the screen memory, after calculating the screen position and storing it in an integer. When I tried to update the colour memory using a similar method, I realised I couldn't due to the upper limit of 32767.

Is there a work around to this, or obvious way of doing it that I'm missing? If not I think that an unsigned integer would be a necessary addition to the language.

neilsf commented 4 years ago

Hi Integers can be treated as signed or unsigned, depending on the context. PEEK and POKE treat them unsigned so go ahead and try POKE 55296, 1 - It will work. PRINT on the other hand treats them as signed. Being signed or unsigned doesn't make a difference in math, it's only a matter of representation. Numeric literals go from -32767 to 65535 where the range from 32769 to 65535 are in fact the same as -32767 to -1.

neilsf commented 4 years ago

You can also use hex literals, e. g. POKE $D800, 1

mrg-75 commented 4 years ago

Thanks, that makes sense now. I was storing the address in a variable, and printing it to check I'd calculated it right. I saw it got printed as a negative number, and jumped to the wrong conclusion.

neilsf commented 4 years ago

Yeah, that's pretty confusing. I've been planning to implement advanced number to string conversion (something similar to sprintf in C) for a long time. That would make it possible to print the same number as signed or unsigned, dec or hex, whatever. Never had time for it.