Asked • 04/28/19

Given an integer, how do I find the next largest power of two using bit-twiddling?

If I have a integer number `n`, how can I find the next number `k > n` such that `k = 2^i`, with some `i` element of `N` by bitwise shifting or logic.Example: If I have `n = 123`, how can I find `k = 128`, which is a power of two, and not `124` which is only divisible by two. This should be simple, but it eludes me.

2 Answers By Expert Tutors

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.