-2

I'm looking for a way to round a number to nearest number that can be divided by 4 without remainder

3 Answers3

2
num = std::round(num / 4.0) * 4.0;
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
2

Here is some pseudo code. Probably not the most efficient way, but...

if num mod 4 == 0 then you are good
if num mod 4 == 1 then subtract 1
if num mod 4 == 2 then you decide (subtract/add 2)
if num mod 4 == 3 then add 1
Jake Smith
  • 2,332
  • 1
  • 30
  • 68
-3

Use the following MACRO:

#define ALIGN4(len) (((len) + 3) & ~3) // round up to 4 items