From a2225d095c549a3e09623c7faf9c5897a2510e1d Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi Date: Mon, 15 Dec 2025 19:38:29 -0800 Subject: [PATCH 1/4] Add find previous power of two implementation --- DIRECTORY.md | 19 +- .../find_previous_power_of_two.rs | 172 ++++++++++++++++++ src/bit_manipulation/mod.rs | 2 + 3 files changed, 184 insertions(+), 9 deletions(-) create mode 100644 src/bit_manipulation/find_previous_power_of_two.rs diff --git a/DIRECTORY.md b/DIRECTORY.md index d8d527a02e3..af47f388d1c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -21,6 +21,7 @@ * [Counting Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/counting_bits.rs) * [Highest Set Bit](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/highest_set_bit.rs) * [N Bits Gray Code](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/n_bits_gray_code.rs) + * [Previous Power of Two](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/find_previous_power_of_two.rs) * [Reverse Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/reverse_bits.rs) * [Sum of Two Integers](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/sum_of_two_integers.rs) * [Swap Odd and Even Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/swap_odd_even_bits.rs) @@ -195,7 +196,7 @@ * [Gradient Descent](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/optimization/gradient_descent.rs) * [Momentum](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/optimization/momentum.rs) * Math - * [Abs](https://github.com/TheAlgorithms/Rust/blob/master/src/math/abs.rs) + * [Absolute](https://github.com/TheAlgorithms/Rust/blob/master/src/math/abs.rs) * [Aliquot Sum](https://github.com/TheAlgorithms/Rust/blob/master/src/math/aliquot_sum.rs) * [Amicable Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/amicable_numbers.rs) * [Area Of Polygon](https://github.com/TheAlgorithms/Rust/blob/master/src/math/area_of_polygon.rs) @@ -259,8 +260,8 @@ * [Prime Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/prime_numbers.rs) * [Quadratic Residue](https://github.com/TheAlgorithms/Rust/blob/master/src/math/quadratic_residue.rs) * [Random](https://github.com/TheAlgorithms/Rust/blob/master/src/math/random.rs) - * [Relu](https://github.com/TheAlgorithms/Rust/blob/master/src/math/relu.rs) - * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sieve_of_eratosthenes.rs) + * [ReLU](https://github.com/TheAlgorithms/Rust/blob/master/src/math/relu.rs) + * [Sieve of Eratosthenes](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sieve_of_eratosthenes.rs) * [Sigmoid](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sigmoid.rs) * [Signum](https://github.com/TheAlgorithms/Rust/blob/master/src/math/signum.rs) * [Simpsons Integration](https://github.com/TheAlgorithms/Rust/blob/master/src/math/simpsons_integration.rs) @@ -268,9 +269,9 @@ * [Sprague Grundy Theorem](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sprague_grundy_theorem.rs) * [Square Pyramidal Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/square_pyramidal_numbers.rs) * [Square Root](https://github.com/TheAlgorithms/Rust/blob/master/src/math/square_root.rs) - * [Sum Of Digits](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_digits.rs) - * [Sum Of Geometric Progression](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_geometric_progression.rs) - * [Sum Of Harmonic Series](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_harmonic_series.rs) + * [Sum of Digits](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_digits.rs) + * [Sum of Geometric Progression](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_geometric_progression.rs) + * [Sum of Harmonic Series](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_harmonic_series.rs) * [Sylvester Sequence](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sylvester_sequence.rs) * [Tanh](https://github.com/TheAlgorithms/Rust/blob/master/src/math/tanh.rs) * [Trapezoidal Integration](https://github.com/TheAlgorithms/Rust/blob/master/src/math/trapezoidal_integration.rs) @@ -285,7 +286,7 @@ * Number Theory * [Compute Totient](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/compute_totient.rs) * [Euler Totient](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/euler_totient.rs) - * [Kth Factor](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/kth_factor.rs) + * [K-th Factor](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/kth_factor.rs) * Searching * [Binary Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search.rs) * [Binary Search Recursive](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search_recursive.rs) @@ -293,8 +294,8 @@ * [Fibonacci Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/fibonacci_search.rs) * [Interpolation Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/interpolation_search.rs) * [Jump Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/jump_search.rs) - * [Kth Smallest](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/kth_smallest.rs) - * [Kth Smallest Heap](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/kth_smallest_heap.rs) + * [K-th Smallest](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/kth_smallest.rs) + * [K-th Smallest Heap](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/kth_smallest_heap.rs) * [Linear Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/linear_search.rs) * [Moore Voting](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/moore_voting.rs) * [Quick Select](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/quick_select.rs) diff --git a/src/bit_manipulation/find_previous_power_of_two.rs b/src/bit_manipulation/find_previous_power_of_two.rs new file mode 100644 index 00000000000..a6d730d3fde --- /dev/null +++ b/src/bit_manipulation/find_previous_power_of_two.rs @@ -0,0 +1,172 @@ +//! Previous Power of Two +//! +//! This module provides a function to find the largest power of two that is less than +//! or equal to a given non-negative integer. +//! +//! # Algorithm +//! +//! The algorithm works by repeatedly left-shifting (doubling) a power value starting +//! from 1 until it exceeds the input number, then returning the previous power (by +//! right-shifting once). +//! +//! For more information: + +/// Finds the largest power of two that is less than or equal to a given integer. +/// +/// The function uses bit shifting to efficiently find the power of two. It starts +/// with 1 and keeps doubling (left shift) until it exceeds the input, then returns +/// the previous value (right shift). +/// +/// # Arguments +/// +/// * `number` - A non-negative integer +/// +/// # Returns +/// +/// A `Result` containing: +/// - `Ok(u32)` - The largest power of two ≤ the input number +/// - `Err(String)` - An error message if the input is negative +/// +/// # Examples +/// +/// ``` +/// use the_algorithms_rust::bit_manipulation::find_previous_power_of_two; +/// +/// assert_eq!(find_previous_power_of_two(0).unwrap(), 0); +/// assert_eq!(find_previous_power_of_two(1).unwrap(), 1); +/// assert_eq!(find_previous_power_of_two(2).unwrap(), 2); +/// assert_eq!(find_previous_power_of_two(3).unwrap(), 2); +/// assert_eq!(find_previous_power_of_two(4).unwrap(), 4); +/// assert_eq!(find_previous_power_of_two(5).unwrap(), 4); +/// assert_eq!(find_previous_power_of_two(8).unwrap(), 8); +/// assert_eq!(find_previous_power_of_two(15).unwrap(), 8); +/// assert_eq!(find_previous_power_of_two(16).unwrap(), 16); +/// assert_eq!(find_previous_power_of_two(17).unwrap(), 16); +/// +/// // Negative numbers return an error +/// assert!(find_previous_power_of_two(-5).is_err()); +/// ``` +/// +/// # Errors +/// +/// Returns an error if the input number is negative. +pub fn find_previous_power_of_two(number: i32) -> Result { + if number < 0 { + return Err("Input must be a non-negative integer".to_string()); + } + + let number = number as u32; + + if number == 0 { + return Ok(0); + } + + let mut power = 1u32; + while power <= number { + power <<= 1; // Equivalent to multiplying by 2 + } + + Ok(if number > 1 { power >> 1 } else { 1 }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_zero() { + assert_eq!(find_previous_power_of_two(0).unwrap(), 0); + } + + #[test] + fn test_one() { + assert_eq!(find_previous_power_of_two(1).unwrap(), 1); + } + + #[test] + fn test_powers_of_two() { + assert_eq!(find_previous_power_of_two(2).unwrap(), 2); + assert_eq!(find_previous_power_of_two(4).unwrap(), 4); + assert_eq!(find_previous_power_of_two(8).unwrap(), 8); + assert_eq!(find_previous_power_of_two(16).unwrap(), 16); + assert_eq!(find_previous_power_of_two(32).unwrap(), 32); + assert_eq!(find_previous_power_of_two(64).unwrap(), 64); + assert_eq!(find_previous_power_of_two(128).unwrap(), 128); + assert_eq!(find_previous_power_of_two(256).unwrap(), 256); + assert_eq!(find_previous_power_of_two(512).unwrap(), 512); + assert_eq!(find_previous_power_of_two(1024).unwrap(), 1024); + } + + #[test] + fn test_numbers_between_powers() { + // Between 2 and 4 + assert_eq!(find_previous_power_of_two(3).unwrap(), 2); + + // Between 4 and 8 + assert_eq!(find_previous_power_of_two(5).unwrap(), 4); + assert_eq!(find_previous_power_of_two(6).unwrap(), 4); + assert_eq!(find_previous_power_of_two(7).unwrap(), 4); + + // Between 8 and 16 + assert_eq!(find_previous_power_of_two(9).unwrap(), 8); + assert_eq!(find_previous_power_of_two(10).unwrap(), 8); + assert_eq!(find_previous_power_of_two(11).unwrap(), 8); + assert_eq!(find_previous_power_of_two(12).unwrap(), 8); + assert_eq!(find_previous_power_of_two(13).unwrap(), 8); + assert_eq!(find_previous_power_of_two(14).unwrap(), 8); + assert_eq!(find_previous_power_of_two(15).unwrap(), 8); + + // Between 16 and 32 + assert_eq!(find_previous_power_of_two(17).unwrap(), 16); + assert_eq!(find_previous_power_of_two(20).unwrap(), 16); + assert_eq!(find_previous_power_of_two(31).unwrap(), 16); + } + + #[test] + fn test_range_0_to_17() { + // Test the exact output from the Python docstring + let expected = vec![0, 1, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 16, 16]; + let results: Vec = (0..18) + .map(|i| find_previous_power_of_two(i).unwrap()) + .collect(); + assert_eq!(results, expected); + } + + #[test] + fn test_large_numbers() { + assert_eq!(find_previous_power_of_two(100).unwrap(), 64); + assert_eq!(find_previous_power_of_two(500).unwrap(), 256); + assert_eq!(find_previous_power_of_two(1000).unwrap(), 512); + assert_eq!(find_previous_power_of_two(2000).unwrap(), 1024); + assert_eq!(find_previous_power_of_two(10000).unwrap(), 8192); + } + + #[test] + fn test_max_safe_values() { + assert_eq!(find_previous_power_of_two(1023).unwrap(), 512); + assert_eq!(find_previous_power_of_two(2047).unwrap(), 1024); + assert_eq!(find_previous_power_of_two(4095).unwrap(), 2048); + } + + #[test] + fn test_negative_number_returns_error() { + let result = find_previous_power_of_two(-1); + assert!(result.is_err()); + assert_eq!(result.unwrap_err(), "Input must be a non-negative integer"); + } + + #[test] + fn test_negative_numbers_return_errors() { + assert!(find_previous_power_of_two(-5).is_err()); + assert!(find_previous_power_of_two(-10).is_err()); + assert!(find_previous_power_of_two(-100).is_err()); + } + + #[test] + fn test_edge_cases() { + // One less than powers of two + assert_eq!(find_previous_power_of_two(127).unwrap(), 64); + assert_eq!(find_previous_power_of_two(255).unwrap(), 128); + assert_eq!(find_previous_power_of_two(511).unwrap(), 256); + } +} diff --git a/src/bit_manipulation/mod.rs b/src/bit_manipulation/mod.rs index d2405375483..13f649207c2 100644 --- a/src/bit_manipulation/mod.rs +++ b/src/bit_manipulation/mod.rs @@ -1,5 +1,6 @@ mod binary_coded_decimal; mod counting_bits; +mod find_previous_power_of_two; mod highest_set_bit; mod n_bits_gray_code; mod reverse_bits; @@ -9,6 +10,7 @@ mod twos_complement; pub use self::binary_coded_decimal::binary_coded_decimal; pub use self::counting_bits::count_set_bits; +pub use self::find_previous_power_of_two::find_previous_power_of_two; pub use self::highest_set_bit::find_highest_set_bit; pub use self::n_bits_gray_code::generate_gray_code; pub use self::reverse_bits::reverse_bits; From ec961466c75b2266aa94786e266e10d4495896f6 Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi Date: Mon, 15 Dec 2025 19:55:25 -0800 Subject: [PATCH 2/4] Add is power of two implementation --- DIRECTORY.md | 1 + src/bit_manipulation/is_power_of_two.rs | 242 ++++++++++++++++++++++++ src/bit_manipulation/mod.rs | 2 + 3 files changed, 245 insertions(+) create mode 100644 src/bit_manipulation/is_power_of_two.rs diff --git a/DIRECTORY.md b/DIRECTORY.md index af47f388d1c..96d903e1b80 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -20,6 +20,7 @@ * [Binary Coded Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/binary_coded_decimal.rs) * [Counting Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/counting_bits.rs) * [Highest Set Bit](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/highest_set_bit.rs) + * [Highest Set Bit](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/is_power_of_two.rs) * [N Bits Gray Code](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/n_bits_gray_code.rs) * [Previous Power of Two](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/find_previous_power_of_two.rs) * [Reverse Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/reverse_bits.rs) diff --git a/src/bit_manipulation/is_power_of_two.rs b/src/bit_manipulation/is_power_of_two.rs new file mode 100644 index 00000000000..4f8435d41b0 --- /dev/null +++ b/src/bit_manipulation/is_power_of_two.rs @@ -0,0 +1,242 @@ +//! Power of Two Check +//! +//! This module provides a function to determine if a given positive integer is a power of two +//! using efficient bit manipulation. +//! +//! # Algorithm +//! +//! The algorithm uses the property that powers of two have exactly one bit set in their +//! binary representation. When we subtract 1 from a power of two, all bits after the single +//! set bit become 1, and the set bit becomes 0: +//! +//! ```text +//! n = 0..100..00 (power of 2) +//! n - 1 = 0..011..11 +//! n & (n - 1) = 0 (no intersections) +//! ``` +//! +//! For example: +//! - 8 in binary: 1000 +//! - 7 in binary: 0111 +//! - 8 & 7 = 0000 = 0 ✓ +//! +//! Author: Alexander Pantyukhin +//! Date: November 1, 2022 + +/// Determines if a given number is a power of two. +/// +/// This function uses bit manipulation to efficiently check if a number is a power of two. +/// A number is a power of two if it has exactly one bit set in its binary representation. +/// The check `number & (number - 1) == 0` leverages this property. +/// +/// # Arguments +/// +/// * `number` - An integer to check (must be non-negative) +/// +/// # Returns +/// +/// A `Result` containing: +/// - `Ok(true)` - If the number is a power of two (including 0 and 1) +/// - `Ok(false)` - If the number is not a power of two +/// - `Err(String)` - If the number is negative +/// +/// # Examples +/// +/// ``` +/// use the_algorithms_rust::bit_manipulation::is_power_of_two; +/// +/// assert_eq!(is_power_of_two(0).unwrap(), true); +/// assert_eq!(is_power_of_two(1).unwrap(), true); +/// assert_eq!(is_power_of_two(2).unwrap(), true); +/// assert_eq!(is_power_of_two(4).unwrap(), true); +/// assert_eq!(is_power_of_two(8).unwrap(), true); +/// assert_eq!(is_power_of_two(16).unwrap(), true); +/// +/// assert_eq!(is_power_of_two(3).unwrap(), false); +/// assert_eq!(is_power_of_two(6).unwrap(), false); +/// assert_eq!(is_power_of_two(17).unwrap(), false); +/// +/// // Negative numbers return an error +/// assert!(is_power_of_two(-1).is_err()); +/// ``` +/// +/// # Errors +/// +/// Returns an error if the input number is negative. +/// +/// # Time Complexity +/// +/// O(1) - The function performs a constant number of operations regardless of input size. +pub fn is_power_of_two(number: i32) -> Result { + if number < 0 { + return Err("number must not be negative".to_string()); + } + + // Convert to u32 for safe bit operations + let num = number as u32; + + // Check if number & (number - 1) == 0 + // For powers of 2, this will always be true + Ok(num & num.wrapping_sub(1) == 0) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_zero() { + // 0 is considered a power of 2 by the algorithm (2^(-∞) interpretation) + assert_eq!(is_power_of_two(0).unwrap(), true); + } + + #[test] + fn test_one() { + // 1 = 2^0 + assert_eq!(is_power_of_two(1).unwrap(), true); + } + + #[test] + fn test_powers_of_two() { + assert_eq!(is_power_of_two(2).unwrap(), true); // 2^1 + assert_eq!(is_power_of_two(4).unwrap(), true); // 2^2 + assert_eq!(is_power_of_two(8).unwrap(), true); // 2^3 + assert_eq!(is_power_of_two(16).unwrap(), true); // 2^4 + assert_eq!(is_power_of_two(32).unwrap(), true); // 2^5 + assert_eq!(is_power_of_two(64).unwrap(), true); // 2^6 + assert_eq!(is_power_of_two(128).unwrap(), true); // 2^7 + assert_eq!(is_power_of_two(256).unwrap(), true); // 2^8 + assert_eq!(is_power_of_two(512).unwrap(), true); // 2^9 + assert_eq!(is_power_of_two(1024).unwrap(), true); // 2^10 + assert_eq!(is_power_of_two(2048).unwrap(), true); // 2^11 + assert_eq!(is_power_of_two(4096).unwrap(), true); // 2^12 + assert_eq!(is_power_of_two(8192).unwrap(), true); // 2^13 + assert_eq!(is_power_of_two(16384).unwrap(), true); // 2^14 + assert_eq!(is_power_of_two(32768).unwrap(), true); // 2^15 + assert_eq!(is_power_of_two(65536).unwrap(), true); // 2^16 + } + + #[test] + fn test_non_powers_of_two() { + assert_eq!(is_power_of_two(3).unwrap(), false); + assert_eq!(is_power_of_two(5).unwrap(), false); + assert_eq!(is_power_of_two(6).unwrap(), false); + assert_eq!(is_power_of_two(7).unwrap(), false); + assert_eq!(is_power_of_two(9).unwrap(), false); + assert_eq!(is_power_of_two(10).unwrap(), false); + assert_eq!(is_power_of_two(11).unwrap(), false); + assert_eq!(is_power_of_two(12).unwrap(), false); + assert_eq!(is_power_of_two(13).unwrap(), false); + assert_eq!(is_power_of_two(14).unwrap(), false); + assert_eq!(is_power_of_two(15).unwrap(), false); + assert_eq!(is_power_of_two(17).unwrap(), false); + assert_eq!(is_power_of_two(18).unwrap(), false); + } + + #[test] + fn test_specific_non_powers() { + assert_eq!(is_power_of_two(6).unwrap(), false); + assert_eq!(is_power_of_two(17).unwrap(), false); + assert_eq!(is_power_of_two(100).unwrap(), false); + assert_eq!(is_power_of_two(1000).unwrap(), false); + } + + #[test] + fn test_large_powers_of_two() { + assert_eq!(is_power_of_two(131072).unwrap(), true); // 2^17 + assert_eq!(is_power_of_two(262144).unwrap(), true); // 2^18 + assert_eq!(is_power_of_two(524288).unwrap(), true); // 2^19 + assert_eq!(is_power_of_two(1048576).unwrap(), true); // 2^20 + } + + #[test] + fn test_numbers_near_powers_of_two() { + // One less than powers of 2 + assert_eq!(is_power_of_two(3).unwrap(), false); // 2^2 - 1 + assert_eq!(is_power_of_two(7).unwrap(), false); // 2^3 - 1 + assert_eq!(is_power_of_two(15).unwrap(), false); // 2^4 - 1 + assert_eq!(is_power_of_two(31).unwrap(), false); // 2^5 - 1 + assert_eq!(is_power_of_two(63).unwrap(), false); // 2^6 - 1 + assert_eq!(is_power_of_two(127).unwrap(), false); // 2^7 - 1 + assert_eq!(is_power_of_two(255).unwrap(), false); // 2^8 - 1 + + // One more than powers of 2 + assert_eq!(is_power_of_two(3).unwrap(), false); // 2^1 + 1 + assert_eq!(is_power_of_two(5).unwrap(), false); // 2^2 + 1 + assert_eq!(is_power_of_two(9).unwrap(), false); // 2^3 + 1 + assert_eq!(is_power_of_two(17).unwrap(), false); // 2^4 + 1 + assert_eq!(is_power_of_two(33).unwrap(), false); // 2^5 + 1 + assert_eq!(is_power_of_two(65).unwrap(), false); // 2^6 + 1 + assert_eq!(is_power_of_two(129).unwrap(), false); // 2^7 + 1 + } + + #[test] + fn test_negative_number_returns_error() { + let result = is_power_of_two(-1); + assert!(result.is_err()); + assert_eq!(result.unwrap_err(), "number must not be negative"); + } + + #[test] + fn test_multiple_negative_numbers() { + assert!(is_power_of_two(-1).is_err()); + assert!(is_power_of_two(-2).is_err()); + assert!(is_power_of_two(-4).is_err()); + assert!(is_power_of_two(-8).is_err()); + assert!(is_power_of_two(-100).is_err()); + } + + #[test] + fn test_all_powers_of_two_up_to_30() { + // Test 2^0 through 2^30 + for i in 0..=30 { + let power = 1u32 << i; // 2^i + assert_eq!( + is_power_of_two(power as i32).unwrap(), + true, + "2^{i} = {power} should be a power of 2" + ); + } + } + + #[test] + fn test_range_verification() { + // Test that between consecutive powers of 2, only the powers return true + for i in 1..10 { + let power = 1 << i; // 2^i + assert_eq!(is_power_of_two(power).unwrap(), true); + + // Check numbers between this power and the next + let next_power = 1 << (i + 1); + for num in (power + 1)..next_power { + assert_eq!( + is_power_of_two(num).unwrap(), + false, + "{num} should not be a power of 2" + ); + } + } + } + + #[test] + fn test_bit_manipulation_correctness() { + // Verify the bit manipulation logic for specific examples + // For 8: 1000 & 0111 = 0000 ✓ + assert_eq!(8 & 7, 0); + assert_eq!(is_power_of_two(8).unwrap(), true); + + // For 16: 10000 & 01111 = 00000 ✓ + assert_eq!(16 & 15, 0); + assert_eq!(is_power_of_two(16).unwrap(), true); + + // For 6: 110 & 101 = 100 ✗ + assert_ne!(6 & 5, 0); + assert_eq!(is_power_of_two(6).unwrap(), false); + } + + #[test] + fn test_edge_case_max_i32_power_of_two() { + // Largest power of 2 that fits in i32: 2^30 = 1073741824 + assert_eq!(is_power_of_two(1073741824).unwrap(), true); + } +} diff --git a/src/bit_manipulation/mod.rs b/src/bit_manipulation/mod.rs index 13f649207c2..95bbb755123 100644 --- a/src/bit_manipulation/mod.rs +++ b/src/bit_manipulation/mod.rs @@ -2,6 +2,7 @@ mod binary_coded_decimal; mod counting_bits; mod find_previous_power_of_two; mod highest_set_bit; +mod is_power_of_two; mod n_bits_gray_code; mod reverse_bits; mod sum_of_two_integers; @@ -12,6 +13,7 @@ pub use self::binary_coded_decimal::binary_coded_decimal; pub use self::counting_bits::count_set_bits; pub use self::find_previous_power_of_two::find_previous_power_of_two; pub use self::highest_set_bit::find_highest_set_bit; +pub use self::is_power_of_two::is_power_of_two; pub use self::n_bits_gray_code::generate_gray_code; pub use self::reverse_bits::reverse_bits; pub use self::sum_of_two_integers::add_two_integers; From 56d031653720f77f121ab3661bb23885d4f6670c Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi <41567902+AliAlimohammadi@users.noreply.github.com> Date: Mon, 15 Dec 2025 20:00:34 -0800 Subject: [PATCH 3/4] Update is_power_of_two.rs --- src/bit_manipulation/is_power_of_two.rs | 124 ++++++++++++------------ 1 file changed, 61 insertions(+), 63 deletions(-) diff --git a/src/bit_manipulation/is_power_of_two.rs b/src/bit_manipulation/is_power_of_two.rs index 4f8435d41b0..a036c715f9a 100644 --- a/src/bit_manipulation/is_power_of_two.rs +++ b/src/bit_manipulation/is_power_of_two.rs @@ -87,87 +87,87 @@ mod tests { #[test] fn test_zero() { // 0 is considered a power of 2 by the algorithm (2^(-∞) interpretation) - assert_eq!(is_power_of_two(0).unwrap(), true); + assert!(is_power_of_two(0).unwrap()); } #[test] fn test_one() { // 1 = 2^0 - assert_eq!(is_power_of_two(1).unwrap(), true); + assert!(is_power_of_two(1).unwrap()); } #[test] fn test_powers_of_two() { - assert_eq!(is_power_of_two(2).unwrap(), true); // 2^1 - assert_eq!(is_power_of_two(4).unwrap(), true); // 2^2 - assert_eq!(is_power_of_two(8).unwrap(), true); // 2^3 - assert_eq!(is_power_of_two(16).unwrap(), true); // 2^4 - assert_eq!(is_power_of_two(32).unwrap(), true); // 2^5 - assert_eq!(is_power_of_two(64).unwrap(), true); // 2^6 - assert_eq!(is_power_of_two(128).unwrap(), true); // 2^7 - assert_eq!(is_power_of_two(256).unwrap(), true); // 2^8 - assert_eq!(is_power_of_two(512).unwrap(), true); // 2^9 - assert_eq!(is_power_of_two(1024).unwrap(), true); // 2^10 - assert_eq!(is_power_of_two(2048).unwrap(), true); // 2^11 - assert_eq!(is_power_of_two(4096).unwrap(), true); // 2^12 - assert_eq!(is_power_of_two(8192).unwrap(), true); // 2^13 - assert_eq!(is_power_of_two(16384).unwrap(), true); // 2^14 - assert_eq!(is_power_of_two(32768).unwrap(), true); // 2^15 - assert_eq!(is_power_of_two(65536).unwrap(), true); // 2^16 + assert!(is_power_of_two(2).unwrap()); // 2^1 + assert!(is_power_of_two(4).unwrap()); // 2^2 + assert!(is_power_of_two(8).unwrap()); // 2^3 + assert!(is_power_of_two(16).unwrap()); // 2^4 + assert!(is_power_of_two(32).unwrap()); // 2^5 + assert!(is_power_of_two(64).unwrap()); // 2^6 + assert!(is_power_of_two(128).unwrap()); // 2^7 + assert!(is_power_of_two(256).unwrap()); // 2^8 + assert!(is_power_of_two(512).unwrap()); // 2^9 + assert!(is_power_of_two(1024).unwrap()); // 2^10 + assert!(is_power_of_two(2048).unwrap()); // 2^11 + assert!(is_power_of_two(4096).unwrap()); // 2^12 + assert!(is_power_of_two(8192).unwrap()); // 2^13 + assert!(is_power_of_two(16384).unwrap()); // 2^14 + assert!(is_power_of_two(32768).unwrap()); // 2^15 + assert!(is_power_of_two(65536).unwrap()); // 2^16 } #[test] fn test_non_powers_of_two() { - assert_eq!(is_power_of_two(3).unwrap(), false); - assert_eq!(is_power_of_two(5).unwrap(), false); - assert_eq!(is_power_of_two(6).unwrap(), false); - assert_eq!(is_power_of_two(7).unwrap(), false); - assert_eq!(is_power_of_two(9).unwrap(), false); - assert_eq!(is_power_of_two(10).unwrap(), false); - assert_eq!(is_power_of_two(11).unwrap(), false); - assert_eq!(is_power_of_two(12).unwrap(), false); - assert_eq!(is_power_of_two(13).unwrap(), false); - assert_eq!(is_power_of_two(14).unwrap(), false); - assert_eq!(is_power_of_two(15).unwrap(), false); - assert_eq!(is_power_of_two(17).unwrap(), false); - assert_eq!(is_power_of_two(18).unwrap(), false); + assert!(!is_power_of_two(3).unwrap()); + assert!(!is_power_of_two(5).unwrap()); + assert!(!is_power_of_two(6).unwrap()); + assert!(!is_power_of_two(7).unwrap()); + assert!(!is_power_of_two(9).unwrap()); + assert!(!is_power_of_two(10).unwrap()); + assert!(!is_power_of_two(11).unwrap()); + assert!(!is_power_of_two(12).unwrap()); + assert!(!is_power_of_two(13).unwrap()); + assert!(!is_power_of_two(14).unwrap()); + assert!(!is_power_of_two(15).unwrap()); + assert!(!is_power_of_two(17).unwrap()); + assert!(!is_power_of_two(18).unwrap()); } #[test] fn test_specific_non_powers() { - assert_eq!(is_power_of_two(6).unwrap(), false); - assert_eq!(is_power_of_two(17).unwrap(), false); - assert_eq!(is_power_of_two(100).unwrap(), false); - assert_eq!(is_power_of_two(1000).unwrap(), false); + assert!(!is_power_of_two(6).unwrap()); + assert!(!is_power_of_two(17).unwrap()); + assert!(!is_power_of_two(100).unwrap()); + assert!(!is_power_of_two(1000).unwrap()); } #[test] fn test_large_powers_of_two() { - assert_eq!(is_power_of_two(131072).unwrap(), true); // 2^17 - assert_eq!(is_power_of_two(262144).unwrap(), true); // 2^18 - assert_eq!(is_power_of_two(524288).unwrap(), true); // 2^19 - assert_eq!(is_power_of_two(1048576).unwrap(), true); // 2^20 + assert!(is_power_of_two(131072).unwrap()); // 2^17 + assert!(is_power_of_two(262144).unwrap()); // 2^18 + assert!(is_power_of_two(524288).unwrap()); // 2^19 + assert!(is_power_of_two(1048576).unwrap()); // 2^20 } #[test] fn test_numbers_near_powers_of_two() { // One less than powers of 2 - assert_eq!(is_power_of_two(3).unwrap(), false); // 2^2 - 1 - assert_eq!(is_power_of_two(7).unwrap(), false); // 2^3 - 1 - assert_eq!(is_power_of_two(15).unwrap(), false); // 2^4 - 1 - assert_eq!(is_power_of_two(31).unwrap(), false); // 2^5 - 1 - assert_eq!(is_power_of_two(63).unwrap(), false); // 2^6 - 1 - assert_eq!(is_power_of_two(127).unwrap(), false); // 2^7 - 1 - assert_eq!(is_power_of_two(255).unwrap(), false); // 2^8 - 1 + assert!(!is_power_of_two(3).unwrap()); // 2^2 - 1 + assert!(!is_power_of_two(7).unwrap()); // 2^3 - 1 + assert!(!is_power_of_two(15).unwrap()); // 2^4 - 1 + assert!(!is_power_of_two(31).unwrap()); // 2^5 - 1 + assert!(!is_power_of_two(63).unwrap()); // 2^6 - 1 + assert!(!is_power_of_two(127).unwrap()); // 2^7 - 1 + assert!(!is_power_of_two(255).unwrap()); // 2^8 - 1 // One more than powers of 2 - assert_eq!(is_power_of_two(3).unwrap(), false); // 2^1 + 1 - assert_eq!(is_power_of_two(5).unwrap(), false); // 2^2 + 1 - assert_eq!(is_power_of_two(9).unwrap(), false); // 2^3 + 1 - assert_eq!(is_power_of_two(17).unwrap(), false); // 2^4 + 1 - assert_eq!(is_power_of_two(33).unwrap(), false); // 2^5 + 1 - assert_eq!(is_power_of_two(65).unwrap(), false); // 2^6 + 1 - assert_eq!(is_power_of_two(129).unwrap(), false); // 2^7 + 1 + assert!(!is_power_of_two(3).unwrap()); // 2^1 + 1 + assert!(!is_power_of_two(5).unwrap()); // 2^2 + 1 + assert!(!is_power_of_two(9).unwrap()); // 2^3 + 1 + assert!(!is_power_of_two(17).unwrap()); // 2^4 + 1 + assert!(!is_power_of_two(33).unwrap()); // 2^5 + 1 + assert!(!is_power_of_two(65).unwrap()); // 2^6 + 1 + assert!(!is_power_of_two(129).unwrap()); // 2^7 + 1 } #[test] @@ -191,9 +191,8 @@ mod tests { // Test 2^0 through 2^30 for i in 0..=30 { let power = 1u32 << i; // 2^i - assert_eq!( + assert!( is_power_of_two(power as i32).unwrap(), - true, "2^{i} = {power} should be a power of 2" ); } @@ -204,14 +203,13 @@ mod tests { // Test that between consecutive powers of 2, only the powers return true for i in 1..10 { let power = 1 << i; // 2^i - assert_eq!(is_power_of_two(power).unwrap(), true); + assert!(is_power_of_two(power).unwrap()); // Check numbers between this power and the next let next_power = 1 << (i + 1); for num in (power + 1)..next_power { - assert_eq!( - is_power_of_two(num).unwrap(), - false, + assert!( + !is_power_of_two(num).unwrap(), "{num} should not be a power of 2" ); } @@ -223,20 +221,20 @@ mod tests { // Verify the bit manipulation logic for specific examples // For 8: 1000 & 0111 = 0000 ✓ assert_eq!(8 & 7, 0); - assert_eq!(is_power_of_two(8).unwrap(), true); + assert!(is_power_of_two(8).unwrap()); // For 16: 10000 & 01111 = 00000 ✓ assert_eq!(16 & 15, 0); - assert_eq!(is_power_of_two(16).unwrap(), true); + assert!(is_power_of_two(16).unwrap()); // For 6: 110 & 101 = 100 ✗ assert_ne!(6 & 5, 0); - assert_eq!(is_power_of_two(6).unwrap(), false); + assert!(!is_power_of_two(6).unwrap()); } #[test] fn test_edge_case_max_i32_power_of_two() { // Largest power of 2 that fits in i32: 2^30 = 1073741824 - assert_eq!(is_power_of_two(1073741824).unwrap(), true); + assert!(is_power_of_two(1073741824).unwrap()); } } From 4573782f07fa512eea8a6de8b96943a1eee48150 Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi <41567902+AliAlimohammadi@users.noreply.github.com> Date: Tue, 16 Dec 2025 09:10:58 -0800 Subject: [PATCH 4/4] Update DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 96d903e1b80..9645d01eda3 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -20,7 +20,7 @@ * [Binary Coded Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/binary_coded_decimal.rs) * [Counting Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/counting_bits.rs) * [Highest Set Bit](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/highest_set_bit.rs) - * [Highest Set Bit](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/is_power_of_two.rs) + * [Is Power of Two](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/is_power_of_two.rs) * [N Bits Gray Code](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/n_bits_gray_code.rs) * [Previous Power of Two](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/find_previous_power_of_two.rs) * [Reverse Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/reverse_bits.rs)