// // This file is part of // // CTBignum // // C++ Library for Compile-Time and Run-Time Multi-Precision and Modular Arithmetic // // // This file is distributed under the Apache License, Version 2.0. See the LICENSE // file for details. #ifndef CT_BITSHIFTS_HPP #define CT_BITSHIFTS_HPP #include #include #include #include namespace cbn { template constexpr auto shift_right(big_int a, size_t k) { // shift-right the big integer a by k bits big_int res{}; if (k == 0) return a; for (auto i = 0; i < N - 1; ++i) { res[i] = (a[i] >> k) | (a[i + 1] << (std::numeric_limits::digits - k)); } res[N - 1] = (a[N - 1] >> k); return res; } template constexpr auto shift_left(big_int a, size_t k) { // shift-left the big integer a by k bits // answer has 1 limb more // if (k == 0) return detail::pad<1>(a); big_int res{}; res[0] = (a[0] << k); for (auto i = 1; i < N; ++i) { res[i] = (a[i] << k) | (a[i - 1] >> (std::numeric_limits::digits - k)); } res[N] = a[N - 1] >> (std::numeric_limits::digits - k); return res; } } #endif