bitset: add functions to manipulate bitsets

tags/0.1.0
S.J.R. van Schaik 7 years ago
parent cd9d974a91
commit c594c2aa9a
  1. 1
      Makefile
  2. 8
      include/bitset.h
  3. 32
      source/bitset.c

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

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

@ -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;
}
Loading…
Cancel
Save