Solved: 03 - Operators
This commit is contained in:
@@ -2,39 +2,29 @@
|
|||||||
|
|
||||||
namespace fundamentals {
|
namespace fundamentals {
|
||||||
|
|
||||||
auto absolute_difference(int lhs, int rhs) -> int {
|
auto absolute_difference(const int lhs, const int rhs) -> int {
|
||||||
// TODO: Return |lhs - rhs| without using std::abs.
|
// NOTE: std::abs not allowed
|
||||||
(void)lhs;
|
return lhs < rhs ? rhs - lhs : lhs - rhs;
|
||||||
(void)rhs;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto is_in_range(int value, int min, int max) -> bool {
|
auto is_in_range(const int value, const int min, const int max) -> bool {
|
||||||
// TODO: Return true when min <= value <= max (inclusive).
|
return value >= min && value <= max;
|
||||||
(void)value;
|
|
||||||
(void)min;
|
|
||||||
(void)max;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto count_set_bits(std::uint8_t value) -> int {
|
auto count_set_bits(const std::uint8_t value) -> int {
|
||||||
// TODO: Count how many bits are set to 1.
|
auto count = 0;
|
||||||
(void)value;
|
for (int i = 0; i < 8; i++) {
|
||||||
return 0;
|
if (((value >> i) & 1) == 1) count++;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto logical_and(bool lhs, bool rhs) -> bool {
|
auto logical_and(const bool lhs, const bool rhs) -> bool {
|
||||||
// TODO: Implement logical AND without using &&.
|
return (lhs & rhs) == 1;
|
||||||
(void)lhs;
|
|
||||||
(void)rhs;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto logical_or(bool lhs, bool rhs) -> bool {
|
auto logical_or(const bool lhs, const bool rhs) -> bool {
|
||||||
// TODO: Implement logical OR without using ||.
|
return (lhs | rhs) == 1;
|
||||||
(void)lhs;
|
|
||||||
(void)rhs;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace fundamentals
|
} // namespace fundamentals
|
||||||
|
|||||||
Reference in New Issue
Block a user