Liam0205 / liam0205.github.io

Deployment of my weblog.
https://liam0205.github.io
35 stars 5 forks source link

寻找不小于给定整数的最小的 2 的幂方 | 始终 #285

Open Liam0205 opened 5 years ago

Liam0205 commented 5 years ago

https://liam.page/2019/02/14/find-smallest-power-of-2-that-is-not-less-than-a-given-integer/

这是一篇简单的记录。 最近在写 YTL 的过程中遇到这样一个子问题:需要定义一个函数 uint32_t func(uint32_t num),返回不小于 num 的最小的 2 的幂方。例如 123451 == func(1);2 == func(2);4 == func(3);4 == func(4);// …… 一开始我想了个用位运算的奇技淫巧,自我感觉还不错: