/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>

void bitset(unsigned char *ba, int ix)
{
    ba[ix / 8] |= (1 << ix % 8);
}

void bitclr(unsigned char * ba, int ix)
{
    ba[ix / 8] &= ~(1 << ix % 8);
}

int bittst(unsigned char *ba, int ix)
{
    return ba[ix / 8] & (1 << ix % 8);
}

int main()
{
    int i;
    unsigned char b[] = { 0, 255 };
    printf("%2X %2X\n", b[0], b[1]);
    for(i = 0; i < 16; i++) printf("%c", bittst(b, i) ? '1' : '0');
    printf("\n");
    bitset(b, 2);
    bitclr(b, 10);
    printf("%2X %2X\n", b[0], b[1]);
    for(i = 0; i < 16; i++) printf("%c", bittst(b, i) ? '1' : '0');
    printf("\n");
    return 0;
}
