Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum size implementation for bool array

I need a minimum size implementation of an array of bools. The size of the array is known at compile time.

I checked std::bitset and boost::array, but they both incur an overhead that is significant for small arrays. For example, if the array size is 8, the container should only use 1 byte of memory (assuming common CPU architecture).

Does this exist or do I need to roll my own?

like image 511
Gabriel Schreiber Avatar asked Mar 11 '13 11:03

Gabriel Schreiber


People also ask

What is size of bool data type?

Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False.

What is size of bool in C?

bool The bool type takes one byte and stores a value of true (1) or false(0). The size of a bool is 1 true 1 1 1 false 0 0 0 Press any key to continue . . . int is the integer data type.

What is a bool array?

A boolean array is a numpy array with boolean (True/False) values. Such array can be obtained by applying a logical operator to another numpy array: import numpy as np a = np.


1 Answers

You can make it by your own, but not from scratch. bitset implementation should have a couple of lines looking like typedef unsigned long _WordT; (SGI) or typedef _Uint32t _Ty; (MSVS). You could carefully replace the type and namespace and make your own container this way. I changed the type to char and sizeof returns 1 (vs2010 proof-of-concept on pastebin)

like image 105
Dmitry Galchinsky Avatar answered Sep 25 '22 22:09

Dmitry Galchinsky