473,325 Members | 2,712 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,325 software developers and data experts.

C++ Primer ex 3.24 (bitset class)

i have solved the problem. any comments on it:

/* C++ Primer 4/e
* chapter 3
*
* exercise 3.24
* STATEMENT:
* consider the sequence 1,2,3,5,13,21. Initialize a "bitset<32>"
object that has a one bit in each position corresponding to a number in
this sequence. */

#include <iostream>
#include <string>
#include <bitset>

int main()
{
std::string bstr("100000001000010010111"); std::bitset<32>
str_bit(bstr);

/* i could have created an empty bitset first and then assign
the values using subscripting but the problem statement requires that
initialisation must set the bits at positions 1,2,3,4,13,21 as ON. so i
used the string as initialisation */

std::cout << "printing bits" << std::endl; for(size_t ix=0; ix !=
str_bit.size(); ++ix)
{
std::cout << "position "
<< ix + 1
<< " : "
<< str_bit[ix]
<< std::endl;
}

return 0;
}
========= OUTPUT =============
{arnuld@arch cpp }% g++ -ansi -pedantic -Wall -Wextra ex_03-24.cpp
{arnuld@arch cpp }% ./a.out
printing bits
position 1 : 1
position 2 : 1
position 3 : 1
position 4 : 0
position 5 : 1
position 6 : 0
position 7 : 0
position 8 : 1
position 9 : 0
position 10 : 0
position 11 : 0
position 12 : 0
position 13 : 1
position 14 : 0
position 15 : 0
position 16 : 0
position 17 : 0
position 18 : 0
position 19 : 0
position 20 : 0
position 21 : 1
position 22 : 0
position 23 : 0
position 24 : 0
position 25 : 0
position 26 : 0
position 27 : 0
position 28 : 0
position 29 : 0
position 30 : 0
position 31 : 0
position 32 : 0
{arnuld@arch cpp }%

--
-- http://arnuld.blogspot.com

Jul 20 '07 #1
5 2104
On 2007-07-20 09:23, arnuld wrote:
i have solved the problem. any comments on it:

/* C++ Primer 4/e
* chapter 3
*
* exercise 3.24
* STATEMENT:
* consider the sequence 1,2,3,5,13,21. Initialize a "bitset<32>"
object that has a one bit in each position corresponding to a number in
this sequence. */

#include <iostream>
#include <string>
#include <bitset>

int main()
{
std::string bstr("100000001000010010111"); std::bitset<32>
str_bit(bstr);

/* i could have created an empty bitset first and then assign
the values using subscripting but the problem statement requires that
initialisation must set the bits at positions 1,2,3,4,13,21 as ON. so i
used the string as initialisation */

std::cout << "printing bits" << std::endl; for(size_t ix=0; ix !=
str_bit.size(); ++ix)
{
std::cout << "position "
<< ix + 1
<< " : "
<< str_bit[ix]
<< std::endl;
}

return 0;
}
A matter of taste perhaps but I find it slightly more readable to use
bitwise operations and shifting to initialise the bitset:

#include <iostream>
#include <bitset>

int main()
{
// Set bits nr. 1, 2, 3, 5, 13, and 21
std::bitset<32str_bit(1<<1 | 1<<2 | 1<<3 | 1<<5 | 1<<13 | 1<<21);
std::cout str_bit;
return 0;
}

The advantage being that it's explicit what bit I'm setting, I don't
have to count the number of characters in a string, which might be a
blessing if I ever need to change anything.

--
Erik Wikström
Jul 20 '07 #2
Erik Wikström wrote:
On 2007-07-20 09:23, arnuld wrote:
>>
#include <iostream>
#include <string>
#include <bitset>

int main()
{
std::string bstr("100000001000010010111"); std::bitset<32>
str_bit(bstr);

/* i could have created an empty bitset first and then assign
the values using subscripting but the problem statement requires that
initialisation must set the bits at positions 1,2,3,4,13,21 as ON. so i
used the string as initialisation */

std::cout << "printing bits" << std::endl; for(size_t ix=0; ix !=
str_bit.size(); ++ix)
{
std::cout << "position "
<< ix + 1
<< " : "
<< str_bit[ix]
<< std::endl;
}

return 0;
}

A matter of taste perhaps but I find it slightly more readable to use
bitwise operations and shifting to initialise the bitset:

#include <iostream>
#include <bitset>

