ROUND? What does it do? in c ++

Can someone explain to me what this does?

#define ROUNDUP(n,width) (((n) + (width) - 1) & ~unsigned((width) - 1))
+3
source share
4 answers

Providing a width equal to even power 2 (so 2,4,8,16,32, etc.), it will return a number equal to or greater than n, which is a multiple of the width and which is the smallest value satisfying these criteria.

So width = 16; 5-> 16, 7-> 16, 15-> 16, 16-> 16, 17-> 32, 18-> 32, etc.

EDIT , , , , , OP, . , , width = 16, n = 15,16,17. , = , ~ = .

+6

n - , 2.

, == 8, n = 5:

(5 + 8 - 1) ~ (7) = 12 ~ 7 = 8

, 5 8. 1 - 8 8. 9 16 16. .. (0 0)

+2

ROUNDUP, , n width, (n + width - 1) & ~unsigned(width - 1).

:)

, , , :

std::string s("WTF");
std::complex<double> c(-11,5);
ROUNDUP(s, c);
+2

He will not work in Cdue unsigned. Here is what is, so far widthlimited to degrees 2:

 n width ROUNDUP(n,width)
----------------
 0   4    0
 1   4    4
 2   4    4
 3   4    4
 4   4    4
 5   4    8
 6   4    8
 7   4    8
 8   4    8
 9   4    12
10   4    12
11   4    12
12   4    12
13   4    16
14   4    16
15   4    16
16   4    16
17   4    20
18   4    20
19   4    20
0
source

Source: https://habr.com/ru/post/1729337/


All Articles