問題:
I want to write a function that returns the nearest next power of 2 number.我想編寫一個返回最接近的 2 的下一個冪的函數。 For example if my input is 789, the output should be 1024. Is there any way of achieving this without using any loops but just using some bitwise operators?例如,如果我的輸入是 789,那麼輸出應該是 1024。有沒有什麼方法可以在不使用任何循環而只使用一些按位運算符的情況下實現這一點?
解決方案:
參考一: https://stackoom.com/question/1xHQ參考二: Rounding up to next power of 2