int main()
{
// Set bits nr. 1, 2, 3, 5, 13, and 21
std::bitset<32str_bit(1<<1 | 1<<2 | 1<<3 | 1<<5 | 1<<13 | 1<<21);
std::cout str_bit;
return 0;
}

The advantage being that it's explicit what bit I'm setting, I don't
have to count the number of characters in a string, which might be a
blessing if I ever need to change anything.
You also set the correct bits, the original was off by one.

--
Ian Collins.
Jul 20 '07 #3
On Fri, 20 Jul 2007 08:45:41 +0000, Erik Wikström wrote:
A matter of taste perhaps but I find it slightly more readable to use
bitwise operations and shifting to initialise the bitset:

#include <iostream>
#include <bitset>

int main()
{
// Set bits nr. 1, 2, 3, 5, 13, and 21
std::bitset<32str_bit(1<<1 | 1<<2 | 1<<3 | 1<<5 | 1<<13 | 1<<21);
std::cout str_bit;
return 0;
}

The advantage being that it's explicit what bit I'm setting, I don't
have to count the number of characters in a string, which might be a
blessing if I ever need to change anything.
yep, that is really good looking, short and expressive :-) but C++ Primer
didnot tell me anything about "n << m" yet. i think it is called
operator-overloading.

--
-- http://arnuld.blogspot.com

Jul 20 '07 #4
"arnuld" <ge*********@gmail.comwrote in message
news:pa****************************@gmail.com...
>On Fri, 20 Jul 2007 08:45:41 +0000, Erik Wikström wrote:
A matter of taste perhaps but I find it slightly more readable to use
bitwise operations and shifting to initialise the bitset:

#include <iostream>
#include <bitset>

int main()
{
// Set bits nr. 1, 2, 3, 5, 13, and 21
std::bitset<32str_bit(1<<1 | 1<<2 | 1<<3 | 1<<5 | 1<<13 | 1<<21);
std::cout str_bit;
return 0;
}

The advantage being that it's explicit what bit I'm setting, I don't
have to count the number of characters in a string, which might be a
blessing if I ever need to change anything.

yep, that is really good looking, short and expressive :-) but C++ Primer
didnot tell me anything about "n << m" yet. i think it is called
operator-overloading.
Actually, every other use of << other than bit shifting is operator
overloading. The << and >keywords are bitwise shifting. They come from C
and predate C++'s overloaded use of them.

1 << 1; means, shift the bit value 1 left 1 bit. 001 becomes 010
1 << 2; means, shift the bit value 1 left 2 bits. 001 becomes 100
etc.
>shifts them to the right.


Jul 20 '07 #5
arnuld wrote:
::: On Fri, 20 Jul 2007 08:45:41 +0000, Erik Wikström wrote:
::: A matter of taste perhaps but I find it slightly more readable to
::: use bitwise operations and shifting to initialise the bitset:
:::
::: #include <iostream>
::: #include <bitset>
:::
::: int main()
::: {
::: // Set bits nr. 1, 2, 3, 5, 13, and 21
::: std::bitset<32str_bit(1<<1 | 1<<2 | 1<<3 | 1<<5 | 1<<13 |
::: 1<<21); std::cout str_bit;
::: return 0;
::: }
:::
::: The advantage being that it's explicit what bit I'm setting, I
::: don't have to count the number of characters in a string, which
::: might be a blessing if I ever need to change anything.
::
:: yep, that is really good looking, short and expressive :-) but C++
:: Primer didnot tell me anything about "n << m" yet. i think it is
:: called operator-overloading.

It is shorter and correct, but also limited to bits that fit in a
value of type long. The string you used could theoretically be much
longer. (Not that it would make it any easier to count the positions
right :-).
Bo Persson
Jul 20 '07 #6

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

Similar topics

29
by: shaun roe | last post by:
I want something which is very like a bitset<64> but with a couple of extra functions: set/get the two 32 bit words, and conversion to unsigned long long. I can do this easily by inheriting from...
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...
10
by: woessner | last post by:
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......
6
by: zacariaz | last post by:
The subject isnt very clear, but ill do my best to explain. In a class in need a bitset which size is defined by the constructor, e.g. something like this: class Example { std::bitset<?>Bs;...
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; //...
3
by: Guy.Tristram | last post by:
Is there any good reason operator< is not defined for std::bitset? It seems to me that: 1 - it would be useful. 2 - it is easy to implement inside the class template. 3 - it is impossible to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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: 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: 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.