I'm trying to round an integer to the nearest multiple of a number.
Say the number is equal to 80
.
Is there a more efficient way to do this than x -= (x % the_number)
?
Here's an example:
#include <stdio.h>
int main(void)
{
int x = 191;
int the_number = 80;
printf("x == %d\n", x);
x -= (x % the_number);
printf("x - (x %% the_number) == %d\n", x);
return 0;
}
Here's another example. It's not a fully working program like the previous is, but it's clearer what I'm trying to do:
#define LIGHT_GRAY 0x0700
#define COLUMNS 80
void pm_putchar(int c, void **ptr)
{
c &= 0xff;
c |= LIGHT_GRAY;
if(c == '\n' | LIGHT_GRAY)
*ptr += COLUMNS;
else if(c == '\r' | LIGHT_GRAY)
*ptr -= (*ptr % COLUMNS);
**(short **)ptr = (short)c;
(*ptr) += 2;
}
In the above example, assume *ptr
is equal to or above the location of the VGA Text Buffer (0x0b8000
).