@tag: BiT C TemplatE CPlusPlus

C

#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
#define TYPE_MAX(t) \
 ((t) (! TYPE_SIGNED (t) \
 ? (t) -1 \
 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))

CPlusPlus

#include <iostream>
#include <typeinfo>
#include <cxxabi.h>

template<typename T>
T max()
{
  T Value = ~0; 
  if (Value == (Value >> 1))  {
    Value = 1;
    Value <<= (sizeof(Value) << 3) - 1;
    Value = ~Value;
  }
  return(Value);
}


  info: unsigned short (16b) [0,65535]
  info: unsigned int (32b) [0,4294967295]
  info: unsigned long (32b) [0,4294967295]
  info: unsigned long long (64b) [0,18446744073709551615]
  info: short (16b) [-32768,32767]
  info: int (32b) [-2147483648,2147483647]
  info: long (32b) [-2147483648,2147483647]
  info: long long (64b) [-9223372036854775808,9223372036854775807]




template<typename  T>
T min()
{ 
  T Value = max<T>();
  return(++Value);
}


template<typename  T>
void display()
{
  std::string name;
  int status;
  name = abi::__cxa_demangle(typeid(T).name(), 0, 0, &status);
  std::cout << "info: " << name << " ("<<(sizeof(T)<<3)<<"b)"<<" [" << min<T>() <<","<< max<T>() <<"]"<<std::endl; 
}


int main()
{
  //display<unsigned char>();
  display<unsigned short>();
  display<unsigned int>();
  display<unsigned long>();
  display<unsigned long long>();
  //display<unsigned float>();
  //display<unsigned double>();

  //display<char>();
  display<short>();
  display<int>();
  display<long>();
  display<long long>();
  //display<float>();
  //display<double>();

  return(0);
}


/*

info: unsigned short (16b) [0,65535]
info: unsigned int (32b) [0,4294967295]
info: unsigned long (32b) [0,4294967295]
info: unsigned long long (64b) [0,18446744073709551615]
info: short (16b) [-32768,32767]
info: int (32b) [-2147483648,2147483647]
info: long (32b) [-2147483648,2147483647]
info: long long (64b) [-9223372036854775808,9223372036854775807]


*/
limit.txt · Last modified: 2022/04/16 12:23 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki