---
title: "位数组"
author: "luckyclouds"
date: "2013-06-27T11:29:46Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-06-27T11:29:46Z"
id: 1895115
url: "https://luckyclouds.iteye.com/blog/1895115"
---

```c

#define CHAR_BIT  (8)
#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[bitslot(b)] |= BITMASK(b))
#define BITCLEAR(a, b) ((a)[bitslot(b)] &= ~BITMASK(b))
#define BITTEST(a, b) ((a)[bitslot(b)] & BITMASK(b))
#define BITNSLOTS(nb) ((nb + CHAR_BIT - 1) / CHAR_BIT)
char bitarray[bitnslots(50)];
memset(bitarray, 0, BITNSLOTS(50));
BITSET(bitarray, 23);

```