How about something like int getbit(bits, n) { return (bits[n>>4] & (n & 0x0F)) == (n & 0x0F); }
This just tests the bit(s) rather than shifting it all the way to the end. The result is a Boolean (well as close as C comes :). -----Original Message----- From: mspgcc-users-ad...@lists.sourceforge.net [mailto:mspgcc-users-ad...@lists.sourceforge.net] On Behalf Of Kelly Murray Sent: Tuesday, June 15, 2004 4:08 AM To: mspgcc-users@lists.sourceforge.net Subject: [Mspgcc-users] bit arrays 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) ); } However, it's really quite ugly compared to just " bits[n] ", I've also looked at the code generated, and it's quite bloated, and given the ROM of a 1101 (1K), I can't really afford that either, the shifting is not a single instruction on a MSP430. The bits[n] generates just 8 bytes of code. (bits[n>>4] >> 15-(n&0xF)) & 0x1) generates 50 bytes My most recent idea is to use something similiar with somewhat less shifting, at least not the loop used above. unsigned char bits[16]; getbit(bits, n) { int bits4 = bits[n>>2]; switch (n & 0x3) { case 11: bits4 >>= 1; case 01: bits4 >>= 1; case 10: bits4 >>= 1; case 11: return bits4 & 1; }} This generates 88 bytes... there must be a simple way to do this that I'm just overlooking. Perhaps some simple assembly code is the best approach? thanks, Kelly Murray ------------------------------------------------------- This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND _______________________________________________ Mspgcc-users mailing list Mspgcc-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mspgcc-users