bitstr.c

来自「经过改进的C语言位操作功能」· C语言 代码 · 共 50 行

C
50
字号
/*

  file    : bitstr.c
  author  : Erik Mavrinac
  version : 1.10

  description: These routines manipulate bits in an unsigned int array, to
    facilitate efficient true-false/boolean arrays. Originally an idea in
    Computer Language magazine (April, 1989, pages 34-43), I have made very ex-
    tensive modifications. These routines may or may not only be able to be
    used on i80x86-- I have no way to try portability experiments.

*/

#include "bitstr.h"

/* size of unsigned int in bits */
#define US 16



void clear_bit(unsigned *string, int bit)

/* sets the "bit" bit to 0 */

{
  *(string+bit/US) &= (~(HEX1 << (US-1-(bit % US))));
}



void set_bit(unsigned *string, int bit)

/* sets the "bit" bit to 1 */

{
  *(string+bit/US) |= (HEX1 << (US-1-(bit % US)));
}



int test_bit(unsigned *string, int bit)

/* returns non-zero if "bit" is set */

{
  return (*(string+bit/US) & (HEX1 << (US-1-(bit % US))));
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?