Skip to content

Instantly share code, notes, and snippets.

@assyrianic
Last active May 4, 2023 00:44
Show Gist options
  • Save assyrianic/e547bdda80524ac79457a1dbe5e8a855 to your computer and use it in GitHub Desktop.
Save assyrianic/e547bdda80524ac79457a1dbe5e8a855 to your computer and use it in GitHub Desktop.
#ifndef BITSTACK_INCLUDED
# define BITSTACK_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
struct BitStack {
uint32_t *stk;
size_t len, sp;
uint8_t bit : 5;
};
static inline void bitstk_push(struct BitStack *const bs, bool const b) {
if( bs->sp >= bs->len ) {
return;
}
bs->stk[bs->sp] ^= (b & 1) << bs->bit;
bs->bit++;
if( bs->bit==0 ) {
bs->sp++;
}
}
static inline bool bitstk_pop(struct BitStack *const bs) {
if( bs->sp > 0 && bs->bit==0 ) {
bs->sp--;
}
bs->bit--;
bool const b = (bs->stk[bs->sp] & (1 << bs->bit)) > 0;
bs->stk[bs->sp] &= ~(1 << bs->bit);
return b;
}
#ifdef __cplusplus
}
#endif
#endif /** BITSTACK_INCLUDED */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment