RE: [Dev-C++] bit extraction problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-08-17 13:40:59
|
One alternative is to count the number of digits, and take the log. One decimal digit consumes 1/log(2) binary digits, or approx 3.322 bits. However, it is still only possible to get close to the true limit - since the log of a number needs at least as many value-digits as the number for an exact result :-) Another approach - which is exact - is to perform the decimal->binary conversion in own code, and for each added decimal digit check if the binary number overflows. For a positive number decimal input number, the resulting binary should increase for each iteration. If the input number is negative, it is simple to check for overflow by negating the result twice and check that the original value was returned. For a traditional machine with two-complement handling, a negation at the end will not overflow. However, I'm not sure if that is true for all strange machines out there, that uses other ways of encoding negative numbers. Maybe it is possible to negate a number twice and check if original value is returned. Anyway, right now, 2-complement rulez :-) /Per W On Sun, 17 Aug 2003, Ioannis Vranos wrote: > > -----Original Message----- > > From: dev...@li... > > [mailto:dev...@li...] On Behalf > > Of Dawood Vora > > Sent: Sunday, August 17, 2003 10:07 AM > > To: Dev C++ > > Subject: [Dev-C++] bit extraction problem > > > > > > bit extraction problem > > > > Below is a program to extract the layout of the bits from an integer. > > It works fine, if the input number is within the range of > > long type of int. > > It is suppose to give me a message, if the input number is > > either greater > > than the max limits of long or lower than the min limits of > > long. But it > > does not. Apparently the offending line is : "cin>>x;" If x > > is not able > > to accommodate any input number it simply stores the number 2 > > as per my > > system. Does anybody know, how to solve this problem. > > > Read the number as a string so as to be sure that it fits however large the > "value" is. Then make some trick to see if it is within > numeric_limits<long>::max(), numeric_limits<long>::min() (defined in > <limits> which are the equivalent of LONG_MIN and LONG_MAX defined in > <climits>). > > > > > > > Ioannis Vranos > > * Programming pages: http://www.noicys.freeurl.com > * Alternative URL 1: http://run.to/noicys > * Alternative URL 2: http://www.noicys.cjb.net > > > > > ------------------------------------------------------- > This SF.Net email sponsored by: Free pre-built ASP.NET sites including > Data Reports, E-commerce, Portals, and Forums are available now. > Download today and enter to win an XBOX or Visual Studio .NET. > http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 > _______________________________________________ > 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 > |