Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C++ Primer ex 3.24 (bitset class) (http://www.velocityreviews.com/forums/t523476-c-primer-ex-3-24-bitset-class.html)

arnuld 07-20-2007 07:23 AM

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


=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= 07-20-2007 08:45 AM

Re: C++ Primer ex 3.24 (bitset class)
 
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<32> str_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

Ian Collins 07-20-2007 08:49 AM

Re: C++ Primer ex 3.24 (bitset class)
 
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<32> str_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.

arnuld 07-20-2007 09:40 AM

Re: C++ Primer ex 3.24 (bitset class)
 
> 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<32> str_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


Jim Langston 07-20-2007 10:14 AM

Re: C++ Primer ex 3.24 (bitset class)
 
"arnuld" <geek.arnuld@gmail.com> wrote in message
news:pan.2007.07.20.09.40.33.685067@gmail.com...
>> On Fri, 20 Jul 2007 08:45:41 +0000, Erik Wikstrm 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<32> str_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.





Bo Persson 07-20-2007 01:25 PM

Re: C++ Primer ex 3.24 (bitset class)
 
arnuld wrote:
::: On Fri, 20 Jul 2007 08:45:41 +0000, Erik Wikstrm 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<32> str_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




All times are GMT. The time now is 05:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.