parent
5a8bf83c7b
commit
57f72ea5f5
@ -1,3 +1,10 @@ |
||||
#pragma once |
||||
|
||||
#include <stdlib.h> |
||||
|
||||
unsigned ilog2(unsigned n); |
||||
int is_bit_set(void *base, size_t n); |
||||
void set_bit(void *base, size_t n); |
||||
void clear_bit(void *base, size_t n); |
||||
size_t next_set_bit(void *base, size_t n); |
||||
size_t next_clear_bit(void *base, size_t n); |
||||
|
@ -1,10 +0,0 @@ |
||||
#pragma once |
||||
|
||||
#include <stdint.h> |
||||
#include <stdlib.h> |
||||
|
||||
int is_bit_set(void *set, size_t n); |
||||
void set_bit(void *set, size_t n); |
||||
void clear_bit(void *set, size_t n); |
||||
size_t next_set_bit(void *set, size_t len); |
||||
size_t next_clear_bit(void *set, size_t len); |
@ -1,61 +0,0 @@ |
||||
#include <bitset.h> |
||||
#include <macros.h> |
||||
|
||||
int is_bit_set(void *set_, size_t n) |
||||
{ |
||||
uint8_t *set = set_; |
||||
uint8_t *word, mask; |
||||
|
||||
word = set + n / BIT_SIZE(*set); |
||||
mask = (1 << (n % BIT_SIZE(*set))); |
||||
|
||||
return (*word & mask) ? 1 : 0; |
||||
} |
||||
|
||||
void set_bit(void *set_, size_t n) |
||||
{ |
||||
uint8_t *set = set_; |
||||
uint8_t *word, mask; |
||||
|
||||
word = set + n / BIT_SIZE(*set); |
||||
mask = (1 << (n % BIT_SIZE(*set))); |
||||
|
||||
*word |= mask; |
||||
} |
||||
|
||||
void clear_bit(void *set_, size_t n) |
||||
{ |
||||
uint8_t *set = set_; |
||||
uint8_t *word, mask; |
||||
|
||||
word = set + n / BIT_SIZE(*set); |
||||
mask = (1 << (n % BIT_SIZE(*set))); |
||||
|
||||
*word &= ~mask; |
||||
} |
||||
|
||||
size_t next_set_bit(void *set_, size_t len) |
||||
{ |
||||
uint8_t *set = set_; |
||||
size_t n; |
||||
|
||||
for (n = 0; n < len; ++n) { |
||||
if (is_bit_set(set, n)) |
||||
return n; |
||||
} |
||||
|
||||
return SIZE_MAX; |
||||
} |
||||
|
||||
size_t next_clear_bit(void *set_, size_t len) |
||||
{ |
||||
uint8_t *set = set_; |
||||
size_t n; |
||||
|
||||
for (n = 0; n < len; ++n) { |
||||
if (!is_bit_set(set, n)) |
||||
return n; |
||||
} |
||||
|
||||
return SIZE_MAX; |
||||
} |
Loading…
Reference in new issue