|
From: Jonathan W. <jon...@gm...> - 2007-06-07 16:55:37
|
On 6/7/07, Philip Bennefall <phi...@pb...> wrote: > > Hi, > Could anyone tell me the formula which is used to calculate the min and > max values for a numerical data type of x bytes? Say for instance that I use > the > sizeof(datatype) function and get the answer 4, which is usually an > integer, how do I then programatically calculate the min and max values for > a signed > and unsigned version of the data type respectively. > Thanks in advance. > Regards > Philip Bennefall > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > Hi, Usually there are constants defined for this use, which might be more useful. However, this is how to calculate the values, supposing the encoding for negative number is 2's complement (machines that don't use this are rather rare, I believe, and won't be running windows...) Also supposing your data type is some kind of binary integer (floats and double are encoded differently) nb_bytes = sizeof(...) nb_bits = 8*nb_bytes max_unsigned = 2^nb_bits - 1 min_unsigned = 0 //no kidding ! max_signed = 2^(nb_bits-1) - 1 min_unsigned = -( 2^(nb_bits-1) ) Jonathan -- <Morpheus> linux, c'est une question de VI ou de MORE |