Answered Questions

  • How can you determine the maximum value that a numeric variable can hold?

    For integral types, on a machine that uses two’s complement arithmetic (which is just about any machine you’re likely to use), a signed type can hold numbers from –2(number of bits – 1) to +2(number of bits – 1) – 1. An unsigned type can hold values from 0 to +2(number of bits) – 1. For instance, a 16-bit signed integer can hold numbers from –2^15 (–32768) to +2^15 – 1 (32767).  

    Jimmy Jack

    • Apr 26th, 2012

    Code
    1. #include
    2. #include
    3.  
    4. template T get_min_value(void)
    5. {
    6.     return (T) (1 << ((sizeof(T) << 3) - 1));
    7. }
    8.  
    9. template T get_max_value(void)
    10. {
    11.     return (~(T) 0 - get_min_value());
    12. }
    13.  
    14. template T get_umin_value(void)
    15. {
    16.     return (T) 0;
    17. }
    18.  
    19. template T get_umax_value(void)
    20. {
    21.     return (~(T) 0);
    22. }
    23.  
    24. int main()
    25. {
    26.     std::cout << "minmum signed integer - " << get_min_value() << std::endl;
    27.     std::cout << "maxmum signed integer - " << get_max_value() << std::endl;
    28.     std::cout << "minmum unsigned integer - " << get_umin_value() << std::endl;
    29.     std::cout << "maxmum unsigned integer - " << get_umax_value() << std::endl;
    30.  
    31.     std::cout << "minmum signed short - " << get_min_value() << std::endl;
    32.     std::cout << "maxmum signed short - " << get_max_value() << std::endl;
    33.     std::cout << "minmum unsigned short - " << get_umin_value() << std::endl;
    34.     std::cout << "maxmum unsigned short - " << get_umax_value() << std::endl;
    35. }
    36.  
    37.  

    VENU BABU

    • Oct 7th, 2011

    First include limits.h
    in that so many variables to find max and min value that can hold by any data type
    for ex: INT_MAX,INT_MIN for integer and so on.......