#include <iostream>
using namespace std;
class bits{
unsigned int value;
public:
bits(unsigned int val){
value = val;
}
static bool formatted; // by default false, if true, then bits are shown 4 digit at a time...
ostream& print(ostream& os) const{
unsigned m = 1 << (sizeof(value)*8 - 1);
unsigned i = 1;
while(m){
os<<!!(value & m) ? '1' : '0';
if (formatted){
i == 4 ? os<<' ', i = 1 : i++;
}
m >>= 1;
}
return os;
}
~bits(){
value = 0;
}
};
ostream& operator<<(ostream& os, const bits& from){
return from.print(os);
}
bool bits::formatted = false;
int main(){
char A = 0x5c;
char B = 0x95;
char C = 0;
cout<<bits(A)<<endl; //0000101010101010
bits::formatted = true;
cout<<bits(B)<<endl; //0000 0011 0000 1111
return 0;
}
No comments:
Post a Comment