Skip to content

refactor: improve bit manipulation helpers docs and validation#310

Open
Ashiish-Agarwal wants to merge 1 commit into
TheAlgorithms:masterfrom
Ashiish-Agarwal:feat-bit-manipulation-cleanup
Open

refactor: improve bit manipulation helpers docs and validation#310
Ashiish-Agarwal wants to merge 1 commit into
TheAlgorithms:masterfrom
Ashiish-Agarwal:feat-bit-manipulation-cleanup

Conversation

@Ashiish-Agarwal

Copy link
Copy Markdown

Summary

This PR improves the bit manipulation helpers in the bit_manipulation module by:

  • Adding clearer JSDoc comments and examples.
  • Adding basic input validation where appropriate.
  • Avoiding mutation of function parameters.
  • Making behavior for invalid inputs explicit.

Changes

  • bit_manipulation/add_binary.ts

    • Added validation to ensure both inputs are non-empty binary strings containing only 0 and 1.
    • Made radix for parseInt explicit.
    • Simplified variable naming and added an example in the JSDoc.
  • bit_manipulation/is_power_of_2.ts

    • Fixed and clarified documentation to match the actual behavior (1 is treated as a power of two).
    • Improved JSDoc (parameter name and return description).
  • bit_manipulation/is_power_of_4.ts

    • Clarified what “power of four” means and documented the n % 3 === 1 property.
    • Added examples in the JSDoc.
  • bit_manipulation/log_two.ts

    • Documented that the function returns floor(log2(n)) for positive integers.
    • Added a RangeError for n <= 0 instead of returning an incorrect value.
    • Avoided mutating the input parameter by using a local variable.

Motivation

These changes make the functions safer to use and easier to understand, especially for people reading the code for learning purposes. The behavior for edge cases is now explicitly documented and enforced.

@Ashiish-Agarwal

Copy link
Copy Markdown
Author

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant