位数组
看了《C和指针》后,参考网上的代码自己实现的版本(真的不是纯抄的)。这是5.9 编程练习的第4题。
头文件bits.h:
/* bits.h */ #ifndef BITS_H_INCLUDED #define BITS_H_INCLUDED void set_bit(char bit_array[], unsigned int bit_number); void clear_bit(char bit_array[], unsigned int bit_number); void reverse_bit(char bit_array[], unsigned int bit_number); void assign_bit(char bit_array[], unsigned int bit_number, int value); int test_bit(char bit_array[], unsigned int bit_number); #endif // BITS_H_INCLUDED
源代码bits.c
/* bits.c */ #include "bits.h" #define BASE 8 #define SHIFT 3 #define MASK 7 void set_bit(char bit_array[], unsigned int bit_number) { bit_array[bit_number >> SHIFT] |= 1 << (bit_number & MASK); } void clear_bit(char bit_array[], unsigned int bit_number) { bit_array[bit_number >> SHIFT] &= ~(1 << (bit_number & MASK)); } void reverse_bit(char bit_array[], unsigned int bit_number) { bit_array[bit_number >> SHIFT] ^= 1 << (bit_number & MASK); } void assign_bit(char bit_array[], unsigned int bit_number, int value) { if (value == 1) { set_bit(bit_array, bit_number); } else if (value == 0) { clear_bit(bit_array, bit_number); } } int test_bit(char bit_array[], unsigned int bit_number) { return (bit_array[bit_number >> SHIFT] & (1 << (bit_number & MASK))) >> (bit_number & MASK); }
测试用例:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "bits.h" int main() { char bits[128]; memset(bits, 0, sizeof(bits)); set_bit(bits, 234); printf("%d\n", test_bit(bits, 234)); clear_bit(bits, 234); printf("%d\n", test_bit(bits, 234)); reverse_bit(bits, 235); printf("%d\n", test_bit(bits, 235)); reverse_bit(bits, 235); printf("%d\n", test_bit(bits, 235)); assign_bit(bits, 1024, 1); printf("%d\n", test_bit(bits, 1024)); return 0; }
答案版本:
头文件bitarray.h
#ifndef BITARRAY_H_INCLUDED #define BITARRAY_H_INCLUDED /* * Prototypes for a suite of functions that implement an array of bits in a * character array. */ // Set a specific bit void set_bit(char bit_array[], unsigned int bit_number); // Clear a specific bit void clear_bit(char bit_array[], unsigned int bit_number); // Assign a value to a bit void assign_bit(char bit_array[], unsigned int bit_number, int value); // Test a specific bit int test_bit(char bit_array[], unsigned int bit_number); #endif // BITARRAY_H_INCLUDED
源代码bitarray.c
/* bitarray.cpp -- Implements an array of bits in a character array. */ #include <limits.h> #include "bitarray.h" static unsigned int character_offset(unsigned int bit_number); static unsigned int bit_offset(unsigned int bit_number); void set_bit(char bit_array[], unsigned int bit_number) { bit_array[character_offset(bit_number)] |= 1 << bit_offset(bit_number); } void clear_bit(char bit_array[], unsigned int bit_number) { bit_array[ character_offset( bit_number ) ] &= ~(1 << bit_offset(bit_number)); } void assign_bit(char bit_array[], unsigned int bit_number, int value) { if (value != 0) set_bit(bit_array, bit_number); else clear_bit(bit_array, bit_number); } int test_bit(char bit_array[], unsigned int bit_number) { return (bit_array[ character_offset( bit_number ) ] & 1 << bit_offset( bit_number )) != 0; } static unsigned int character_offset(unsigned int bit_number) { return bit_number / CHAR_BIT; } static unsigned int bit_offset(unsigned int bit_number) { return bit_number % CHAR_BIT; }