Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Check for a power of two

Hardware interview practice

Check for a power of two

EasyBit ManipulationSystemVerilog

Return 1 when an unsigned integer is a non-zero power of two.

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
Reviewed example

Work through one case

Input
value = 64 (0b0100_0000)
Expected output
1 (power of two)

A nonzero power of two has one set bit, so value & (value - 1) is zero.

What to cover

Requirements

  1. Reject zero.
  2. Use a constant-time bit operation.
  3. Do not loop over every bit.
Continue practicing

Related questions

Bit ManipulationGet, set, clear, and toggle one bit→Bit ManipulationClear the lowest set bit→
asic.fyi · Learn silicon end to end.info@asic.fyi