From 35afea97c04550c1c15d54b110dc34bfd4e018e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gil=20de=20G=C3=B3mez=20P=C3=A9rez?= Date: Tue, 11 Aug 2026 18:35:45 +0300 Subject: [PATCH] Solved: 03 - Operators --- modules/01_fundamentals/src/operators.cpp | 40 +++++++++-------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/modules/01_fundamentals/src/operators.cpp b/modules/01_fundamentals/src/operators.cpp index 7eb6855..cc7c894 100644 --- a/modules/01_fundamentals/src/operators.cpp +++ b/modules/01_fundamentals/src/operators.cpp @@ -2,39 +2,29 @@ namespace fundamentals { -auto absolute_difference(int lhs, int rhs) -> int { - // TODO: Return |lhs - rhs| without using std::abs. - (void)lhs; - (void)rhs; - return 0; +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(int value, int min, int max) -> bool { - // TODO: Return true when min <= value <= max (inclusive). - (void)value; - (void)min; - (void)max; - return false; +auto is_in_range(const int value, const int min, const int max) -> bool { + return value >= min && value <= max; } -auto count_set_bits(std::uint8_t value) -> int { - // TODO: Count how many bits are set to 1. - (void)value; - return 0; +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(bool lhs, bool rhs) -> bool { - // TODO: Implement logical AND without using &&. - (void)lhs; - (void)rhs; - return false; +auto logical_and(const bool lhs, const bool rhs) -> bool { + return (lhs & rhs) == 1; } -auto logical_or(bool lhs, bool rhs) -> bool { - // TODO: Implement logical OR without using ||. - (void)lhs; - (void)rhs; - return false; +auto logical_or(const bool lhs, const bool rhs) -> bool { + return (lhs | rhs) == 1; } } // namespace fundamentals