Quick arithmetic question

Given a number (call it x), how do you (programatically) find the following multiple of another number (255)? For example, if x is 0, the following multiple of 255 is 255, if x is 700 then the following multiple of 255 is 765.

So, how would you calculate it?
Last edited on

1
2
3
4
5
int product = 255;
while (product < x) 
{
  product = product+255;
}
Is there any way to do it without using a loop? This needs to be fast since it's part of a 6502 emulator (granted the 6502 was like 4 MHz and can be emulated in Javascript, but, still).
Maybe [(x/255) + 1] * 255
Or, if you want to also avoid multiplication, x + 255 - x % 255
Thanks, that looks good. I think I'll use m4ster r0shi's one.
Topic archived. No new replies allowed.