bitset: add functions to manipulate bitsets

This commit is contained in:
S.J.R. van Schaik 2017-03-17 15:02:17 +00:00
parent cd9d974a91
commit c594c2aa9a
3 changed files with 41 additions and 0 deletions

View file

@ -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

8
include/bitset.h Normal file
View file

@ -0,0 +1,8 @@
#pragma once
#include <stdint.h>
#include <stdlib.h>
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);

32
source/bitset.c Normal file
View file

@ -0,0 +1,32 @@
#include <bitset.h>
#include <macros.h>
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;
}