[ create a new paste ] login | about

Link: http://codepad.org/EGCNPBdN    [ raw code | output | fork ]

ninwa - C++, pasted on Sep 15:
#include <string>
#include <sstream>
#include <iostream>

/* TODO: Add a way to dermine the number of bits that are in a byte
   on our current architecture. This should be possible using the
   numerical limits header. */
#define BITS_IN_BYTE 8

namespace BitSystem{
	enum BitSystem
	{
		UNSIGNED,
		SIGNED_MAGNITUDE,
		TWOS_COMPLIMENT,
		ONES_COMPLIMENT
	};
}

template <class T>
class BitInterpreter
{
	T _bits;

	bool _is_signed;
	size_t _type_sz;

	void BitInterpreter_(bool is_signed, size_t size, T bits);

public:
	BitInterpreter(char bits);
	BitInterpreter(unsigned char bits);
	BitInterpreter(unsigned short bits);
	BitInterpreter(unsigned int bits);
	BitInterpreter(unsigned long bits);
//	BitInterpreter(unsigned long long bits);
	BitInterpreter(signed char bits);
	BitInterpreter(signed short bits);
	BitInterpreter(signed int bits);
	BitInterpreter(signed long bits);
//	BitInterpreter(signed long long bits);

public:
	std::string getBitsAsString();	
	std::string getInterpretationAsString(BitSystem::BitSystem system = BitSystem::UNSIGNED);
};
	
template<class T> 
void BitInterpreter<T>::BitInterpreter_(bool is_signed, size_t size, T bits)
{
	_is_signed = is_signed;
	_type_sz = size;
	_bits = bits;
}

template<class T> BitInterpreter<T>::BitInterpreter(char bits){ BitInterpreter_(true, sizeof(T) * BITS_IN_BYTE, bits); }
template<class T> BitInterpreter<T>::BitInterpreter(unsigned char bits){ BitInterpreter_(false, sizeof(T) * BITS_IN_BYTE, bits); }
template<class T> BitInterpreter<T>::BitInterpreter(unsigned short bits){ BitInterpreter_(false, sizeof(T ) * BITS_IN_BYTE, bits); }
template<class T> BitInterpreter<T>::BitInterpreter(unsigned int bits){ BitInterpreter_(false, sizeof(T) * BITS_IN_BYTE, bits); }
template<class T> BitInterpreter<T>::BitInterpreter(unsigned long bits){ BitInterpreter_(false, sizeof(T) * BITS_IN_BYTE, bits); }
//template<class T> BitInterpreter<T>::BitInterpreter(unsigned long long bits){ BitInterpreter_(false, sizeof(T) * BITS_IN_BYTE, bits); }
template<class T> BitInterpreter<T>::BitInterpreter(signed char bits){ BitInterpreter_(true, sizeof(T) * BITS_IN_BYTE, bits); }
template<class T> BitInterpreter<T>::BitInterpreter(signed short bits){ BitInterpreter_(true, sizeof(T) * BITS_IN_BYTE, bits); }
template<class T> BitInterpreter<T>::BitInterpreter(signed int bits){ BitInterpreter_(true, sizeof(T) * BITS_IN_BYTE, bits); }
template<class T> BitInterpreter<T>::BitInterpreter(signed long bits){ BitInterpreter_(true, sizeof(T) * BITS_IN_BYTE, bits); }
//template<class T> BitInterpreter<T>::BitInterpreter(signed long long bits){ BitInterpreter_(true, sizeof(T) * BITS_IN_BYTE, bits); }

	template<class T>
	std::string BitInterpreter<T>::getBitsAsString()
	{
		std::string s_bits = "";
		
		s_bits += (_bits < 0) ? "1" : "0";
		for(int i = _type_sz-2; i >= 0; i--)
		{
			T mask = (1 << i);
			T isolated = mask & _bits;

			s_bits += ((isolated != 0) ? "1" : "0");
		}
		

		return s_bits;
	}

template<class T>
std::string BitInterpreter<T>::getInterpretationAsString(BitSystem::BitSystem system)
{
	std::string s_interpretation = "";
	std::stringstream ss;

	/* Note, in the future it may be interesting to add a way to determine whether or not
	   our machine is using two's compliment, because our cases all presently assumes that  
	   it does to operate correctly. */

	switch(system)
	{	
		default:
		case BitSystem::UNSIGNED:
		{
			ss << ((unsigned long) _bits);
			s_interpretation = ss.str();
			break;
		}
		case BitSystem::SIGNED_MAGNITUDE:
		{
			T bits = _bits;
			if(_is_signed)
			{
				bits <<= 1;
				bits >>= 1;
				ss << "-";
			}

			ss << (unsigned long) bits;
			break;
		}
		case BitSystem::ONES_COMPLIMENT:
		{
			T bits = (_is_signed && _bits < 0) ? _bits + 1 : _bits;
			ss << bits;
			s_interpretation = ss.str();
			break;
		}
		case BitSystem::TWOS_COMPLIMENT: 
		{
			ss << (long) _bits;
			s_interpretation = ss.str();
			break;
		}
	}

	return s_interpretation;
}

template<class T>
BitInterpreter<T> CreateBitInterpreter(T bits)
{
	return BitInterpreter<T>(bits);
}

int main(int argc, char* argv[])
{
	/* An example of setting up a BitInterpreter operating on short's with
	   our value 3 (binary: 11) */
	BitInterpreter<short> short_interp = CreateBitInterpreter<short>(3);

	/* An example of our short being returned to us as a string (with the appropriate zero's padded */
	std::cout << short_interp.getBitsAsString() << std::endl;

	/* An example of our short being re-interpreted and returned to us as a string as if it
	   were (which it is) in a two's compliment system. */
	std::cout << short_interp.getInterpretationAsString(BitSystem::TWOS_COMPLIMENT) << std::endl;

	/* An example of a bit interpreter created operating on int's with the value 5 (binary: 101) */
	BitInterpreter<int> int_interp = CreateBitInterpreter<int>(5);
	std::cout << int_interp.getBitsAsString() << std::endl;
	std::cout << int_interp.getInterpretationAsString(BitSystem::ONES_COMPLIMENT) << std::endl;

	BitInterpreter<int> int_interp2 = CreateBitInterpreter<int>(-5);
	std::cout << int_interp2.getBitsAsString() << std::endl;
	std::cout << int_interp2.getInterpretationAsString(BitSystem::ONES_COMPLIMENT) << std::endl;
}


Output:
1
2
3
4
5
6
0000000000000011
3
00000000000000000000000000000101
5
11111111111111111111111111111011
-4


Create a new paste based on this one


Comments: