Advertisement
Semper_Idem

Order three ints without branches

May 1st, 2020
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <bits/stdc++.h> // CHAR_BIT
  2. #include <cstdint>
  3. #include <iostream>
  4.  
  5. int64_t abs(int64_t n) {
  6.   int64_t mask = n >> (sizeof(n) * CHAR_BIT - 1);
  7.   return (n + mask) ^ mask;
  8. }
  9.  
  10. int32_t max(int32_t a, int32_t b) {
  11.   int64_t _a = a;
  12.   int64_t _b = b;
  13.   return (int32_t)(((_a + _b) + abs(_a - _b)) / 2);
  14. }
  15.  
  16. int32_t min(int32_t a, int32_t b) {
  17.   int64_t _a = a;
  18.   int64_t _b = b;    
  19.   return (int32_t)(((_a + _b) - abs(_a - _b)) / 2);
  20. }
  21.  
  22. void order(int32_t a, int32_t b, int32_t c, int32_t &low, int32_t &mid , int32_t &high) {
  23.   low = min(min(a, b), c);
  24.   mid = max(min(a, b), min(max(a, b), c));
  25.   high = max(max(a, b), c);
  26. }
  27.  
  28. int main()
  29. {
  30.   int32_t a, b, c, low, mid, high;
  31.   std::cin >> a >> b >> c;
  32.   order(a, b, c, low, mid, high);
  33.   std::cout << low << " " << mid << " " << high << std::endl;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement