By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

creating an int from bits

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 built-in 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 bit-storage and
bit-to-int conversion mechanism is fastest.

Thanks for any ideas,
cpp
Jul 22 '05 #1
Share this Question
Share on Google+
19 Replies


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 built-in 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 bit-storage and
bit-to-int 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.
Jul 22 '05 #2

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 built-in 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 bit-storage and
bit-to-int conversion mechanism is fastest.


If I needed speed I think I would store the bits directly in an unsigned
int.

john
Jul 22 '05 #3

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.

Jul 22 '05 #4

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

Jul 22 '05 #5

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
Jul 22 '05 #6

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

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 built-in 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 bit-storage and
bit-to-int 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@!!! }
Jul 22 '05 #8

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
Jul 22 '05 #9

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 built-in 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 bit-storage and
bit-to-int 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.)
Jul 22 '05 #10

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?
Jul 22 '05 #11

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 4-blocks chars in 00111101 from right to left) {
number <<= 4u;
const char c = the block 1101, then 0011
number |= <something appropriate>;
}
Jul 22 '05 #12

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
Jul 22 '05 #13

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;
}
Jul 22 '05 #14

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
Jul 22 '05 #15

P: n/a
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message news:<8p********************@bgtnsc05-news.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 non-portable
assembly so it could be even better.

My advise? typedef, initially to std::bitset<18> until profiling
tells you otherwise.

Regards,
Michiel Salters
Jul 22 '05 #16

P: n/a
Michiel Salters wrote:
Use an unsigned long, it mus always have 32 bits. signed long


Really? Tell that to my 64-bit sun box. :-)

Jacques.

Jul 22 '05 #17

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 64-bit 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
Jul 22 '05 #18

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?
Jul 22 '05 #19

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
)

Jul 22 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.