some questions about "bitset" class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arnuld

    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<64b itvec(32);
    std::bitset<32b v(1010101);

    std::string bstr;
    std::bitset<8bs v(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<8b sv(bstr): " << bsv << std::endl;

    std::cout << "\nprinting bits of \"std::bitset<6 4bitvec(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<64b itvec(32):
    000000000000000 000000000000000 000000000000000 000000000000010 0000
    std::bitset<32b v(1010101): 000000000000111 101101001101101 01
    std::bitset<8bs v(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<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<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 ?


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

  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

    #2
    Re: some questions about &quot;bitset&qu ot; class

    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<64b itvec(32);
    std::bitset<32b v(1010101);
    >
    std::string bstr;
    std::bitset<8bs v(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<8b sv(bstr): " << bsv << std::endl;
    >
    std::cout << "\nprinting bits of \"std::bitset<6 4bitvec(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<64b itvec(32):
    000000000000000 000000000000000 000000000000000 000000000000010 0000
    std::bitset<32b v(1010101): 000000000000111 101101001101101 01
    std::bitset<8bs v(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
    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<1b 1(1);) or you can use 10 bits
    (std::bitset<10 b10(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<32 bv(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<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 ?
    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

    Comment

    • Ian Collins

      #3
      Re: some questions about &quot;bitset&qu ot; class

      arnuld wrote:

      <snip>
      >
      ======== OUTPUT ============
      {arnuld@arch cpp }% g++ -ansi -pedantic -Wall -Wextra ex_03-23.cpp
      {arnuld@arch cpp }% ./a.out
      std::bitset<64b itvec(32):
      000000000000000 000000000000000 000000000000000 000000000000010 0000
      std::bitset<32b v(1010101): 000000000000111 101101001101101 01
      std::bitset<8bs v(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?
      >
      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<32 bv(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<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 ?
      >
      They both are, the former outputs the MSB first, in the latter, you
      output the LSB first.

      --
      Ian Collins.

      Comment

      Working...