P: n/a

Hi,
I am going to have a series of bit flags which I could store in an
array, or as a string ("10011001"), or any other way.
I want to be able to turn this series of bits into an int. I know C++
must have some class or builtin functionality for this, but my web
searching thus far hasn't found it. Can someone let me know what I
should use?
I am flexible on how to store the bits, so I'm taking suggestions for
that too. Ideally, I want whatever combination of bitstorage and
bittoint conversion mechanism is fastest.
Thanks for any ideas,
cpp  
Share this Question
P: n/a

cppaddict wrote: Hi,
I am going to have a series of bit flags which I could store in an array, or as a string ("10011001"), or any other way.
I want to be able to turn this series of bits into an int. I know C++ must have some class or builtin functionality for this, but my web searching thus far hasn't found it. Can someone let me know what I should use?
I am flexible on how to store the bits, so I'm taking suggestions for that too. Ideally, I want whatever combination of bitstorage and bittoint conversion mechanism is fastest.
How many bits? How do you want to handle the case where there are more
bits than can fit into an int?
Jacques.  
P: n/a

"cppaddict" <he***@hello.com> wrote in message
news:2v********************************@4ax.com... Hi,
I am going to have a series of bit flags which I could store in an array, or as a string ("10011001"), or any other way.
I want to be able to turn this series of bits into an int. I know C++ must have some class or builtin functionality for this, but my web searching thus far hasn't found it. Can someone let me know what I should use?
bitset if you have a fixed number of bits. vector<bool> (I guess) if it is
variable. I am flexible on how to store the bits, so I'm taking suggestions for that too. Ideally, I want whatever combination of bitstorage and bittoint conversion mechanism is fastest.
If I needed speed I think I would store the bits directly in an unsigned
int.
john  
P: n/a
 How many bits? How do you want to handle the case where there are more bits than can fit into an int?
Jacques.
I will never have more than 18 bits.  
P: n/a
 If I needed speed I think I would store the bits directly in an unsigned int.
John,
How would I accomplish this? Is there a way to set the individual
bits of an int? That's what I want to do.
Thanks,
cpp  
P: n/a

"cppaddict" <he***@hello.com> wrote in message
news:19********************************@4ax.com... If I needed speed I think I would store the bits directly in an unsigned int.
John,
How would I accomplish this? Is there a way to set the individual bits of an int? That's what I want to do.
Thanks, cpp
inline void set_bit(unsigned& bits, unsigned n)
{
bits = (1 << n);
}
inline void clear_bit(unsigned& bits, unsigned n)
{
bits &= ~(1 << n);
}
inline bool test_bit(unsigned bits, unsigned n)
{
return (bits & (1 << n)) != 0;
}
Untested code.
john  
P: n/a

cppaddict wrote: How many bits? How do you want to handle the case where there are more bits than can fit into an int?
Jacques.
I will never have more than 18 bits.
Then John's answer is all you need, because an int usually has at least
32 bits.  
P: n/a

