summaryrefslogtreecommitdiff
path: root/bits.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2008-07-17 04:56:20 +0000
committerEric S. Raymond <esr@thyrsus.com>2008-07-17 04:56:20 +0000
commit65f2700670f1e3028ba949e2981583f88caa0d14 (patch)
tree519af5f6323db449d634be8e2656ee177276502d /bits.c
parent4ca672e113bd7ab6c106c41d53b10a4828228b4a (diff)
downloadgpsd-65f2700670f1e3028ba949e2981583f88caa0d14.tar.gz
Revised and corrected bitfield functions.
Diffstat (limited to 'bits.c')
-rw-r--r--bits.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/bits.c b/bits.c
index 9f97ac5a..a2da1098 100644
--- a/bits.c
+++ b/bits.c
@@ -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;