Is there any way to implement 33 bit unsigned integer for gcc compiler? As of now I am using unsigned 64 bit integer to store 33 bit value. But unfortunately i want the value to be reset after it reaches full 33 bits...
+13
A:
You could use a bit field, e.g. (result)
#include <stdint.h>
#include <cstdio>
struct uint33_t {
uint64_t value : 33;
};
int main() {
uint33_t x = {0x1FFFFFFFFull};
printf("%llx\n", x.value);
x.value += 1;
printf("%llx\n", x.value);
return 0;
}
KennyTM
2010-10-12 17:36:34
Bit fields of data types than `int`, `signed int` and `unsigned int` are compiler-specific extensions, but GCC supports it so +1 anyway :-)
Christoffer
2010-10-12 17:46:19
@Christoffer: It's not an extension. The C++ standard allows any "integral or enumeration type" (§[class.bit]/3) to be used as the bit-field type.
KennyTM
2010-10-12 17:50:27
@Kenny: Thank you so much.. it helps. I am loving Stackoverflow
neuron
2010-10-12 18:30:20
@KennyTM quite right, I thought of C not C++
Christoffer
2010-10-13 07:01:07
+2
A:
The value will overflow when it is pushed beyond the 33-bit boundary; if you use a 33-bit mask when fetching the value you should get the behavior you want.
fbrereto
2010-10-12 17:37:43
Translated: Use the native 64 bit datatype, and only use 33 bits of it when reading.
Billy ONeal
2010-10-12 17:39:41
A:
I wrote some code dealing with this:
http://bitbucket.org/pnathan/logic-vector
You are free to look it over. If you modify/improve it please patch back. :)
Paul Nathan
2010-10-12 17:38:43