Find First Non-Zero Bit Position

Intel

Problem Description

Write a SystemVerilog function first_nonzero_bit that accepts a 32-bit unsigned integer and returns the position of the first (lowest) non-zero bit, where position 0 is the LSB.

If the input is 0 (no non-zero bits), return -1.

Examples:

Input (binary)

Expected output

Reason

32'h0000_0001

0

bit[0] is set

32'h0000_0008

3

bit[3] is the first set bit

32'hFFFF_FFF0

4

bits[3:0]=0, bit[4]=1

32'h8000_0000

31

only MSB is set

32'h0000_0000

-1

no bits set

Function signature:

systemverilog

function int first_nonzero_bit(input logic [31:0] val);

SystemVerilog

Code Editor Access

Log in to write, run and submit your solution to this interview question. It's free.

Output

Output goes here...