diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2008-07-17 04:56:20 +0000 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2008-07-17 04:56:20 +0000 |
commit | 65f2700670f1e3028ba949e2981583f88caa0d14 (patch) | |
tree | 519af5f6323db449d634be8e2656ee177276502d /bits.c | |
parent | 4ca672e113bd7ab6c106c41d53b10a4828228b4a (diff) | |
download | gpsd-65f2700670f1e3028ba949e2981583f88caa0d14.tar.gz |
Revised and corrected bitfield functions.
Diffstat (limited to 'bits.c')
-rw-r--r-- | bits.c | 16 |
1 files changed, 10 insertions, 6 deletions
@@ -25,21 +25,25 @@ unsigned long long ubits(char buf[], unsigned int start, unsigned int width) unsigned int i;; assert(width <= sizeof(long long) * BITS_PER_BYTE); - for (i = 0; i < (width + BITS_PER_BYTE - 1) / BITS_PER_BYTE; i++) { + for (i = start / BITS_PER_BYTE; i < (start + width + BITS_PER_BYTE - 1) / BITS_PER_BYTE; i++) { fld <<= BITS_PER_BYTE; - fld |= (unsigned char)buf[start / BITS_PER_BYTE + i]; + fld |= (unsigned char)buf[i]; } #ifdef DEBUG printf("Extracting %d:%d from %s: segment 0x%llx = %lld\n", start, width, gpsd_hexdump(buf, 12), fld, fld); #endif /* DEBUG */ - fld &= (0xffffffff >> (start % BITS_PER_BYTE)); + fld >>= (BITS_PER_BYTE - ((start + width) % BITS_PER_BYTE)); #ifdef DEBUG - printf("After masking: 0x%llx = %lld\n", fld, fld); + printf("After downshifting by %d bits: 0x%llx = %lld\n", + (BITS_PER_BYTE - ((start + width) % BITS_PER_BYTE)), + fld, fld); #endif /* DEBUG */ - fld >>= (BITS_PER_BYTE - 1) - ((start + width) % BITS_PER_BYTE); + + fld &= ~(0xffffffff << width); #ifdef DEBUG - printf("After downshifting: 0x%llx = %lld\n", fld, fld); + printf("After selecting out the bottom %u bits: 0x%llx = %lld\n", + width, fld, fld); #endif /* DEBUG */ return fld; |