From c594c2aa9a3f2be2e64ee02b2bbef9c068e89b67 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Fri, 17 Mar 2017 15:02:17 +0000 Subject: [PATCH] bitset: add functions to manipulate bitsets --- Makefile | 1 + include/bitset.h | 8 ++++++++ source/bitset.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 include/bitset.h create mode 100644 source/bitset.c diff --git a/Makefile b/Makefile index cee6b6d..4cab395 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ CFLAGS += -Iinclude CFLAGS += -Wall -Wundef -Wextra -Wshadow -Wimplicit-function-declaration CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes +obj-y += source/bitset.o obj-y += source/main.o obj-y += source/shell/cmd.o obj-y += source/shell/flash.o diff --git a/include/bitset.h b/include/bitset.h new file mode 100644 index 0000000..530799b --- /dev/null +++ b/include/bitset.h @@ -0,0 +1,8 @@ +#pragma once + +#include +#include + +int is_bit_set(uint8_t *set, size_t n); +void set_bit(uint8_t *set, size_t n); +void clear_bit(uint8_t *set, size_t n); diff --git a/source/bitset.c b/source/bitset.c new file mode 100644 index 0000000..7700cd1 --- /dev/null +++ b/source/bitset.c @@ -0,0 +1,32 @@ +#include +#include + +int is_bit_set(uint8_t *set, size_t n) +{ + uint8_t *word, mask; + + word = set + n / BIT_SIZE(*set); + mask = (1 << (n % BIT_SIZE(*set))); + + return (*word & mask) ? 1 : 0; +} + +void set_bit(uint8_t *set, size_t n) +{ + uint8_t *word, mask; + + word = set + n / BIT_SIZE(*set); + mask = (1 << (n % BIT_SIZE(*set))); + + *word |= mask; +} + +void clear_bit(uint8_t *set, size_t n) +{ + uint8_t *word, mask; + + word = set + n / BIT_SIZE(*set); + mask = (1 << (n % BIT_SIZE(*set))); + + *word &= ~mask; +}