bool vs bit array

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

bool vs bit array

Post by Neo »

I have a scenario where I need to store the state of 64 entities (max).
I could do this with either a bool array or a 64bit variable (where I use 1 bit to represent each entity).

Option1

Code: Select all

bool state[64];
Option2

Code: Select all

UINT64 state;
//use shifts to determine individual entities
I was wondering which would be more efficient/faster.
Any ideas on this?
Only Human
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post by Tyler »

Clearly bool would be faster because it is a build in system for handling which only requires a single check. The second option is far ore efficient though as it only takes up 8 bytes... whereas bool if i remember correctly uses a whole byte meaning you save 56 bytes.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

not sure if it works but might you try

Code: Select all

char _64bits[64]:1;
not sure if it works though..

edit:
ok.. tried to do that so my best idea is to do something like

Code: Select all

typedef struct{
    unsigned char bit0:1;
    unsigned char bit1:1;
    unsigned char bit2:1;
    unsigned char bit3:1;
    unsigned char bit4:1;
    unsigned char bit5:1;
    unsigned char bit6:1;
    unsigned char bit7:1;
}__attribute__((packed))_8bits;

_8bits _64bits[8];
but thats the only fast, easy, and effiecent way...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: bool vs bit array

Post by Brendan »

Hi,
Neo wrote:I was wondering which would be more efficient/faster.
Any ideas on this?
I'd be tempted to use a 64-bit unsigned int and inline assembly (defined as a macro) based on the "BT" instruction.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Hmmmm...

Is this C99? Or C++? (Because you use bool, which doesn't exist in C89.) Userspace, I assume - or kernel work?

For userspace C++ (or if your kernel-space library includes it), you could take a look at <bitset>... depending on what you want to do with those flags, that might be more conventient / efficient.

Don't use vector<bool>, though. It's broken. (Because operator[] doesn't return a pointer to an element, it isn't really a vector, or a STL container at that, so the whole abstraction is broken. You might want deque<bool>, but <bitset> is optimized for bool.)
Every good solution is obvious once you've found it.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Post by Neo »

Solar wrote:Hmmmm...

Is this C99? Or C++? (Because you use bool, which doesn't exist in C89.) Userspace, I assume - or kernel work?
C++, userspace
Only Human
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Re: bool vs bit array

Post by B.E »

Neo wrote: I was wondering which would be more efficient/faster.
Any ideas on this?
You have to deside on which is more applicable to the problem. Here's some factors that you need to consider:
  • The array is faster then the 64-bit UINT, if you are using it for single bool manipulation. but is slower when using it for multiple bool manipulation.

    Using the array procedures more readable code then the 64-bit UINT.

    the integer is a lot harder to upgrade.
The choice is yours
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
Post Reply