- Bitset Initialization - 4 Updates
- A "better" C++ - 5 Updates
MikeCopeland <mrc2323@cox.net>: Nov 29 01:27PM -0700 Is there a way to initialize a non-contiguous number of bits in a std::bitset (other than with a string of 0 or 1 bits with those desired bits as 1)? That is, I have a declration of: std::bitset<100> myBits; and I want to set the following bits: 7, 9, 43, 47, 51-59, 66, 67 I can, of course, construct a string constant of these bits, but it's tedious to code and completely lacking in good documentation... Is there an easy way to do so in source code? TIA --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus |
BartC <bc@freeuk.com>: Nov 29 09:19PM On 29/11/2015 20:27, MikeCopeland wrote: > I can, of course, construct a string constant of these bits, but it's > tedious to code and completely lacking in good documentation... > Is there an easy way to do so in source code? TIA I just saw this post by chance and know nothing of the capabilities of C++. But your requirement reminds me of the Pascal way to do the same: var myBits: set of 1..100; myBits := [7, 9, 43, 47, 51..59, 66, 67]; So exactly as you've denoted except for using .. for the range (I assume you don't mean -8). This can serve as a target anyway. -- Bartc |
Paavo Helde <myfirstname@osa.pri.ee>: Nov 29 04:07PM -0600 MikeCopeland <mrc2323@cox.net> wrote in news:MPG.30c50d893dd76c039896b7 > and I want to set the following bits: > 7, 9, 43, 47, 51-59, 66, 67 > I can, of course, construct a string constant of these bits, but it's > tedious to code and completely lacking in good documentation... > Is there an easy way to do so in source code? TIA Not quite one line, but close (C++11): #include <iostream> #include <bitset> int main() { std::bitset<100> mybitset; for (auto bit: {7, 9, 43, 47, 51, 52, 53, 54, 55, 56, 57, 58, 59, 66, 67}) { mybitset.set(bit); } std::cout << mybitset.to_string() << "\n"; } or more fancy & verbose: #include <iostream> #include <numeric> #include <bitset> #include <iterator> int main() { using b100 = std::bitset<100>; const int bits[] = {7, 9, 43, 47, 51, 52, 53, 54, 55, 56, 57, 58, 59, 66, 67}; b100 mybitset = std::accumulate(std::begin(bits), std::end(bits), b100(), [](b100 bset, int bit) {bset.set(bit); return bset; }); std::cout << mybitset.to_string() << "\n"; } |
Geoff <geoff@invalid.invalid>: Nov 29 03:31PM -0800 On Sun, 29 Nov 2015 13:27:11 -0700, MikeCopeland <mrc2323@cox.net> wrote: > I can, of course, construct a string constant of these bits, but it's >tedious to code and completely lacking in good documentation... > Is there an easy way to do so in source code? TIA I don't see a way to express the range 51-59 but here's my solution: #include <iostream> #include <bitset> int main (void) { std::bitset<100> myBits; const int mySets[] = {7, 9, 43, 47, 51, 52, 53, 54, 55, 56, 57, 58, 59, 66, 67}; for (int i = 0; i < sizeof(mySets)/sizeof(int); i++) myBits.set(mySets[i]); std::cout << "Bits "; for (size_t i = 0; i < myBits.size(); i++) { if (myBits.test(i)) { std::cout << i << " "; } } std::cout << "are set." << std::endl; } |
Lynn McGuire <lmc@winsim.com>: Nov 29 01:42PM -0600 On 8/19/2015 3:46 AM, Juha Nieminen wrote: > moderately successful, the vast majority of them have been forgotten. > But they are still better than C++, dammit! > --- news://freenews.netfront.net/ - complaints: news@netfront.net --- C was the first computer language to successfully change from line oriented (Fortran, Cobol, etc) to byte oriented. And, then C++ jumped from pure procedural to object oriented. All in all, good enough. And the high speed of the resultant programs is a big plus. Lynn |
Bo Persson <bop@gmb.dk>: Nov 29 11:14PM +0100 On 2015-11-29 20:42, Lynn McGuire wrote: > oriented (Fortran, Cobol, etc) to byte oriented. And, then C++ jumped > from pure procedural to object oriented. All in all, good enough. And > the high speed of the resultant programs is a big plus. No, it wasn't. C++ is inspired by the classes of Simula 67, which is an extension of Algol 60. Bo Persson |
Lynn McGuire <lmc@winsim.com>: Nov 29 04:22PM -0600 On 11/29/2015 4:14 PM, Bo Persson wrote: > C++ is inspired by the classes of Simula 67, which is an extension of > Algol 60. > Bo Persson Sorry, Algol was not a successful computer language. Lynn |
Robert Wessel <robertwessel2@yahoo.com>: Nov 29 04:32PM -0600 On Sun, 29 Nov 2015 16:22:39 -0600, Lynn McGuire <lmc@winsim.com> wrote: >> Algol 60. >> Bo Persson >Sorry, Algol was not a successful computer language. Sure it was. Just not in the US. |
Lynn McGuire <lmc@winsim.com>: Nov 29 05:00PM -0600 On 11/29/2015 4:32 PM, Robert Wessel wrote: >>> Bo Persson >> Sorry, Algol was not a successful computer language. > Sure it was. Just not in the US. I don't even regard Pascal as a successful language and I wrote software in Turbo Pascal for half a decade. Lynn |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment