New and improved binary class

A

Alex Buell

Right... here it is - flames and comments all welcome:

#ifndef __BINARY__
#define __BINARY__

#include <iostream>
#include <string>
#include <limits>
#include <exception>

class binary
{
public:
class bad_size : public std::exception
{
virtual const char* what() const throw()
{
return "Number of Bits != Number of digits!";
}
} bad_size;

class bad_range : public std::exception
{
virtual const char* what() const throw()
{
return "Out of Range";
}
} bad_range;

template <typename T>
binary(const T& n)
{
const unsigned int bits =
std::numeric_limits<T>::digits; for (unsigned int i = 0; i < bits; i++)
digits.insert(digits.begin(), (n & (1 << i)) ?
'1' : '0');

// digits.insert(digits.begin(), 'E'); // tests
exception if (digits.size() != bits)
throw bad_size;
}

const bool test(const unsigned int n) const
{
if (n > digits.size() || n == 0)
throw bad_range;

return (digits[n - 1] == '1') ? true : false;
}

void set(const unsigned int n)
{
if (n > digits.size() || n == 0)
throw bad_range;

digits[n - 1] = '1';
}

friend std::eek:stream& operator<<(std::eek:stream& stream, const
binary& bin) {
return stream << bin.digits;
}

private:
std::string digits;
};

#endif // __BINARY__
 
M

mlimber

Alex said:
Right... here it is - flames and comments all welcome:

For the sake of completeness, the previous version can be found here:

http://groups.google.com/group/comp.lang.c++/browse_frm/thread/a9eba91ee4bd9466
#ifndef __BINARY__
#define __BINARY__

Don't use double underscores to start your names.
#include <iostream>
#include <string>
#include <limits>
#include <exception>

class binary
{
public:
class bad_size : public std::exception
{
virtual const char* what() const throw()
{
return "Number of Bits != Number of digits!";
}
} bad_size;

class bad_range : public std::exception
{
virtual const char* what() const throw()
{
return "Out of Range";
}
} bad_range;

template <typename T>
binary(const T& n)
{
const unsigned int bits =
std::numeric_limits<T>::digits; for (unsigned int i = 0; i < bits; i++)
digits.insert(digits.begin(), (n & (1 << i)) ?
'1' : '0');

How about calling digits.reserve( bits ) before the loop?
// digits.insert(digits.begin(), 'E'); // tests exception
if (digits.size() != bits)
throw bad_size;

This is unnecessary, methinks. It's an internal error that should not
be passed to the user. Use assert instead. If string::reserve() [or
string::insert(), if you don't use reserve] fails, it will throw its
own exception that the user does need to handle because it's beyond the
scope of your class.
}

const bool test(const unsigned int n) const
{
if (n > digits.size() || n == 0)

Many application domains start counting at bit 0 rather than 1. Perhaps
you should allow the user to choose which is the starting bit.
throw bad_range;

return (digits[n - 1] == '1') ? true : false;
}

void set(const unsigned int n)
{
if (n > digits.size() || n == 0)
throw bad_range;

digits[n - 1] = '1';
}

friend std::eek:stream& operator<<(std::eek:stream& stream, const
binary& bin) {
return stream << bin.digits;
}

private:
std::string digits;
};

#endif // __BINARY__

So anyway, what's the problem with std::bitset? Compare binary() in
_TC++PL_ 3rd ed., section 17.5.3.3.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top