bits.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "claraty/bits.h"
00023 #include <stdio.h>
00024
00025 using namespace std;
00026 using namespace claraty;
00027
00028
00029
00030
00031
00032 namespace claraty {
00033
00034 string Bits::to_str() const
00035 {
00036
00037
00038 char buffer[128], *output;
00039 output = new char[256];
00040
00041 sprintf(buffer, "[%2d] {", static_cast<unsigned int>(_Nbits));
00042 strcpy(output, buffer);
00043
00044
00045
00046
00047 int n_bytes = (_Nbits + 7)/8;
00048 for (int i = n_bytes - 1; i >= 0; --i) {
00049 sprintf(buffer, "%2x ", static_cast<unsigned int>((_data >> i*8) & 0xff));
00050 strcat(output, buffer);
00051 }
00052 strcat(output, "} ");
00053
00054
00055
00056 int n = (_Nbits + 3) / 4 * 4;
00057 int j=0;
00058
00059 for (size_t i = n - 1; i >= 0; i--) {
00060 if (i >= _Nbits) {
00061 buffer[j++] = '-';
00062 continue;
00063 }
00064
00065
00066 if (test(i))
00067 buffer[j++] = '1';
00068 else
00069 buffer[j++] = '0';
00070
00071 if ( (i % 4) == 0 && i != 0)
00072 buffer[j++] = ' ';
00073 }
00074 buffer[j] = 0;
00075
00076 strcat(output, buffer);
00077
00078 string s(output);
00079 delete [] output;
00080
00081 return s;
00082 }
00083
00084
00085
00086
00087
00088
00089
00090 void Bits::print(const char* msg) const
00091 {
00092 cout << msg << "\n " << to_str().c_str() << endl;
00093
00094 }
00095
00096 ostream& operator<<(ostream& ostr, const Bits& rhs)
00097 {
00098 ostr << rhs.to_str().c_str();
00099
00100 return ostr;
00101 }
00102
00103
00104 }