473,766 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8825
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.co m> wrote in message
news:2v******** *************** *********@4ax.c om...
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.co m> wrote in message
news:19******** *************** *********@4ax.c om...
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(unsigne d& bits, unsigned n)
{
bits |= (1 << n);
}

inline void clear_bit(unsig ned& bits, unsigned n)
{
bits &= ~(1 << n);
}

inline bool test_bit(unsign ed 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.co m> wrote in message
news:2v******** *************** *********@4ax.c om...
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(unsigne d& bits, unsigned n)
{
bits |= (1 << n);
}

inline void clear_bit(unsig ned& bits, unsigned n)
{
bits &= ~(1 << n);
}

inline bool test_bit(unsign ed 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.co m> wrote in message
news:2v******** *************** *********@4ax.c om...
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

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

Similar topics

7
1443
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 compression modules but I can't understand them much... Thank you very much Any good link would be appreciated of course :)
2
1553
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 Transfer Service) to check for the most current version of a file and downloads this version. I have a wrapper class thats imported as a reference in order for me to be
7
2283
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 prototype: unsigned long long extractValue(unsigned long long target, int a, int b) where a and b are the range of bits counted off from the most significant
2
6161
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 would like to put in the header of a packet. 2. I know the size of data I will send and the header size in bytes, so I allocate the space in memory for both and create two pointers: for data part (dataptr) and header part. Header pointer coinsided...
38
3010
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 managed to create arrays using DOUBLE data types, but when I try to access the array, the compiler complains that the number is not an INT, i.e.
3
2284
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 these structs. A typical usage case will be as ff (note the code below is Pseudocode and WILL NOT compile) //example structs (I have left out the ctors/dtors etc for brevity sake) struct MyStructA
8
2074
by: DaTurk | last post by:
Hi, I was just curious how you would go about creating a unique identifier with 3 ints.
19
8611
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 varying lengths (so no arrays!). Does anyone know the most efficient way to do this? I have tried: list = ,,,,] for _ in xrange(3000000)]
20
3427
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 flag5; bool flag6;
0
9571
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8835
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.