1 /**************************************** 2 * File Name : bit.c 3 * Creat Data : 2015.1.19 4 * Author : ZY 5 *****************************************/ 6 7 /*编写一组函数,实现位数组*/ 8 9 #include <stdio.h> 10 11 //每个函数的第1个参数是个字符数组,用于实际存储所有的位。 12 //第2个参数用于标示需要访问的位 13 14 /*把指定的位设置为1*/ 15 void set_bit( char bit_array[],unsigned bit_number ) 16 { 17 bit_array[bit_number] = 1; 18 printf("%d ",bit_array[bit_number]); 19 } 20 /*把指定的位清零*/ 21 void clear_bit( char bit_array[],unsigned bit_number ) 22 { 23 bit_array[bit_number] = 0; 24 printf("%d ",bit_array[bit_number]); 25 } 26 /*如果value的值为0,则把指定位清0,否则设置1*/ 27 void assign_bit( char bit_array[],unsigned bit_number,int value ) 28 { 29 if( 0 == value ) 30 { 31 bit_array[bit_number] = 0; 32 } 33 else 34 { 35 bit_array[bit_number] = 1; 36 } 37 printf("%d ",bit_array[bit_number]); 38 } 39 /*如果指定位不是0,函数返回真,否则返回假*/ 40 int test_bit( char bit_array[],unsigned bit_number ) 41 { 42 if( 0 == bit_number ) 43 { 44 return 0; 45 } 46 else 47 { 48 return 1; 49 } 50 } 51 52 int main() 53 { 54 char arr[] = "asdfghjkl"; 55 unsigned bit = 5; 56 set_bit( arr,bit ); 57 clear_bit( arr,bit ); 58 assign_bit( arr,bit,1); 59 assign_bit( arr,bit,0); 60 printf("%d ",test_bit( arr,bit )); 61 }