473,385 Members | 1,940 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,385 software developers and data experts.

creating an int from bits

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
19 8775
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

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

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

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

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
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
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
"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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: andrea | last post by:
I was thinking to code the huffman algorithm and trying to compress something with it, but I've got a problem. How can I represent for example a char with only 3 bits?? I had a look to the...
2
by: Amongin Ewinyu | last post by:
Hi, I'm trying to create a Windows service programmatically that will do the following: - When a user logs on, it will automatically run a service that uses BITS (Background Intelligent...
7
by: Digital Puer | last post by:
I'd like to be able to create an integer from a range of contiguous bits in another integer. I'm on a big-ending machine, in case that matters. I would ideally like to create a function with this...
2
by: bobrics | last post by:
Hi, I would like to create a packet for a RAW socket transfer. Please let me know if this is the right approach. 1. First, I am creating a header structure where I store all the information I...
38
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
8
by: DaTurk | last post by:
Hi, I was just curious how you would go about creating a unique identifier with 3 ints.
19
by: Dr Mephesto | last post by:
Hi! I would like to create a pretty big list of lists; a list 3,000,000 long, each entry containing 5 empty lists. My application will append data each of the 5 sublists, so they will be of...
20
by: Plissken.s | last post by:
Is there an efficient way to create a struct of flag in C++? I need to create a struct of boolean flag, like this: struct testStruct { bool flag1; bool flag2; bool flag3; bool flag4; bool...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.