|
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;
}
|