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]
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]
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Why not bytes?
http://c-faq.com/misc/bitsets.html
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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.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.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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); } };
Don't quote me on that... ...seriously
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.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*