By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Bitset Replacement

P: n/a
I'm using a C++ compiler on a DSP that doesn't provide the bitset
class. Can anyone suggest a replacement for it? I've written a lot of
code that uses the bitset class (before I knew about the...
inadequacies of the compiler), so it needs to have approximately the
same interface.

Thanks in advance,
Bill

Jun 12 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On 12 Jun 2006 07:51:52 -0700, I waved a wand and this message
magically appeared from wo******@gmail.com:
I'm using a C++ compiler on a DSP that doesn't provide the bitset
class. Can anyone suggest a replacement for it? I've written a lot
of code that uses the bitset class (before I knew about the...
inadequacies of the compiler), so it needs to have approximately the
same interface.


You're in luck, here's something you might can use. I hereby place this
code into the public domain with this post.

#ifndef __BINARY__
#define __BINARY__

#include <iostream>
#include <string>
#include <limits>
#include <assert.h>

class binary
{
public:
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');

assert(digits.size() == bits);
}

const bool test(const unsigned int n) const
{
assert(n < digits.size() + 1);
return (digits[n - 1] == '1') ? true : false;
}

void set(const unsigned int n)
{
assert(n < digits.size() + 1);
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 12 '06 #2

P: n/a
wo******@gmail.com wrote:
I'm using a C++ compiler on a DSP that doesn't provide the bitset
class. Can anyone suggest a replacement for it? I've written a lot
of code that uses the bitset class (before I knew about the...
inadequacies of the compiler), so it needs to have approximately the
same interface.


Uh... Get a free library implementation, like STLport or GNU, and
"re-use" their bitset. AFAICT, most if not all of it is in the
header. If your compiler doesn't have it, maybe it still can compile
the one others have implemented...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 12 '06 #3

P: n/a
> > I'm using a C++ compiler on a DSP that doesn't provide the bitset
class. Can anyone suggest a replacement for it? I've written a lot
of code that uses the bitset class (before I knew about the...
inadequacies of the compiler), so it needs to have approximately the
same interface.
Uh... Get a free library implementation, like STLport or GNU, and
"re-use" their bitset. AFAICT, most if not all of it is in the
header. If your compiler doesn't have it, maybe it still can compile
the one others have implemented...


Yes, that would be an ideal solution. And I have tried that, with the
bitset header from both Visual C++ and g++. But both of them depend on
other definitions in the vendor's library implementation. And I don't
think it would be a good idea to start copying more and more pieces of
the library in to my project.

However, I have not checked out STLport. I'll look in to that right
now. Thanks for the suggestion.

--Bill

Jun 12 '06 #4

P: n/a
* wo******@gmail.com:
I'm using a C++ compiler on a DSP that doesn't provide the bitset
class. Can anyone suggest a replacement for it? I've written a lot of
code that uses the bitset class (before I knew about the...
inadequacies of the compiler), so it needs to have approximately the
same interface.


It's a bit ironic to lack bit handling functionality on a bit cruncher.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 12 '06 #5

P: n/a
wo******@gmail.com wrote:
I'm using a C++ compiler on a DSP that doesn't provide the bitset
class. Can anyone suggest a replacement for it? I've written a lot
of code that uses the bitset class (before I knew about the...
inadequacies of the compiler), so it needs to have approximately the
same interface.

Uh... Get a free library implementation, like STLport or GNU, and
"re-use" their bitset. AFAICT, most if not all of it is in the
header. If your compiler doesn't have it, maybe it still can compile
the one others have implemented...


Yes, that would be an ideal solution. And I have tried that, with the
bitset header from both Visual C++ and g++. But both of them depend on
other definitions in the vendor's library implementation. And I don't
think it would be a good idea to start copying more and more pieces of
the library in to my project.

However, I have not checked out STLport. I'll look in to that right
now. Thanks for the suggestion.


FYI, I have successfully used portions of STLport (which I ported -- no
changes to the code beyond configuring the build options) on a TI DSP.

Cheers! --M

Jun 12 '06 #6

P: n/a
Alex Buell wrote:
On 12 Jun 2006 07:51:52 -0700, I waved a wand and this message
magically appeared from wo******@gmail.com:
I'm using a C++ compiler on a DSP that doesn't provide the bitset
class. Can anyone suggest a replacement for it? I've written a lot
of code that uses the bitset class (before I knew about the...
inadequacies of the compiler), so it needs to have approximately the
same interface.
You're in luck, here's something you might can use. I hereby place this
code into the public domain with this post.

#ifndef __BINARY__
#define __BINARY__


Uhh, you *still* can't use names starting with double underscores, as
they are reserved for the implementation. Yes, it might not cause a
problem on your system, but think about all the other folks who might
use your library.
#include <iostream>
#include <string>
#include <limits>
#include <assert.h>
If he doesn't have <bitset>, I'd guess he lacks the other standard
headers, too. BTW, you c/should use <cassert> instead of <assert.h>.

class binary
{
public:
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');
To reiterate from the last time you posted this code: you should use
reserve() here to prevent reallocation inefficiencies.

assert(digits.size() == bits);
}

const bool test(const unsigned int n) const
{
assert(n < digits.size() + 1);
return (digits[n - 1] == '1') ? true : false;
}

void set(const unsigned int n)
{
assert(n < digits.size() + 1);
digits[n - 1] = '1';
}

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

private:
std::string digits;
Using a std::string is perhaps efficient for printing, but I'm guessing
the code the OP has written is more concerned with bit-twiddling and so
forth where this wouldn't likely be efficient since he'll need the
value back as a number eventually. Moreover, DSPs generally don't excel
at string handling, and so that functionality is often put elsewhere in
the system.
};

