473,385 Members | 1,372 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,385 software developers and data experts.

Bitset Replacement

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
10 2271
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
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
> > 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
* 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
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
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
<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.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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hunter Hou | last post by:
Hello, I have this very simple program, but it can't be compiled. What's wrong here? thanks, hunter #include <iostream>
2
by: shaun roe | last post by:
As a follow up to my question about STL and bitset<64> I'd like to share a quirk (bug?) about unsigned long long support and the bitset. I'm using gcc 3.2 on linux or gcc 3.3 on mac, the answer is...
5
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): ...
5
by: Rich S. | last post by:
Hi, Is the code below the best way to have the less than function for an 80 bit bitset or is there something faster / better? When I populate this map with millions (... and millions) of sets...
14
by: Haro Panosyan | last post by:
How to construct a bitset from string? For example: std::bitset<16> b16("1011011110001011"); Is this possible? Thanks in advance. -haro
3
by: shaun | last post by:
I have a function for returning the value of a bit field in a number: template<typename T> T bitfield(const T num, const unsigned int bitStart, const unsigned int bitEnd){ T mask,...
4
by: Sarath | last post by:
>From the documentation of MSDN, it is saying that bitset is not a STL container Unlike the similar vector<boolClass, the bitset class does not have iterators and is not an Standard Template...
2
by: arnuld | last post by:
i am confused on some aspects of bitset class: /* C++ Primer 4/e * chapter 3 * * exercise 3.23 * */ #include <iostream>
5
by: swcamry | last post by:
class bitset::reference { friend class bitset; reference(); // no public constructor public: ~reference(); operator bool () const; //...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.