Thread: [Dev-C++] bit extraction problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Dawood V. <da...@ku...> - 2003-08-17 07:07:08
|
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.
#include <cstdlib>
#include <iostream>
#include <climits>
using namespace std;
#define BYTE 8
int main()
{
string bits;
long x;
unsigned long y = 1;
cin>>x;
if ((x >= INT_MIN) && (x <= INT_MAX)){
int i = 1;
while (i++ <= BYTE*sizeof(x)) {
if (x&y)
bits = '1' + bits;
else
bits = '0' + bits;
y<<=1;
}
cout<<"the bit layout in x :";
cout<<bits<<'\n';
}
else
cout<< "input number either too large or too small\n";
system("PAUSE");
return 0;
}
|
|
From: w.y. <yok...@ve...> - 2003-08-17 11:00:26
|
Dawood Vora wrote:
>
> string bits;
> long x;
> unsigned long y = 1;
>
> cin>>x;
> if ((x >= INT_MIN) && (x <= INT_MAX)){
Try this out and examine the output carefully:
cout << "sizeof(int) = " << sizeof(int) << endl;
cout << "sizeof(long) = " << sizeof(long) << endl;
cout << "sizeof(long long) = " << sizeof(long long) << endl;
cout << "\nINT_MIN, INT_MAX = " << INT_MIN << ", "
<< INT_MAX << endl;
cout << "LONG_MIN, LONG_MAX = " << LONG_MIN << ", "
<< LONG_MAX << endl;
cout << "\n(hex)INT_MIN, INT_MAX = " << hex << INT_MIN
<< ", " << INT_MAX << endl;
cout << "(hex)LONG_MIN, LONG_MAX = " << LONG_MIN << ", "
<< LONG_MAX << endl;
cout << "\nLONG_LONG_MIN, LONG_LONG_MAX = " << dec
<< LONG_LONG_MIN << ", " << LONG_LONG_MAX << endl;
cout << " (hex) = " << hex << LONG_LONG_MIN << ", "
<< LONG_LONG_MAX << endl;
|
|
From: Ioannis V. <iv...@em...> - 2003-08-17 12:44:03
|
> -----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 |
|
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 > |
|
From: Ioannis V. <iv...@em...> - 2003-08-18 00:17:10
|
One "analog" approach that I can think is to convert the numeric_limits<long>::max() and min() to strings and count the digits. Then compare the number of digits of the input with the number of digits of max or min depending if the input has a minus sign. If the number if digits is equal with the equivalent limit, compare if the first digit is larger/smaller of the corresponding limit (depending on the minus sign). And so on. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net > -----Original Message----- > From: Per Westermark [mailto:pw...@ia...] > Sent: Sunday, August 17, 2003 4:19 PM > To: Ioannis Vranos > Cc: 'Dev C++' > Subject: RE: [Dev-C++] bit extraction problem > > > 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 :-) |