Question about round_up macro

前端 未结 5 750
暗喜
暗喜 2021-02-07 12:55
#define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))

With the above macro, could someone please help me on understanding the \"(s)-1\" part, why\'s t

5条回答
  •  离开以前
    2021-02-07 13:38

    The & makes it so.. well ok, lets take some binary numbers.

    (with 1000 being page size)
    PAGE_ROUND_UP(01101b)=
    01101b+1000b-1b & ~(1000b-1b) =
    01101b+111b & ~(111b) =
    01101b+111b & ...11000b = (the ... means 1's continuing for size of ULONG)
    10100b & 11000b=
    10000b
    

    So, as you can see(hopefully) This rounds up by adding PAGE_SIZE to x and then ANDing so it cancels out the bottom bits of PAGE_SIZE that are not set

提交回复
热议问题