Skip to content

Instantly share code, notes, and snippets.

@ifukazoo
Created September 8, 2013 05:38
Show Gist options
  • Save ifukazoo/6482138 to your computer and use it in GitHub Desktop.
Save ifukazoo/6482138 to your computer and use it in GitHub Desktop.
Boolean配列
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
static void set_bit(unsigned char* barray, int idx)
{
div_t t;
t = div(idx, 8);
*(barray + t.quot) |= (1 << t.rem);
}
static int ref_bit(unsigned char* barray, int idx)
{
div_t t;
t = div(idx, 8);
return (*(barray + t.quot) & (1 << t.rem)) > 0;
}
static void clear_bit(unsigned char* barray, int idx)
{
div_t t;
t = div(idx, 8);
*(barray + t.quot) &= (unsigned char)(~(1 << t.rem));
}
int main(void)
{
unsigned char array[8] = {0};
int i;
set_bit(array, 5);
assert(array[0] == 0x20);
set_bit(array, 13);
assert(array[1] == 0x20);
assert(ref_bit(array, 5) == 1);
assert(ref_bit(array, 13) == 1);
clear_bit(array, 13);
assert(ref_bit(array, 13) == 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment