How to check if a number is a power of two for O(1)

how-to-check-if-a-number-is-a-power-of-two-for-o(1)

Image description

In binary representation, numbers consist of 0 and 1. And it just so happens that all powers of two are 10*.
For example, 2 -> 10, 8 -> 1000, and so on.
When we do a comparison of number & number – 1 == 0, we check 100 & 011, which will always yield 0.

3 → 11. 3 & 2 => 11 & 10 = 1.

Total
1
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
solving-risc-v-kata-locally,-the-not-so-easy-way

Solving RISC-V Kata locally, the not-so-easy way

Next Post
my-google-summer-of-code-2022-–-google-blockly-workspace-multiselect-plugin

My Google Summer of Code 2022 – Google Blockly Workspace MultiSelect Plugin

Related Posts