Skip to content

fix: off-by-one error in binary_count_trailing_zeros for zero input #14479

@zendy199x

Description

@zendy199x

Description

"The function returns 0 for a == 0, but mathematically, trailing zeros in binary representation of 0 are undefined (or conventionally treated as infinite). While log2(a & -a) fails for a=0, the current fallback to 0 is inconsistent with the documented behavior: binary_count_trailing_zeros(16) returns 4, but 0 has infinitely many trailing zeros — returning 0 is misleading and could cause bugs in algorithms relying on this (e.g., bit manipulation loops expecting correct trailing zero counts)."

Severity: high
File: bit_manipulation/binary_count_trailing_zeros.py

Expected Behavior

This should be handled properly to prevent runtime errors or degraded reliability.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions