tags:

views:

174

answers:

4

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...

+2  A: 
struct int33
{
   unsigned long long x:33;
};

?

Armen Tsirunyan
+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
Bit fields of data types than `int`, `signed int` and `unsigned int` are compiler-specific extensions, but GCC supports it so +1 anyway :-)
Christoffer
@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
@Kenny: Thank you so much.. it helps. I am loving Stackoverflow
neuron
@KennyTM quite right, I thought of C not C++
Christoffer
+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
Translated: Use the native 64 bit datatype, and only use 33 bits of it when reading.
Billy ONeal
@Billy: thanks for the rewording.
fbrereto
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