-
Notifications
You must be signed in to change notification settings - Fork 20.8k
Add Power of Four Check using bit manipulation #7065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DenizAltunkapan
merged 9 commits into
TheAlgorithms:master
from
krishna-medapati:feature/power-of-four-check
Nov 15, 2025
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e15105b
Add Power of Four Check using bit manipulation
krishna-medapati 4504025
Fix code formatting in PowerOfFourTest
krishna-medapati 5fb478e
Merge branch 'master' into feature/power-of-four-check
krishna-medapati b0264b8
Move PowerOfFour classes to maths package
krishna-medapati dba445f
Fix package declaration in PowerOfFourTest
krishna-medapati ae5f3a2
Fix code formatting in PowerOfFourTest
krishna-medapati 213d5c0
Remove redundant import from PowerOfFourTest
krishna-medapati 3dd3e92
Merge branch 'master' into feature/power-of-four-check
krishna-medapati f292e81
Remove unrelated file
krishna-medapati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.thealgorithms.maths; | ||
|
|
||
| /** | ||
| * Utility class for checking if a number is a power of four. | ||
| * A power of four is a number that can be expressed as 4^n where n is a non-negative integer. | ||
| * This class provides a method to determine if a given integer is a power of four using bit manipulation. | ||
| * | ||
| * @author krishna-medapati (https://github.com/krishna-medapati) | ||
| */ | ||
| public final class PowerOfFour { | ||
| private PowerOfFour() { | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the given integer is a power of four. | ||
| * | ||
| * A number is considered a power of four if: | ||
| * 1. It is greater than zero | ||
| * 2. It has exactly one '1' bit in its binary representation (power of two) | ||
| * 3. The '1' bit is at an even position (0, 2, 4, 6, ...) | ||
| * | ||
| * The method uses the mask 0x55555555 (binary: 01010101010101010101010101010101) | ||
| * to check if the set bit is at an even position. | ||
| * | ||
| * @param number the integer to check | ||
| * @return true if the number is a power of four, false otherwise | ||
| */ | ||
| public static boolean isPowerOfFour(int number) { | ||
| if (number <= 0) { | ||
| return false; | ||
| } | ||
| boolean isPowerOfTwo = (number & (number - 1)) == 0; | ||
| boolean hasEvenBitPosition = (number & 0x55555555) != 0; | ||
| return isPowerOfTwo && hasEvenBitPosition; | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
src/test/java/com/thealgorithms/maths/PowerOfFourTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.thealgorithms.maths; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class PowerOfFourTest { | ||
|
|
||
| @Test | ||
| void testPowersOfFour() { | ||
| assertTrue(PowerOfFour.isPowerOfFour(1)); | ||
| assertTrue(PowerOfFour.isPowerOfFour(4)); | ||
| assertTrue(PowerOfFour.isPowerOfFour(16)); | ||
| assertTrue(PowerOfFour.isPowerOfFour(64)); | ||
| assertTrue(PowerOfFour.isPowerOfFour(256)); | ||
| assertTrue(PowerOfFour.isPowerOfFour(1024)); | ||
| } | ||
|
|
||
| @Test | ||
| void testNonPowersOfFour() { | ||
| assertFalse(PowerOfFour.isPowerOfFour(2)); | ||
| assertFalse(PowerOfFour.isPowerOfFour(3)); | ||
| assertFalse(PowerOfFour.isPowerOfFour(5)); | ||
| assertFalse(PowerOfFour.isPowerOfFour(8)); | ||
| assertFalse(PowerOfFour.isPowerOfFour(15)); | ||
| assertFalse(PowerOfFour.isPowerOfFour(32)); | ||
| } | ||
|
|
||
| @Test | ||
| void testEdgeCases() { | ||
| assertFalse(PowerOfFour.isPowerOfFour(0)); | ||
| assertFalse(PowerOfFour.isPowerOfFour(-1)); | ||
| assertFalse(PowerOfFour.isPowerOfFour(-4)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.