Why upper bits is different after <bitset> to_ulong

D

DamonChong

Hi,

I am trying this in my code and getting strange answers. Would
appreciate if someone can point out what it is i'm missing here. My
thanks in advance.

-------------code snippets--------------
// where str is a std::string variable
std::bitset<4> size;
size.reset();
size |= std::atoi( (str.substr(pos,4)).c_str() );

std::cout << "next - <" << str.substr(pos,4) << ">, after convert - "
<< size.template to_string<char,
std::char_traits<char>,std::allocator<char> >();

------------output to console-----------
size - <0110>, after convert - 1110


----------end of output---------------
Above is the output i get. Notice i am expecting after convert to show
0110 but instead i get 1110!! BTW, i'm using VC++ Toolkit 2003.
Hmmmm.....
 
H

hk_mp5kpdw

DamonChong said:
Hi,

I am trying this in my code and getting strange answers. Would
appreciate if someone can point out what it is i'm missing here. My
thanks in advance.

-------------code snippets--------------
// where str is a std::string variable
std::bitset<4> size;
size.reset();
size |= std::atoi( (str.substr(pos,4)).c_str() );

std::cout << "next - <" << str.substr(pos,4) << ">, after convert - "
<< size.template to_string<char,
std::char_traits<char>,std::allocator<char> >();

------------output to console-----------
size - <0110>, after convert - 1110


----------end of output---------------
Above is the output i get. Notice i am expecting after convert to show
0110 but instead i get 1110!! BTW, i'm using VC++ Toolkit 2003.
Hmmmm.....

If I understand what you are trying to do, I think all you would need
is this:

std::bitset<4> size(str.substr(pos,4));
std::cout << "next - <" << str.substr(pos,4)
<< ">, after convert - " << size;
 
D

DamonChong

Hi,

Thanks for the reply. I'm not able to do that because this occurs
within a while loop and I actually did a size.reset() after each loop.

Regards and thanks again.
 
D

DamonChong

Hi,

This is strange. I modified my codes to create new instances of
std::bitset<4> size for each loop of the while loop as you suggested
above and it is ok. Hmmm, I don't know why my reset() call is not
working still but at least this works, so I will stick with it for the
time being. Thanks again.
 
J

Jeff Flinn

DamonChong said:
Hi,

I am trying this in my code and getting strange answers. Would
appreciate if someone can point out what it is i'm missing here. My
thanks in advance.

-------------code snippets--------------
// where str is a std::string variable
std::bitset<4> size;
size.reset();
size |= std::atoi( (str.substr(pos,4)).c_str() );

std::cout << "next - <" << str.substr(pos,4) << ">, after convert - "
<< size.template to_string<char,
std::char_traits<char>,std::allocator<char> >();

------------output to console-----------
size - <0110>, after convert - 1110


----------end of output---------------
Above is the output i get. Notice i am expecting after convert to show
0110 but instead i get 1110!! BTW, i'm using VC++ Toolkit 2003.
Hmmmm.....

Assuming str.substr(pos,4)) == "0110"

then

std::atoi( (str.substr(pos,4)).c_str() ) == 110

which in binary is 1101110

and the lower 4 bits are 1110

So what unexpected?

Jeff Flinn
 
D

DamonChong

Duh, i can't believe what a silly mistake this is! Thanks alot, I was
trying to convert binary (from a char array of ones and zeros) into
integer using the wrong method. Silly me ;P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top