Interview Question · Code Writing
Is This Number a Power of Two
The Question
The interviewer asks
Write a one-line check that an 8-bit unsigned x is a power of two.
You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
Use x != 0 && (x & (x - 1)) == 0. A power of two has exactly one bit set. Subtracting 1 clears that bit and sets all the bits below it, so the AND is zero. The x != 0 part matters, because zero also gives a zero AND. In hardware it is a subtractor, an AND and two zero checks.
Read the diagram as text
- x: the input word
- x - 1: a subtractor
- x & (x - 1): clears the lowest 1
- Zero check: is the AND 0?
- Nonzero check: is x != 0?
- is_pow2: both checks true
- x → x - 1
- x - 1 → x & (x - 1)
- x → x & (x - 1)
- x & (x - 1) → Zero check
- x → Nonzero check
- Zero check → is_pow2
- Nonzero check → is_pow2
Without the nonzero check, x = 0 would pass, because 0 AND anything is 0.
| x | x - 1 | x & (x - 1) | Power of two? |
|---|---|---|---|
40 = 0010_1000 | 39 = 0010_0111 | 0010_0000 | No |
32 = 0010_0000 | 31 = 0001_1111 | 0000_0000 | Yes |
Every value from 0 to 255.
accepted: 1,2,4,8,16,32,64,128, 8 values in 0..255 accepted PASS: exactly the eight powers of twoSelf-Check
Tick each key point you covered in your answer.
What Interviewers Listen For (Fresher)
A good fresher answer explains the one-bit-set idea and shows x - 1 on an example such as 40. It adds the zero check without being asked.
What Interviewers Listen For (Junior)
A good junior answer asks about signed inputs and the width. It offers $countones(x) == 1 as a readable alternative and compares the hardware.
Cross-Questions
Where the interviewer goes next. Open a node to practise that follow-up.
WhyWhat IfBreak ItCompare
- Is This Number a Power of Two
-
Break ItThe Signed Input That Passed as a Power of TwoL0 FresherL1 Junior
Asked if the input could be signed
The input is now an 8-bit signed value, kept in 8 bits. Which input breaks the check?
systemverilog logic signed [7:0] m; m = x - 8'sd1; return x != 0 && (x & m) == 0;2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
-128breaks it. Its pattern is1000_0000, one bit set. In 8 bits,-128 - 1wraps to127, so the AND is zero. Checkx > 0instead ofx != 0.Self-Check
Tick each key point you covered in your answer.
Missed Corner
"The trick works for any integer."
It tests for one bit set. In two's complement the most negative value also has one bit set. The twin reports
-128as a power of two.Run It
Every 8-bit signed value through the unsigned-style check.
RUNIcarus Verilog · in your browserExpected:is_pow2(-128) = 1, 8 values acceptedFAIL: -128 is 8'b1000_0000, one bit set, so the unsigned test calls it a power of twoSee the Fix
module tb; // An 8-bit signed datapath: x - 1 is kept in 8 bits, as a register would keep it. function automatic bit is_pow2(input logic signed [7:0] x); logic signed [7:0] m; m = x - 8'sd1; return x > 0 && (x & m) == 0; endfunction int n = 0; initial begin for (int v = -128; v < 128; v++) if (is_pow2(v)) n++; $display("is_pow2(-128) = %0d, %0d values accepted", is_pow2(-128), n); if (!is_pow2(-128) && n == 7) $display("PASS: x > 0 keeps out -128; 1 to 64 are the seven powers of two"); end endmodulePrints:
PASS: x > 0 keeps out -128; 1 to 64 are the seven powers of two -
CompareCounting the Ones in a WordL0 FresherL1 Junior
Count the set bits in a byte. Compare a bit-by-bit loop with the
x & (x - 1)trick.2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
The loop looks at all 8 bits every time. The trick clears one 1 per step, so it takes as many steps as there are ones. In software, that saves time for sparse words. In hardware, the fixed 8-step loop unrolls to a popcount tree. The clear-lowest loop must be bounded to 8 steps before synthesis; it then gives similar logic, not fewer steps.
RUNIcarus Verilog · in your browserExpected:x=00 loop: 0 ones in 8 steps clear-lowest: 0 ones in 0 stepsx=b4 loop: 4 ones in 8 steps clear-lowest: 4 ones in 4 stepsx=ff loop: 8 ones in 8 steps clear-lowest: 8 ones in 8 stepsSelf-Check
Tick each key point you covered in your answer.
Misconception
"The
whileversion is faster in hardware too."A synthesizable loop must have a fixed bound, and synthesis unrolls it into one cycle of logic. The step count is a software idea.
Source Sutherland, RTL Modeling with SystemVerilog (2017) ch. 6.3.1.1 Static loops versus data-dependent loops
-
WhyWhy x & (x - 1) Clears the Lowest OneL0 Fresher
Why does
x & (x - 1)clear the lowest set bit?2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
Subtracting 1 borrows from the lowest 1. That bit becomes 0 and every 0 below it becomes 1. Bits above it do not change. ANDing with
xkeeps the upper bits and zeroes the rest.RUNIcarus Verilog · in your browserExpected:x = 00101000x - 1 = 00100111x & (x-1) = 00100000Self-Check
Tick each key point you covered in your answer.
-
What IfBinary to Gray Code and BackL0 FresherL1 Junior
What if the next puzzle is Gray code? Convert binary to Gray and back.
2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
Binary to Gray is
g = b ^ (b >> 1). The way back is not the same formula. Each binary bit is the XOR of all the Gray bits at and above it. Consecutive Gray values differ in one bit, including the wrap from 15 to 0.RUNIcarus Verilog · in your browserExpected:gray of 0..7: 0000 0001 0011 0010 0110 0111 0101 010016 of 16 steps change one bit; 0 of 16 round trips wrongPASS: binary to Gray and back for all 16 valuesSelf-Check
Tick each key point you covered in your answer.
Misconception
Uses
g ^ (g >> 1)to convert back.Each binary bit depends on every Gray bit above it, not only its neighbour. The twin gets most round trips wrong.
Run It
The round trip with the forward formula used backwards.
RUNIcarus Verilog · in your browserExpected:FAIL: g ^ (g >> 1) does not undo the Gray code; the inverse needs an XOR of all higher bitsSee the Fix
module tb; function automatic logic [3:0] to_gray(input logic [3:0] b); return b ^ (b >> 1); endfunction function automatic logic [3:0] from_gray(input logic [3:0] g); logic [3:0] b; b[3] = g[3]; for (int i = 2; i >= 0; i--) b[i] = b[i+1] ^ g[i]; // each bit is the XOR of all Gray bits above it return b; endfunction int bad = 0, onebit = 0; logic [3:0] diff; initial begin for (int v = 0; v < 16; v++) begin if (from_gray(to_gray(v)) !== v[3:0]) bad++; diff = to_gray(v[3:0]) ^ to_gray(v[3:0] + 4'd1); // includes 15 -> 0 if (diff[0] + diff[1] + diff[2] + diff[3] == 1) onebit++; end $display("gray of 0..7: %b %b %b %b %b %b %b %b", to_gray(0), to_gray(1), to_gray(2), to_gray(3), to_gray(4), to_gray(5), to_gray(6), to_gray(7)); $display("%0d of 16 steps change one bit; %0d of 16 round trips wrong", onebit, bad); if (bad == 0) $display("PASS: binary to Gray and back for all 16 values"); end endmodulePrints:
PASS: binary to Gray and back for all 16 values
What People Get Wrong
Common wrong answers and why they fail. Run It shows the wrong belief failing on a real simulator.
Missed Corner
Writes (x & (x - 1)) == 0 and stops.
For x = 0 the AND is also zero, so zero passes. The twin accepts nine values instead of eight.
Run It
The same check without x != 0.
9 values in 0..255 accepted FAIL: (x & (x - 1)) == 0 is also true for x = 0, which is not a power of twoSee the Fix
module tb;
function automatic bit is_pow2(input logic [7:0] x);
return x != 0 && (x & (x - 1)) == 0;
endfunction
string list = "";
int n = 0;
initial begin
for (int v = 0; v < 256; v++)
if (is_pow2(v)) begin n++; list = {list, $sformatf("%0d,", v)}; end
$display("accepted: %s", list);
$display("%0d values in 0..255 accepted", n);
if (n == 8) $display("PASS: exactly the eight powers of two");
end
endmodule
Prints: PASS: exactly the eight powers of two
Wrong Altitude
Loops over the bits and counts them in a one-line question.
It works, but the interviewer asked for the bit trick. Give it first, then mention the loop as the slower, clearer version.