5.4 Exercises: Write a set of functions, implement the dimension array, the function prototype is as follows:
Refers to positioning set to 1
void Set_bit (char bit_array[], unsigned bit_number);
Refers to the position clear 0 void Clear_bit (char bit_array[], unsigned bit_number);
Position zeroing, otherwise set to 1
void Assign_bit (char bit_array[], unsigned bit_number, int value);
The parameter specifies a position of 1 returns True, 0 returns 1int test_bit (char bit_array[], unsigned bit_number);
The first bit of each function is an array of characters, which actually stores all the bits, the second parameter is used to indicate, the bit to access, and the caller of the function to prevent this value from being too large and exceeding the array bounds.
Character offset unsigned int char_offset (unsigned bit_number) {return bit_number/char_bit;} Bit bit offset unsigned int bit_offset (unsigned bit_number) {return bit_number% Char_bit;} void Set_bit (char bit_array[], unsigned bit_number) {Bit_array[char_offset (bit_number)] |= 1 << bit_offset (bit_ number);} void Clear_bit (char bit_array[], unsigned bit_number) {Bit_array[char_offset (bit_number)] &= ~ (1 << bit_ Offset (bit_number));} void Assign_bit (char bit_array[], unsigned 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 bit_number) {//For this bit bit and operation, if 1 then the result is 1<< (bit_number% char_bit) unsigned Iszero = Bit_array[bit_number/char_bit] & (1 << (bit_number% char_bit)); if (Iszero = = (1 << (bit_number % char_bit)) {return TRUE;} return FALSE;}
C and pointer fifth bit array