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("100000001 000010010111"); 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 5 2124
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("100000001 000010010111"); 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<32s tr_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
Erik Wikström wrote:
On 2007-07-20 09:23, arnuld wrote:
>> #include <iostream> #include <string> #include <bitset>
int main() { std::string bstr("100000001 000010010111"); 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 initialisati on 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<32s tr_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.
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<32s tr_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
"arnuld" <ge*********@gm ail.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<32s tr_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.
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<32s tr_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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 bitset<64>, but I know that STL
has no virtual destructor. Can I get around this by calling the
baseclass destructor explicitly in my derived class?
Is there another way to get all of the bitset<64> functionality without
rewriting a lot of...
|
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():
"...each character is 1 if the corresponding bit is set, and 0 if it is
not. In general, character position i corresponds to bit position N - 1 -
|
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 it gets
slower and slower, I'm looking to make it faster.
Thanks
|
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...
inadequacies of the compiler), so it needs to have approximately the
same interface.
Thanks in advance,
Bill
|
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;
public:
Example(int input) {
? = input;
| |
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 Library container.
Actaully what's so special in STL containers?
What I know is that there will be certain operators overloaded,
supports template meta programming, having iterators, compatible with
|
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>
|
by: swcamry |
last post by:
class bitset::reference {
friend class bitset;
reference(); // no public
constructor
public:
~reference();
operator bool () const; // convert to bool
reference& operator= ( bool x ); // assign from bool
reference& operator= ( const reference& x ); // assign from bit
reference& flip(); // flip bit value
|
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 implement efficiently (for bitsets too large
for to_ulong) outside of the class.
For now I will use boost::dynamic_bitset, which does implement it.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |