creating an int from bits

C

cppaddict

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
 
J

Jacques Labuschagne

cppaddict said:
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.
 
J

John Harrison

cppaddict said:
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.

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

john
 
C

cppaddict

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
 
J

John Harrison

cppaddict said:
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
 
K

Kelvin@!!!

cppaddict said:
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...)
 
C

cppaddict

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
 
J

Jeff Schwab

John said:
bitset if you have a fixed number of bits. vector<bool> (I guess) if it is
variable.




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

Siemel Naran

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?
 
S

Siemel Naran

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>;
}
 
J

John Harrison

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
 
M

Michael D. Borghardt

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 != '0');
}
std::cout << "string is " << str << " number is " << number <<
std::endl;
return 0;
}
 
M

Michiel Salters

cppaddict said:
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
 
M

Michiel Salters

Siemel Naran said:
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
 
D

Default User

Jacques said:
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
 
S

Siemel Naran

Michiel Salters said:
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?
 
D

David Harmon

On Tue, 11 May 2004 03:33:16 GMT in comp.lang.c++, "Siemel Naran"
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/[email protected]
)
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top