 # How to add 8 bit or 16 bit unsigned numbers?

Hello. I am trying to make a 6502 assembly emulator for a roblox game, and I’ve gotten pretty far in doing so. Only issue is that in some parts, I absolutely NEED to add unsigned numbers together.

Is there any built-in function for adding 8 bit unsigned numbers together? I’ve been using the bit32 library a lot, but it doesn’t seem to have an adding function.

(For example, 8 bit unsigned math would be like: 0 - 2 = 254)

There is no built in method no

Assuming you’re representing your numbers as normal lua doubles

``````local function uint8_add(x, y)
return (x + y) % 256
end

print(uint8_add(10, -10)) -- 0
print(uint8_add(10, -20)) -- 246
print(uint8_add(250, 10)) -- 4
print(uint8_add(0, -2))   -- 254
``````

Thank you, this is exactly what I was looking for. I assume that I could also replace the 256 with 65536 for 16 bit numbers too, correct?

Yes you can do that. For signed math I would probably add 128, do the unsigned version, then subtract 128