Files
cpp-course/modules/01_fundamentals/src/operators.cpp
T

33 lines
769 B
C++

#include "operators.hpp"
namespace fundamentals {
auto absolute_difference(const int lhs, const int rhs) -> int {
// NOTE: std::abs not allowed
return lhs < rhs ? rhs - lhs : lhs - rhs;
}
auto is_in_range(const int value, const int min, const int max) -> bool {
return value >= min && value <= max;
}
auto count_set_bits(const std::uint8_t value) -> int {
auto count = 0;
for (int i = 0; i < 8; i++) {
if (((value >> i) & 1) == 1) count++;
}
return count;
}
auto logical_and(const bool lhs, const bool rhs) -> bool {
// NOTE: && can't be used
return (lhs & rhs) == 1;
}
auto logical_or(const bool lhs, const bool rhs) -> bool {
// NOTE: || can't be used
return (lhs | rhs) == 1;
}
} // namespace fundamentals