Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mixing variable types in a numerical calculation
#1
My program is calculating attribute memory addresses for screen characters. In the interests of only DIMing what I need, I store the screen coordinates as UBYTE since I only need numbers from 0-32 at most.

I have spent a long time to find that the compiler doesn't perform the maths properly when these UBYTE variables are used with larger numbers in a calculation - even though I'm never asking for that UBYTE variable to be made any larger. See this example:
Code:
DIM x,y AS UBYTE
DIM attrMem AS INTEGER
CLS
y=18
x=4
attrMem=22528+y*32+x
POKE attrMem,16 'red paper, black ink

The result should be a red square AT 18,4. However, it is printed in the wrong location on the screen unless I change variables x and y to type INTEGER. Why would this be, since I am not asking for y or x to be expanded beyond their existing values? Is this expected behaviour, or a bug? I haven't really programmed any languages which have needed me to be so specific about number types before, so apologies if this is a silly question.
Reply


Messages In This Thread
Mixing variable types in a numerical calculation - by patters - 01-05-2021, 12:50 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)