# 2022q1 Homework4 (quiz4)
contributed by < [Wallmountain](https://github.com/Wallmountain) >
:::danger
https://hackmd.io/@sysprog/linux2022-homework4 裡頭的描述:「開發紀錄」的 HackMD 網址應該要用「固定網址」(參見 用固定網址發布筆記),也就是如 https://hackmd.io/@itsme/XXXX 的形式。
請修正登記於 https://hackmd.io/@sysprog/linux2022-homework4 裡頭的超連結。
:notes: jserv
:::
## 測驗一
> 延伸[第 3 週測驗題](https://hackmd.io/@sysprog/linux2022-quiz3)的測驗 7,已知輸入必為大於 0 的數值 (即 x > 0),以下程式碼可計算 $⌈log2(x)$⌉,也就是 [ceil](https://man7.org/linux/man-pages/man3/ceil.3.html) 和 [log2](https://man7.org/linux/man-pages/man3/log2.3.html) 的組合並轉型為整數:
```cpp
int ceil_log2(uint32_t x)
{
uint32_t r, shift;
x--;
r = (x > 0xFFFF) << 4;
x >>= r;
shift = (x > 0xFF) << 3;
x >>= shift;
r |= shift;
shift = (x > 0xF) << 2;
x >>= shift;
r |= shift;
shift = (x > 0x3) << 1;
x >>= shift;
return (r | shift | x >> 1) + 1;
}
```