四捨五入到下一個 2 的冪 - Rounding up to next power of 2

問題:

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章