bitset: add functions to manipulate bitsets
This commit is contained in:
parent
cd9d974a91
commit
c594c2aa9a
3 changed files with 41 additions and 0 deletions
1
Makefile
1
Makefile
|
@ -16,6 +16,7 @@ CFLAGS += -Iinclude
|
||||||
CFLAGS += -Wall -Wundef -Wextra -Wshadow -Wimplicit-function-declaration
|
CFLAGS += -Wall -Wundef -Wextra -Wshadow -Wimplicit-function-declaration
|
||||||
CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
|
CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
|
||||||
|
|
||||||
|
obj-y += source/bitset.o
|
||||||
obj-y += source/main.o
|
obj-y += source/main.o
|
||||||
obj-y += source/shell/cmd.o
|
obj-y += source/shell/cmd.o
|
||||||
obj-y += source/shell/flash.o
|
obj-y += source/shell/flash.o
|
||||||
|
|
8
include/bitset.h
Normal file
8
include/bitset.h
Normal 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
32
source/bitset.c
Normal 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;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue