Kelly Murray wrote:
Given the small amount of ram (128 bytes) in the x1101, I need to use
bit arrays for a 64bit value. Is there a good/best way to do this?
I've done roughly the code below:
unsigned int bits[4]; // 16bit words x 4 = 64bits
getbit(bits, n) { return( (bits[n>>4] >> 15-(n&0xF)) & 0x1) ); }
note, you can use "long long", mspgcc supports 64 bit types natively.
if you use something like
unsigned long long x = 0x80f0000300000000L;
such an if caluse:
if (x & (1ULL<<63)) { ... }
should generate relatively efficient code, as the compiler knows that he
doesnt need to look at all bits. it shouldn't even use a single shift
instruction as the (1<<63L) is computed by the preprocessor and gcc
looks at the "right" 16 bits of the full number.
an other chance can be using unions, so that you can access the same
number at 16 and 64 bit acccess, that way you can choose the more
efficent code snippet.
typedef union {
unsigned long long ull;
unsigned long ul[2];
unsigned short us[4];
unsigned char uc[8];
} MYTYPE;
MYTYPE x;
x.ull = 0x80f0000300000000L;
if (x.us[0] & (1<<15)) { ... }
however, gcc is usualy quiet good at optimizing, so that using the 64
bit number directly is often easier and as efficient as hand optimizing.
after a quick look at some samples, it looks like it optimizes 32 bits
very well (only accessing the used bits, which it doesnt for 64 bit). so
going with the union from above and doing bit ops on the 32 bit type
could be efficient. but if you have to assign/compare/calculate complete
64 bit numbers, the ull access should be the best.
chris