Skip to content

[Bug] reverse_bit function returns incorrect output #12868

@sadmravi

Description

@sadmravi

Repository commit

3f1e376

Python version (python --version)

Python 3.12.8

Dependencies version (pip freeze)

Summery

The reverse_bits function added in PR #4120 appears to return the original number’s binary string instead of its 32-bit bit-reversed form.

Example:

When input is 5, the correct 32-bit reversed output should be:

Input (decimal): 5
Expected output: 10100000000000000000000000000000
Actual output: 00000000000000000000000000000101

This behavior occurs because the reverse_bits function calls get_reverse_bit_string with the already bit-reversed decimal result. Inside get_reverse_bit_string, the number is reversed again, effectively undoing the original reversal. As a result, the final output is incorrect.

Proposed Solution

The fix is to stop calling get_reverse_bit_string() inside reverse_bits() and instead return the reversed decimal value directly. Since get_reverse_bit_string() already handles string formatting, reverse_bits() should now strictly deal with returning the integer result of the bit reversal.

Expected behavior

Input: 5
Expected 32-bit binary: 00000000000000000000000000000101
Expected reversed: 10100000000000000000000000000000
Expected decimal: 2684354560

Actual behavior

Input: 5
Actual : 00000000000000000000000000000101

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions