A
arnuld
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<64> bitvec(32);
std::bitset<32> bv(1010101);
std::string bstr;
std::bitset<8> bsv(bstr);
std::cout << "std::bitset<64> bitvec(32): " << bitvec << std::endl;
std::cout << "std::bitset<32> bv(1010101): " << bv << std::endl;
std::cout << "std::bitset<8> bsv(bstr): " << bsv << std::endl;
std::cout << "\nprinting bits of \"std::bitset<64> bitvec(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<64> bitvec(32):
0000000000000000000000000000000000000000000000000000000000100000
std::bitset<32> bv(1010101): 00000000000011110110100110110101
std::bitset<8> bsv(bstr): 00000000
printing bits of "std::bitset<64> bitvec(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<64> bitvec(32)" is representing integer 32 in 64
bit-pattern, right?
compare (1) this with (2):
(2) in "bitset<n> b", "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<32> bv(1010101)" has no "1010101"
bit-pattern in it. why ? Is it printing the "1010101" in 32 bit-pattern.
(4) "std::bitset<64> bitvec(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 ?
/* C++ Primer 4/e
* chapter 3
*
* exercise 3.23
*
*/
#include <iostream>
#include <string>
#include <bitset>
int main()
{
std::bitset<64> bitvec(32);
std::bitset<32> bv(1010101);
std::string bstr;
std::bitset<8> bsv(bstr);
std::cout << "std::bitset<64> bitvec(32): " << bitvec << std::endl;
std::cout << "std::bitset<32> bv(1010101): " << bv << std::endl;
std::cout << "std::bitset<8> bsv(bstr): " << bsv << std::endl;
std::cout << "\nprinting bits of \"std::bitset<64> bitvec(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<64> bitvec(32):
0000000000000000000000000000000000000000000000000000000000100000
std::bitset<32> bv(1010101): 00000000000011110110100110110101
std::bitset<8> bsv(bstr): 00000000
printing bits of "std::bitset<64> bitvec(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<64> bitvec(32)" is representing integer 32 in 64
bit-pattern, right?
compare (1) this with (2):
(2) in "bitset<n> b", "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<32> bv(1010101)" has no "1010101"
bit-pattern in it. why ? Is it printing the "1010101" in 32 bit-pattern.
(4) "std::bitset<64> bitvec(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 ?