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

33 lines
769 B
C++
Raw Normal View History

2026-08-11 17:40:36 +03:00
#include "operators.hpp"
namespace fundamentals {
2026-08-11 18:35:45 +03:00
auto absolute_difference(const int lhs, const int rhs) -> int {
// NOTE: std::abs not allowed
return lhs < rhs ? rhs - lhs : lhs - rhs;
2026-08-11 17:40:36 +03:00
}
2026-08-11 18:35:45 +03:00
auto is_in_range(const int value, const int min, const int max) -> bool {
return value >= min && value <= max;
2026-08-11 17:40:36 +03:00
}
2026-08-11 18:35:45 +03:00
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;
2026-08-11 17:40:36 +03:00
}
2026-08-11 18:35:45 +03:00
auto logical_and(const bool lhs, const bool rhs) -> bool {
2026-08-11 18:41:26 +03:00
// NOTE: && can't be used
2026-08-11 18:35:45 +03:00
return (lhs & rhs) == 1;
2026-08-11 17:40:36 +03:00
}
2026-08-11 18:35:45 +03:00
auto logical_or(const bool lhs, const bool rhs) -> bool {
2026-08-11 18:41:26 +03:00
// NOTE: || can't be used
2026-08-11 18:35:45 +03:00
return (lhs | rhs) == 1;
2026-08-11 17:40:36 +03:00
}
} // namespace fundamentals