#endif // __BINARY__


Cheers! --M

Jun 12 '06 #7

P: n/a
<wo******@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
> I'm using a C++ compiler on a DSP that doesn't provide the bitset
> class. Can anyone suggest a replacement for it? I've written a lot
> of code that uses the bitset class (before I knew about the...
> inadequacies of the compiler), so it needs to have approximately the
> same interface.
Uh... Get a free library implementation, like STLport or GNU, and
"re-use" their bitset. AFAICT, most if not all of it is in the
header. If your compiler doesn't have it, maybe it still can compile
the one others have implemented...


Yes, that would be an ideal solution. And I have tried that, with the
bitset header from both Visual C++ and g++.


Uh... The suggestion was that you get a *free* library implementation.
VC++ is a commercial product that you license. Even when you don't pay
for a copy of it, it's still covered by that license. So you're not free
to do as you please with it. (You might take a look at the copyright
notice on the header files.)
But both of them depend on
other definitions in the vendor's library implementation. And I don't
think it would be a good idea to start copying more and more pieces of
the library in to my project.


Agreed, for a host of reasons.

Having said all that, I suspect your best bet would be to get a truly
"free" copy of bitset, as from STLport or libstdc++, and cauterize
the various loose ends.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jun 12 '06 #8

P: n/a

P.J. Plauger wrote:
<wo******@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
> I'm using a C++ compiler on a DSP that doesn't provide the bitset
> class. Can anyone suggest a replacement for it? I've written a lot
> of code that uses the bitset class (before I knew about the...
> inadequacies of the compiler), so it needs to have approximately the
> same interface.

Uh... Get a free library implementation, like STLport or GNU, and
"re-use" their bitset. AFAICT, most if not all of it is in the
header. If your compiler doesn't have it, maybe it still can compile
the one others have implemented...


Yes, that would be an ideal solution. And I have tried that, with the
bitset header from both Visual C++ and g++.


Uh... The suggestion was that you get a *free* library implementation.
VC++ is a commercial product that you license. Even when you don't pay
for a copy of it, it's still covered by that license. So you're not free
to do as you please with it. (You might take a look at the copyright
notice on the header files.)


The license grants you permission to use in and link with your
products. The compiler pulls out just what you use...I don't see the
difference if you do that by hand to get a particular job done. It is
a totally legitamate thing to do.

Jun 12 '06 #9

P: n/a
wo******@gmail.com wrote:
I'm using a C++ compiler on a DSP that doesn't provide the bitset
class. Can anyone suggest a replacement for it? I've written a lot of
code that uses the bitset class (before I knew about the...
inadequacies of the compiler), so it needs to have approximately the
same interface.

Thanks in advance,
Bill


std::vector<bool> perhaps?
Jun 12 '06 #10

P: n/a

"Noah Roberts" <ro**********@gmail.com> skrev i meddelandet
news:11*********************@f6g2000cwb.googlegrou ps.com...

P.J. Plauger wrote:
<wo******@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
>> > I'm using a C++ compiler on a DSP that doesn't provide the
>> > bitset
>> > class. Can anyone suggest a replacement for it? I've written
>> > a lot
>> > of code that uses the bitset class (before I knew about the...
>> > inadequacies of the compiler), so it needs to have
>> > approximately the
>> > same interface.
>
>> Uh... Get a free library implementation, like STLport or GNU,
>> and
>> "re-use" their bitset. AFAICT, most if not all of it is in the
>> header. If your compiler doesn't have it, maybe it still can
>> compile
>> the one others have implemented...
>
> Yes, that would be an ideal solution. And I have tried that,
> with the
> bitset header from both Visual C++ and g++.


Uh... The suggestion was that you get a *free* library
implementation.
VC++ is a commercial product that you license. Even when you don't
pay
for a copy of it, it's still covered by that license. So you're not
free
to do as you please with it. (You might take a look at the
copyright
notice on the header files.)


The license grants you permission to use in and link with your
products. The compiler pulls out just what you use...I don't see
the
difference if you do that by hand to get a particular job done. It
is
a totally legitamate thing to do.


Obviously not, if you use it with a different C++ compiler. :-)
Bo Persson
Jun 12 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.