malachite_nz/integer/arithmetic/
power_of_2.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer::Integer;
10use malachite_base::num::arithmetic::traits::PowerOf2;
11use malachite_base::num::basic::traits::One;
12
13impl PowerOf2<u64> for Integer {
14    /// Raises 2 to an integer power.
15    ///
16    /// $f(k) = 2^k$.
17    ///
18    /// # Worst-case complexity
19    /// $T(n) = O(n)$
20    ///
21    /// $M(n) = O(n)$
22    ///
23    /// where $T$ is time, $M$ is additional memory, and $n$ is `pow`.
24    ///
25    /// # Examples
26    /// ```
27    /// use malachite_base::num::arithmetic::traits::PowerOf2;
28    /// use malachite_nz::integer::Integer;
29    ///
30    /// assert_eq!(Integer::power_of_2(0), 1);
31    /// assert_eq!(Integer::power_of_2(3), 8);
32    /// assert_eq!(
33    ///     Integer::power_of_2(100).to_string(),
34    ///     "1267650600228229401496703205376"
35    /// );
36    /// ```
37    #[inline]
38    fn power_of_2(pow: u64) -> Self {
39        Self::ONE << pow
40    }
41}