Skip to content

fix: prevent digit position overflow in OctalToBinary for large octal inputs - #7776

Merged
alxkm merged 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-octal-to-binary-overflow
Nov 24, 2026
Merged

alxkm merged 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-octal-to-binary-overflow

Conversation

@Aditya2375

Copy link
Copy Markdown
Contributor

What: OctalToBinary.convertOctalToBinary returns wrong results for octal inputs with 5 or more digits.

Why: digitPosition is an int and gets multiplied by 1000 for every octal digit. At 10^12 it overflows int (10^12 mod 2^32 = -727379968), so convertOctalToBinary(10000) returned -727379968 instead of 1000000000000.

Change: make digitPosition a long (one line) and add a regression test covering 5- to 7-digit octal inputs. The new test fails on the current implementation and passes with this change; all existing OctalToBinaryTest cases still pass.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

digitPosition grew as an int and overflowed for octal inputs with 5 or more digits, so convertOctalToBinary(10000) returned a negative value instead of 1000000000000. Making it a long keeps the position exact for the full range the method can represent.
Fails on the current implementation because digitPosition overflowed int; passes with the long fix.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.44%. Comparing base (9e216aa) to head (69cb8c3).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7776      +/-   ##
============================================
- Coverage     82.45%   82.44%   -0.01%     
+ Complexity     8512     8509       -3     
============================================
  Files           847      847              
  Lines         26291    26291              
  Branches       5134     5134              
============================================
- Hits          21677    21675       -2     
  Misses         3825     3825              
- Partials        789      791       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Aditya2375
Aditya2375 marked this pull request as ready for review November 24, 2026 05:58

@alxkm alxkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you for the contribution.

@alxkm
alxkm merged commit 54d056d into TheAlgorithms:master Nov 24, 2026
7 checks passed
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.

3 participants