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

array of bits

I feel like I might be overlooking something obvious, but...

I want to store an array of bits (0's and 1's) in the cheapest possible way.
The array might be 1,000 to 10,000 elements long. What variable type/
structure should I use?

Thanks!
Nov 14 '05 #1
5 14238
"Darius Fatakia" <da************@yahoo.com> writes:
I feel like I might be overlooking something obvious, but...

I want to store an array of bits (0's and 1's) in the cheapest possible way.
The array might be 1,000 to 10,000 elements long. What variable type/
structure should I use?


Use an array of integer type (`unsigned char' or `unsigned int',
for instance), then use bitwise operators to access individual
bits.
--
"Given that computing power increases exponentially with time,
algorithms with exponential or better O-notations
are actually linear with a large constant."
--Mike Lee
Nov 14 '05 #2
On Thu, 15 Jan 2004 17:38:19 -0800, "Darius Fatakia"
<da************@yahoo.com> wrote in comp.lang.c:
I feel like I might be overlooking something obvious, but...

I want to store an array of bits (0's and 1's) in the cheapest possible way.
The array might be 1,000 to 10,000 elements long. What variable type/
structure should I use?

Thanks!


You must have somehow missed this question (and its answer) when you
read the FAQ for this group, as you are supposed to and surely did
before you posted here:

20.8 How can I implement sets or arrays of bits?

There's a link to the FAQ for comp.lang.c, which includes the answer
to this question as well as many, many others, in my signature block.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
Darius Fatakia wrote:

I feel like I might be overlooking something obvious, but...
It's called the FAQ.

I want to store an array of bits (0's and 1's) in the cheapest
possible way. The array might be 1,000 to 10,000 elements long.
What variable type/ structure should I use?


Something that can hold 10,000 bits. I suggest you start with:

#define BITCOUNTMAX 10000

and go on from there. Define cheap.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4
Jack Klein wrote:
On Thu, 15 Jan 2004 17:38:19 -0800, "Darius Fatakia"
<da************@yahoo.com> wrote in comp.lang.c:
I want to store an array of bits (0's and 1's) in the cheapest possible way.
The array might be 1,000 to 10,000 elements long. What variable type/
structure should I use?

You must have somehow missed this question (and its answer) when you
read the FAQ for this group, as you are supposed to and surely did
before you posted here:

20.8 How can I implement sets or arrays of bits?

There's a link to the FAQ for comp.lang.c, which includes the answer
to this question as well as many, many others, in my signature block.


He didn't just ask how he can implement array of bits, but
how to do it in the cheapest way.

Unfortunately he didn't say what cheap was.

On some machines the fastest (cheapest execution time) is to store
each bit in a single char or int array element.

Cheapest in memory used is to use the logical operators and
store the appropriate number of bits in an unsigned char or
unsigned int array element.

You might need to try both to see which is best on your machine.

-- glen

Nov 14 '05 #5
"Jack Klein" <ja*******@spamcop.net> wrote:
You must have somehow missed this question (and its answer)
when you read the FAQ for this group, as you are supposed
to and surely did before you posted here:

20.8 How can I implement sets or arrays of bits?


The macros given in the FAQ:
#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))

Seem to be missing a way to clear a bit?
#define BITCLEAR(a, b) ((a)[BITSLOT(b)] &= ~BITMASK(b))

May as well add a toggler too:
#define BITFLIP(a, b) ((a)[BITSLOT(b)] ^= BITMASK(b))

--
Simon.
Nov 14 '05 #6

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

Similar topics

35
by: Scott Kelley | last post by:
Is there a way to reset all elements of an array with a single instruction? I want to set all elements to zero. Currently looping to do so. thx, Scott Kelley
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
4
by: Lance | last post by:
I have an array of bytes that I need to convert into an array of Integers. But, the number of bits per value in the Byte array is not necessarily divisible by 8 (although it will never exceed...
4
by: msosno01 | last post by:
I have Java client that connects to C++ server. The client sends integer in binary using DataOutputStream write function. I am reading these data into buffer. I have to convert this buffer back...
12
by: rajus | last post by:
I want to store the (x,y) coordinates of about 10,000 points in a 2D array.How can I store them and retrieve them later?
6
by: Active8 | last post by:
Hi: This prob "magically" stopped and I'm not sure if I can recreate it, but thought I'd share it and maybe learn something. #include "stdafx.h" #include <stdlib.h> #include <math.h>...
3
by: strikeforce299 | last post by:
I am trying to write a program that gets a 8 bit binary number from the user and converts it to the equivalent decimal and display on screen. i have it working but the user has to enter each bit...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.