fork download
  1. #include <iostream>
  2. #include <stdint.h>
  3. #include <climits>
  4.  
  5. template <typename T>
  6. class Num {
  7. public:
  8. inline Num (T src) : val (src) {}
  9.  
  10. T val;
  11. };
  12.  
  13. template <typename T> Num<T> inline operator * (Num<T> a, Num<T> b) { return ( static_cast<T> (a.val * b.val) ); }
  14. template <typename T> Num<T> inline operator / (Num<T> a, Num<T> b) { return ( static_cast<T> (a.val / b.val) ); }
  15. template <typename T> Num<T> inline operator + (Num<T> a, Num<T> b) { return ( static_cast<T> (a.val + b.val) ); }
  16. template <typename T> Num<T> inline operator - (Num<T> a, Num<T> b) { return ( static_cast<T> (a.val - b.val) ); }
  17. template <typename T> Num<T> inline operator % (Num<T> a, Num<T> b) { return ( static_cast<T> (a.val % b.val) ); }
  18.  
  19. int main () {
  20. typedef Num<uint16_t> N16;
  21. typedef Num<uint32_t> N32;
  22. N16 res16 = (N16 ( 1000 ) * N16 ( 1000 )) / N16 ( 100 );
  23. N32 res32 = (N32 ( 1000 ) * N32 ( 1000 )) / N32 ( 100 );
  24.  
  25. std::cout << "16bit Rechnung: " << res16.val << '\n'
  26. << "32bit Rechnung: " << res32.val << '\n'
  27. << "Diese Plattform hat " << (sizeof(int) * CHAR_BIT) << "bit \"int\", "
  28. << (sizeof(long) * CHAR_BIT) << "bit \"long\", "
  29. << (sizeof(void*) * CHAR_BIT) << "bit Adressraum.\n";
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
16bit Rechnung: 169
32bit Rechnung: 10000
Diese Plattform hat 32bit "int", 32bit "long", 32bit Adressraum.