473,324 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

New and improved binary class

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::ostream& operator<<(std::ostream& stream, const
binary& bin) {
return stream << bin.digits;
}

private:
std::string digits;
};

#endif // __BINARY__
--
http://www.munted.org.uk

Take a nap, it saves lives.
Jun 2 '06 #1
1 1853
Alex Buell wrote:
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....eba91ee4bd9466
#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::ostream& operator<<(std::ostream& 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

Jun 2 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: someone | last post by:
Suppose that I have a class in an assembly that is delivered to the user, what can I do to change the class so that it doesn't break the binary compatibility? That is, user application can run...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
11
by: ajou_king | last post by:
I was running some tests on my Win32 1GHZ processor to see how long it would take to transmit objects numerous times via TCP/IP using C# ..NET Remoting vs the C++ trustworthy method of binary...
21
by: nicolasg | last post by:
does anyone know a module or something to convert numbers like integer to binary format ? for example I want to convert number 7 to 0111 so I can make some bitwise operations... Thanks
15
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with...
1
by: Michael | last post by:
I have a solution for this, but it feels wrong. If anyone could offer a better one, I'm all ears. (Or technically, eyes.) Basically, I have a bunch of classes. For concreteness, one is a...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
12
by: =?Utf-8?B?enRSb24=?= | last post by:
Hi all, I recently came across something really strange and after a couple of days of debugging, I finally nailed the cause of it. However, I have absolutely no idea what I am doing wrong or is...
5
by: Canned | last post by:
Hi, I'm trying to write a class that can convert ascii to binary and vice versa. I write my class based on this function I've found on internet That works perfectly, but when I try to implement...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.