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

some questions about "bitset" class

i am confused on some aspects of bitset class:

/* C++ Primer 4/e
* chapter 3
*
* exercise 3.23
*
*/

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

int main()
{
std::bitset<64bitvec(32);
std::bitset<32bv(1010101);

std::string bstr;
std::bitset<8bsv(bstr);

std::cout << "std::bitset<64bitvec(32): " << bitvec << std::endl;
std::cout << "std::bitset<32bv(1010101): " << bv << std::endl;
std::cout << "std::bitset<8bsv(bstr): " << bsv << std::endl;

std::cout << "\nprinting bits of \"std::bitset<64bitvec(32)\": " <<
std::endl; for(size_t ix=0; ix != bitvec.size(); ++ix)
{
std::cout << bitvec[ix] << " ";
}

std::cout << std::endl;

return 0;
}
======== OUTPUT ============
{arnuld@arch cpp }% g++ -ansi -pedantic -Wall -Wextra ex_03-23.cpp
{arnuld@arch cpp }% ./a.out
std::bitset<64bitvec(32):
00000000000000000000000000000000000000000000000000 00000000100000
std::bitset<32bv(1010101): 00000000000011110110100110110101
std::bitset<8bsv(bstr): 00000000

printing bits of "std::bitset<64bitvec(32)": 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
{arnuld@arch cpp }%
(1) "std::bitset<64bitvec(32)" is representing integer 32 in 64
bit-pattern, right?

compare (1) this with (2):

(2) in "bitset<nb", "b" has n bits, each bit is zero. In "bitset<n>
b(u)", "b" is a copy of the unsigned long value u, then what is the use of
"n" ?
(3) the output of "std::bitset<32bv(1010101)" has no "1010101"
bit-pattern in it. why ? Is it printing the "1010101" in 32 bit-pattern.

(4) "std::bitset<64bitvec(32)" has 2 outputs, one is direct using
"std::cout" and another is using "for" loop. why the output is reversed in
these 2 cases? which one output is the correct representation of
bit-pattern ?
--
-- http://arnuld.blogspot.com

Jul 20 '07 #1
2 2698
On 2007-07-20 09:09, arnuld wrote:
i am confused on some aspects of bitset class:

/* C++ Primer 4/e
* chapter 3
*
* exercise 3.23
*
*/

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

int main()
{
std::bitset<64bitvec(32);
std::bitset<32bv(1010101);

std::string bstr;
std::bitset<8bsv(bstr);

std::cout << "std::bitset<64bitvec(32): " << bitvec << std::endl;
std::cout << "std::bitset<32bv(1010101): " << bv << std::endl;
std::cout << "std::bitset<8bsv(bstr): " << bsv << std::endl;

std::cout << "\nprinting bits of \"std::bitset<64bitvec(32)\": " <<
std::endl; for(size_t ix=0; ix != bitvec.size(); ++ix)
{
std::cout << bitvec[ix] << " ";
}

std::cout << std::endl;

return 0;
}
======== OUTPUT ============
{arnuld@arch cpp }% g++ -ansi -pedantic -Wall -Wextra ex_03-23.cpp
{arnuld@arch cpp }% ./a.out
std::bitset<64bitvec(32):
00000000000000000000000000000000000000000000000000 00000000100000
std::bitset<32bv(1010101): 00000000000011110110100110110101
std::bitset<8bsv(bstr): 00000000

printing bits of "std::bitset<64bitvec(32)": 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
{arnuld@arch cpp }%
(1) "std::bitset<64bitvec(32)" is representing integer 32 in 64
Yes
compare (1) this with (2):

(2) in "bitset<nb", "b" has n bits, each bit is zero. In "bitset<n>
b(u)", "b" is a copy of the unsigned long value u, then what is the use of
"n" ?
Set u = 1, then there are lots of ways to store this value. You can use
a bitset with only 1 bit (std::bitset<1b1(1);) or you can use 10 bits
(std::bitset<10b10(1);). The first will have the binary representation
"1", the second "0000000001". The value supplied (u) is only the initial
value while the number of bits (n) determine the number of different
values possible to store.
(3) the output of "std::bitset<32bv(1010101)" has no "1010101"
bit-pattern in it. why ? Is it printing the "1010101" in 32 bit-pattern.
Because the value (u) is a long, and the decimal number 1010101 does not
have the binary representation 1010101, but rather the one printed by
your code.
(4) "std::bitset<64bitvec(32)" has 2 outputs, one is direct using
"std::cout" and another is using "for" loop. why the output is reversed in
these 2 cases? which one output is the correct representation of
bit-pattern ?
It depends on in which end of the output you put the most significant
digit. Normally we put the most significant digit to the left (first)
and the least significant digit to the right (last) (such as in the
number 1234, the digit telling how many thousands is to the left) and
this is what std::cout do. With your loop however you put the least
significant digit first and the most significant digit last.

--
Erik Wikström
Jul 20 '07 #2
arnuld wrote:

<snip>
>
======== OUTPUT ============
{arnuld@arch cpp }% g++ -ansi -pedantic -Wall -Wextra ex_03-23.cpp
{arnuld@arch cpp }% ./a.out
std::bitset<64bitvec(32):
00000000000000000000000000000000000000000000000000 00000000100000
std::bitset<32bv(1010101): 00000000000011110110100110110101
std::bitset<8bsv(bstr): 00000000

printing bits of "std::bitset<64bitvec(32)": 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
{arnuld@arch cpp }%
(1) "std::bitset<64bitvec(32)" is representing integer 32 in 64
bit-pattern, right?
Yes, the << operator outputs the bits left to right.
compare (1) this with (2):

(2) in "bitset<nb", "b" has n bits, each bit is zero. In "bitset<n>
b(u)", "b" is a copy of the unsigned long value u, then what is the use of
"n" ?
Bits can be set or shifted into the hight positions.
>
(3) the output of "std::bitset<32bv(1010101)" has no "1010101"
bit-pattern in it. why ? Is it printing the "1010101" in 32 bit-pattern.
You forgot to quote the string.
(4) "std::bitset<64bitvec(32)" has 2 outputs, one is direct using
"std::cout" and another is using "for" loop. why the output is reversed in
these 2 cases? which one output is the correct representation of
bit-pattern ?
They both are, the former outputs the MSB first, in the latter, you
output the LSB first.

--
Ian Collins.
Jul 20 '07 #3

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

Similar topics

3
by: Jason | last post by:
hello, I've been looking into the idea of using a bitset to store a collection of fields efficiently in a data structure that uses a large number of elements. My question is what technique is...
1
by: Filip Kasperkiewicz | last post by:
Hi ! I need some examples of usage <bitset> class. If you can help me write filipoNO_SPAM@benchmark.pl. Thanks for all help Philip Kasperkiewicz
9
by: Rich Herrick | last post by:
The latest version of my "Pascal-like" set class is available here: http://www.richherrick.com/software/herrick-1.0.zip Those from the old YAHOO BOOST forum might remember it from several...
2
by: Michael Olea | last post by:
Hiya. Frequently (as in at least twice now) I find that when I write some sort of container I want at least two out of three possible versions: o fixed max size known at compile time o fixed...
6
by: tmartsum | last post by:
I have a discussion in comp.std.c++ After a (bit stupid) suggestion on representing a fixed 'big' length int I moderated it to "Bitfields-ints should be allowed to have any fixed length" I...
19
by: felixnielsen | last post by:
Some might remember that i, not so long ago, started a treath or two about a weird 3d labyrinth. I now have a working code, that i want to share, hear comments, advice, ect., but first let me...
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...
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...
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
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
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.