Skip to content

Conversation

@krishna-medapati
Copy link
Contributor

Fixes #6940

Description

Added a utility class to check if a number is a power of four using efficient bit manipulation.

Implementation Details

  • Uses bit manipulation to check if a number is a power of four in O(1) time
  • Verifies the number is a power of two: (n & (n-1)) == 0
  • Checks if the set bit is at an even position using mask 0x55555555
  • Follows the existing code style and structure of the repository

Changes Made

  • Created PowerOfFour.java with isPowerOfFour() method
  • Added comprehensive unit tests in PowerOfFourTest.java
  • Included JavaDoc documentation

Testing

All test cases pass:

  • Powers of four: 1, 4, 16, 64, 256, 1024
  • Non-powers of four: 2, 3, 5, 8, 15, 32
  • Edge cases: 0, -1, -4

- Implements isPowerOfFour method using bit manipulation
- Checks if number is power of two and has bit at even position
- Includes comprehensive unit tests
- Fixes TheAlgorithms#6940
@codecov-commenter
Copy link

codecov-commenter commented Nov 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.35%. Comparing base (98eecb9) to head (f292e81).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7065      +/-   ##
============================================
- Coverage     78.36%   78.35%   -0.02%     
- Complexity     6684     6689       +5     
============================================
  Files           755      756       +1     
  Lines         22203    22208       +5     
  Branches       4360     4363       +3     
============================================
+ Hits          17400    17401       +1     
- Misses         4105     4107       +2     
- Partials        698      700       +2     

☔ View full report in Codecov by Sentry.
📢 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.

@krishna-medapati
Copy link
Contributor Author

krishna-medapati commented Nov 14, 2025

@DenizAltunkapan could please you look into the pr sir!i have already once contributed one pr .

@DenizAltunkapan
Copy link
Member

DenizAltunkapan commented Nov 15, 2025

@krishna-medapati could you please do me the favor and assure that your implemented classes are in the same package as the existing power of two class? https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PowerOfTwoOrNot.java

@krishna-medapati
Copy link
Contributor Author

@krishna-medapati could you please do me the favor and assure that your implemented classes are in the same package as the existing power of two class? https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PowerOfTwoOrNot.java

@DenizAltunkapan Done! I've successfully moved both PowerOfFour classes to the com.thealgorithms.maths package to match the PowerOfTwoOrNot class location. All tests are passing and all checks are green. Ready for final review. Thank you .

@DenizAltunkapan DenizAltunkapan merged commit 3979e82 into TheAlgorithms:master Nov 15, 2025
6 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.

[FEATURE REQUEST] Power of Four Check (Bit Manipulation)

3 participants