"cppaddict" <he***@hello.com> wrote in message
news:2v********************************@4ax.com... Hi,
I am going to have a series of bit flags which I could store in an array, or as a string ("10011001"), or any other way.
I want to be able to turn this series of bits into an int. I know C++ must have some class or builtin functionality for this, but my web searching thus far hasn't found it. Can someone let me know what I should use?
I am flexible on how to store the bits, so I'm taking suggestions for that too. Ideally, I want whatever combination of bitstorage and bittoint conversion mechanism is fastest.
Thanks for any ideas, cpp
if you want both readiblity and speed... i suggest you using hex numbers...
4 bits in one hex number... for example 0xF = 1111b ( note: the "b" here is
just to tell you it's a binary #, C++ deosnt support that...)

{ Kelvin@!!! }  
P: n/a
 inline void set_bit(unsigned& bits, unsigned n) { bits = (1 << n); }
inline void clear_bit(unsigned& bits, unsigned n) { bits &= ~(1 << n); }
inline bool test_bit(unsigned bits, unsigned n) { return (bits & (1 << n)) != 0; }
Untested code.
john
Thanks John.
That's what I was looking for.
cpp  
P: n/a

John Harrison wrote: "cppaddict" <he***@hello.com> wrote in message news:2v********************************@4ax.com...
Hi,
I am going to have a series of bit flags which I could store in an array, or as a string ("10011001"), or any other way.
I want to be able to turn this series of bits into an int. I know C++ must have some class or builtin functionality for this, but my web searching thus far hasn't found it. Can someone let me know what I should use?
bitset if you have a fixed number of bits. vector<bool> (I guess) if it is variable.
I am flexible on how to store the bits, so I'm taking suggestions for that too. Ideally, I want whatever combination of bitstorage and bittoint conversion mechanism is fastest.
If I needed speed I think I would store the bits directly in an unsigned int.
I thought you hated unsigned int's, and kinda thought they should be
removed from the language? (Not trying to rekindle any discussion on
the topic, just curious whether your opinion has changed.)  
P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2g3bkfF3pkf0U1@uni inline void set_bit(unsigned& bits, unsigned n) { bits = (1 << n); }
inline void clear_bit(unsigned& bits, unsigned n) { bits &= ~(1 << n); }
inline bool test_bit(unsigned bits, unsigned n) { return (bits & (1 << n)) != 0; }
Sometimes we need flip bit, which is the ^= operator.
Also, on some systems unsigned may be 16 bits, whereas the OP wants 18.
Then std::bitset is good. How slow is it really?  
P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2g3bkfF3pkf0U1@uni inline void set_bit(unsigned& bits, unsigned n) { bits = (1 << n); }
inline void clear_bit(unsigned& bits, unsigned n) { bits &= ~(1 << n); }
inline bool test_bit(unsigned bits, unsigned n) { return (bits & (1 << n)) != 0; }
Also, these are good methods to set and unset individual bits. But given a
stream of bits 1101 with the rightmost char as the most 1, and the left most
as 8, we can do:
unsigned number = 0;
while (for all chars in 1101 from right to left) {
number <<= 1u;
const char c = the character, 1 then 0 then 1 then 1;
number = c == '1';
}
We can make if more efficient by not doing the first leftshift, which we can
do by unrolling the loop.
It is also possible that treating the chars as blocks, rather than as single
bits, for example 1101 as one block could make it even more efficient. But
I'd have to think about this one more, to see if it is implementable:
while (for all 4blocks chars in 00111101 from right to left) {
number <<= 4u;
const char c = the block 1101, then 0011
number = <something appropriate>;
}  
P: n/a

> > If I needed speed I think I would store the bits directly in an unsigned int.
I thought you hated unsigned int's, and kinda thought they should be removed from the language? (Not trying to rekindle any discussion on the topic, just curious whether your opinion has changed.)
No my opinion hasn't changed. But doing bit twiddling is one area where you
hardly notice the difference. It's when you are using integers
arithmetically that you can get caught out.
I'm also of the opinion that all programming is a community effort and
sometimes you have to accept and use techniques just because its the
majority view. So even if I was to design an STL like container I would have
the size() method return an unsigned integer type, much as it might pain me
to do so.
john  
P: n/a

Hi.
How about this
#include <iostream>
int main()
{
char str[] = "10010001";
int number = 0;
for (int i = 0; i < (sizeof(str)  1); ++i)
{
number = (number << 1)  (str[i] != '0');
}
std::cout << "string is " << str << " number is " << number <<
std::endl;
return 0;
}  
P: n/a

cppaddict <he***@hello.com> wrote in message news:<m7********************************@4ax.com>. .. How many bits? How do you want to handle the case where there are more bits than can fit into an int?
Jacques.
I will never have more than 18 bits.
Use an unsigned long, it mus always have 32 bits. signed long
also has 32 bits, but one of them is a a sign bit. That
can be a bit weird at times.
Plain int may have 16 bits.
Regards,
Michiel Salters  
P: n/a

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message news:<8p********************@bgtnsc05news.ops.worldnet.att.net>... Also, on some systems unsigned may be 16 bits, whereas the OP wants 18. Then std::bitset is good. How slow is it really?
Depends on the implementation of course, but it's a template. That means
that bitset<N> with N<=(bits in int) probably is implemented as an
class __small_bitset_impl {
unsigned int bits;
protected:
// all the usual bitset ops, inlined
};
template< bool Small, int N>
class __bitset_impl {
// usual impl with arrays of ints
};
template< int N >
class __bitset_impl<false, N> : public __small_bitset_impl { };
E.g. for N<=32 or N<=16 (as appropriate) bitset is just a set of
inlined utility functions around the same int you would use. Of
course, an even better version might use some nonportable
assembly so it could be even better.
My advise? typedef, initially to std::bitset<18> until profiling
tells you otherwise.
Regards,
Michiel Salters  
P: n/a

Michiel Salters wrote: Use an unsigned long, it mus always have 32 bits. signed long
Really? Tell that to my 64bit sun box. :)
Jacques.  
P: n/a

Jacques Labuschagne wrote: Michiel Salters wrote: Use an unsigned long, it mus always have 32 bits. signed long
Really? Tell that to my 64bit sun box. :)
You believe that something with 64 bits does not also have 32 bits?
He did not say that an unsigned long is EXACTLY 32 bits, he said it HAS
32 bits.
Brian Rodenborn  
P: n/a

"Michiel Salters" <Mi*************@logicacmg.com> wrote in message Use an unsigned long, it mus always have 32 bits. signed long also has 32 bits, but one of them is a a sign bit. That
Where in the standard does it say long is 32 or more bits?  
P: n/a

On Tue, 11 May 2004 03:33:16 GMT in comp.lang.c++, "Siemel Naran"
<Si*********@REMOVE.att.net> wrote, "Michiel Salters" <Mi*************@logicacmg.com> wrote in message
Use an unsigned long, it mus always have 32 bits. signed long also has 32 bits, but one of them is a a sign bit. That
Where in the standard does it say long is 32 or more bits?
It's adopted from the C standard, wherein it says that ULONG_MAX must be
greater or equal to 4294967295, etc. (the calculation of the number of
bits required is left to the information theory student. But see http://groups.google.com/gr*********....earthlink.net
)   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 7833
 replies: 19
 date asked: Jul 22 '05
