Vincent Lefevre <vincent-o...@vinc17.net> wrote:

> On 2018-07-23 08:06:46 +0000, Schwarz, Konrad wrote:
> > I don't think such code (to detect whether an arbitrary type is
> > signed or unsigned) exists.
>
> For the signness, one can just do: (T) -1 < 0
>
> Then, to get the minimum value of a signed type, assuming
> two's complement and no padding bits:
>
>   (2 * -((T) 1 << (sizeof(T) * CHAR_BIT - 2)))
>
> as said in the message I've posted a few days ago.

You can even check the hardware type.....

On thursday, I changed my code to:

#define TYPE_ISSIGNED(t)        (((t)-1) < ((t)0)) 
#define TYPE_ISUNSIGNED(t)      (!TYPE_ISSIGNED(t)) 
#if (-3 & 3) == 1               /* Two's complement */ 
#define TYPE_MSBVAL(t)          (2 * -(((t)1) << (sizeof (t)*CHAR_BIT - 2))) 
#else 
#define TYPE_MSBVAL(t)          ((t)(~((t)0) << (sizeof (t)*CHAR_BIT - 1))) 
#endif 
#define TYPE_MINVAL(t)          (TYPE_ISSIGNED(t)                       \ 
                                    ? TYPE_MSBVAL(t)                    \ 
                                    : ((t)0)) 
#define TYPE_MAXVAL(t)          ((t)(~((t)0) - TYPE_MINVAL(t))) 


As a hint, you may use:

#if (-3 & 3) == 1
        two's complement
#elif (-3 & 3) == 0
        one's complement
#elif (-3 & 3) == 3
        magnitude type
#endif

Jörg

-- 
 EMail:jo...@schily.net                    (home) Jörg Schilling D-13353 Berlin
    joerg.schill...@fokus.fraunhofer.de (work) Blog: http://schily.blogspot.com/
 URL: http://cdrecord.org/private/ http://sf.net/projects/schilytools/files/'

Reply via email to