From 72231e07cdb990c1cd382b4a74f60a1bec8674f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20G=C3=A4hwiler?= Date: Wed, 25 Oct 2017 00:38:11 +0300 Subject: [PATCH] fixed bit helpers --- src/helpers.c | 8 ++++---- src/helpers.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/helpers.c b/src/helpers.c index 0a51ac9..8b9d37e 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -2,12 +2,12 @@ #include "helpers.h" -uint8_t lwmqtt_read_bits(uint8_t byte, uint8_t pos, uint8_t num) { - return (byte & (uint8_t)((~((~0) << num)) << pos)) >> pos; +uint8_t lwmqtt_read_bits(uint8_t byte, int pos, int num) { + return (byte & (uint8_t)((~(0xFF << num)) << pos)) >> pos; } -void lwmqtt_write_bits(uint8_t *byte, uint8_t value, uint8_t pos, uint8_t num) { - *byte = (*byte & ~(uint8_t)((~((~0) << num)) << pos)) | (value << pos); +void lwmqtt_write_bits(uint8_t *byte, uint8_t value, int pos, int num) { + *byte = (*byte & ~(uint8_t)((~(0xFF << num)) << pos)) | (value << pos); } lwmqtt_err_t lwmqtt_read_data(uint8_t **buf, const uint8_t *buf_end, uint8_t **data, size_t len) { diff --git a/src/helpers.h b/src/helpers.h index a008e36..4a62e66 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -11,7 +11,7 @@ * @param num - The number of bits to read. * @return The read bits as a byte. */ -uint8_t lwmqtt_read_bits(uint8_t byte, uint8_t pos, uint8_t num); +uint8_t lwmqtt_read_bits(uint8_t byte, int pos, int num); /** * Write bits to a byte. @@ -21,7 +21,7 @@ uint8_t lwmqtt_read_bits(uint8_t byte, uint8_t pos, uint8_t num); * @param pos - The position of the first bit. * @param num - The number of bits to write. */ -void lwmqtt_write_bits(uint8_t *byte, uint8_t value, uint8_t pos, uint8_t num); +void lwmqtt_write_bits(uint8_t *byte, uint8_t value, int pos, int num); /** * Reads arbitrary data from the specified buffer. The pointer is incremented by bytes read.