C and pointer fifth bit array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.