contributed by <56han>
第 7 週測驗題 - 測驗 1
將 2 個 32 位元寬度的整數合併
Question: 為甚麼 x 和 y 都要加 0L,再進行位移?
Answer: 因為 x 和 y 的位元寬是 32 位元 ( uint32_t ) ,左移 32 位會超出它的位元寬,會出現 warning: left shift count >= width of type。long 是 64 位元,左移 32 位在這種情況下是合法的,不會引發警告。
(-1L >> 32) 計算出的值是0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 1111 1111。
static inline uint64_t bit_compound(uint32_t x, uint32_t y)