473,404 Members | 2,187 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,404 software developers and data experts.

Why upper bits is different after <bitset> to_ulong

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.....

Jul 23 '05 #1
5 1843

DamonChong wrote:
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;

Jul 23 '05 #2
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.

Jul 23 '05 #3
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.

Jul 23 '05 #4

"DamonChong" <so********@excite.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
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
Jul 23 '05 #5
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

Jul 23 '05 #6

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

Similar topics

5
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(): ...
14
by: Ben | last post by:
Hi, I need to write some data types into an array of unsigned chars. These are basically "signals" within a message, so each signal will have a start bit and a length. The signals will also...
2
by: Michal Wyrebski | last post by:
Hello, I was thinking how is the fastest and the best way of passing bitset variable to some function. Only one method came to my mind. It's about passing converted value: #v+ void...
5
by: Artemis Fowl | last post by:
when i used bitset<> to accept data from the user, much to my surprise, i got the wrong answer. After a bit of fiddling, figured out that the data being entered was being consumed in reverse. I...
8
by: tobias.sturn | last post by:
How would be the best solution to create a dynamic bitset cause with the stl bitset you have to give a number of how big the bitset will be and i dont know the size at the beginning... Thanks...
2
by: pedagani | last post by:
Problem: read 'nBits' (where 0< nbits <= 32) from a buffer 'buffer' at a byte position 'pos' with a *bit* offset value of 'bitOffset'. Below is the code that does that (not tested for errors),...
7
by: felixnielsen | last post by:
I would wery much like this to work: @code start #include <iostream> #include <bitset> const unsigned short size = 2; // 2^<unsigned int> // union V { char c; std::bitset<size*size*size> b;...
4
by: Alan Ning | last post by:
Hi all, Say I have the following, string str("Hello World\n") I would like to extract 5 bits at a time from the string. I am doing this to convert ascii string to another string type. Is...
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...
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
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...
0
marktang
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,...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.