long long is a C99 (latest version of C) data type. It doesn't exist in C++.
[edit] http://www.thescripts.com/forum/thread63790.html [/edit]
Printable View
long long is a C99 (latest version of C) data type. It doesn't exist in C++.
[edit] http://www.thescripts.com/forum/thread63790.html [/edit]
Why not bytes?
http://c-faq.com/misc/bitsets.html
As I showed earlier, simply use the much under-rated ~ instead of subtracting from some magic number.
This is in much the same vein as the way that if you want a constant of 0xFFFFFFFF then you're best to write ~0, which unlike writing -1, even also gives the correct value on a non twos-complement machine.
CornedBee: CHAR_BIT is actually only defined in limits.h on VS2005 Express.
I don't know why no-one's mentioned it (that I can see), but instead of 4294967295 (assuming that was what was meant by 4294967296), you can use ULONG_MAX from <climits>. Or UINT_MAX, if that's what you're after. Or the signed versions, LONG_MAX and INT_MAX.Quote:
This is in much the same vein as the way that if you want a constant of 0xFFFFFFFF then you're best to write ~0, which unlike writing -1, even also gives the correct value on a non twos-complement machine.
I have no experience of dealing with bit operations. Could someone explain the code above or show me a good reference ?Code:bool Set(int element, bool value)
{
if (element > elementCount)
return false;
int ed32 = element / 32;
int em32 = element % 32;
unsigned int modValue;
if (value)
{
modValue = 1 << em32;
array[ed32] |= modValue;
}
else
{
modValue = 4294967296 - (1 << em32);
array[ed32] &= modValue;
}
return true;
}
Thanks.
Here's the revised version:
Code:class CBoolArray
{
bool initialized;
unsigned int* array;
unsigned int elementCount;
int bitsPerInt;
public:
CBoolArray(unsigned int elements)
{
initialized = true;
bitsPerInt = sizeof(int) * CHAR_BIT;
int arraySize = elements / bitsPerInt + ((elements % bitsPerInt) ? 1 : 0);
array = new unsigned int[arraySize];
for (int i = 0; i < arraySize; i++)
array[i] = 0;
elementCount = elements;
}
CBoolArray()
{
initialized = false;
bitsPerInt = sizeof(int) * CHAR_BIT;
}
void Resize(unsigned int elements)
{
if (elements == elementCount)
return;
unsigned int* prevArray = array;
int eModBits = elements % bitsPerInt;
int eDivBits = elements / bitsPerInt;
int arraySize = elements / bitsPerInt + (eModBits ? 1 : 0);
int i;
if (initialized)
{
if (elements < elementCount)
{
array = new unsigned int[arraySize];
for (i = 0; i < arraySize - 1; i++)
array[i] = prevArray[i];
if (eModBits)
{
prevArray[i] &= (1 << eModBits) - 1;
array[i] = prevArray[i];
}
}
else
{
array = new unsigned int[arraySize];
for (int i = 0; i < (eDivBits) + (eModBits ? 1 : 0); i++)
array[i] = prevArray[i];
for(; i < arraySize; i++)
array[i] = 0;
}
elementCount = elements;
}
else
{
initialized = true;
int arraySize = eDivBits + (eModBits ? 1 : 0);
array = new unsigned int[arraySize];
for (int i = 0; i < arraySize; i++)
array[i] = 0;
elementCount = elements;
}
}
bool Set(unsigned int element, bool value)
{
if (element > elementCount)
return false;
int eModBits = element % bitsPerInt;
int eDivBits = element / bitsPerInt;
if (value)
array[eDivBits] |= (1 << eModBits);
else
array[eDivBits] &= ~(1 << eModBits);
return true;
}
bool At(unsigned int element)
{
if (element > elementCount)
return false;
return ((array[element / bitsPerInt] >> (element % bitsPerInt)) & 1);
}
};
That looks much better.
Here's a few more sugestions though:
bitsPerInt is being declared as a variable, though logically it is a constant.
Might I suggest declaring it like this:You can do away with the 'initialised' variable as well by setting 'array' to NULL in the constructor.Code:enum { bitsPerInt = sizeof(int) * CHAR_BIT };
I recommend learning about constructor initialiser lists and using them.
The for loops can be replaced with calls to std::fill or std::copy.
The class currently leaks lots of memory. You need to at the very least either write a destructor and fix Resize, or declare 'array' as an std::auto_ptr.
Even better, you can simply use a std::vector in place of both 'array' and 'elementCount'.
Then on to the usability improvements:
Add an array operator and a const array operator.
sizeof(object)*CHAR_BIT is always the number of bits in the object.
The container, yes, not necessarily the value.
[edit]For illustration: consider a 40-bit register containing a 32-bit int. Is the maximum value of a 32-bit int a 40-bit value?