#include <stdio.h>
void set_bit(char bit_array[],unsigned bit_number);
void clear_bit(char bit_array[],unsigned bit_number);
void assign_bit(char bit_array[],unsigned bit_number,int value);
int test_bit(char bit_array[],unsigned bit_number);
int main(int argc, const char *argv[])
{
char a[20]="abcdefghi";
int b = 1;
int c = 2;
int d = 3;
set_bit(a,b);
clear_bit(a,c);
assign_bit(a,d,1);
printf("%d\n",test_bit(a,b));
printf("%s\n",a);
return 0;
}
/* 把指定位置1 */
void set_bit(char bit_array[],unsigned bit_number)
{
bit_array[bit_number] = 1;
}
/* 把指定为淸0 */
void clear_bit(char bit_array[],unsigned bit_number)
{
bit_array[bit_number] = 0;
}
/* 如果value的值为0,把指定的位淸0,否则置1 */
void assign_bit(char bit_array[],unsigned bit_number,int value)
{
if(value == 0){
bit_array[bit_number] = 0;
}else {
bit_array[bit_number] = 1;
}
}
/* 指定的位不是0,函数返回真,否则返回假 */
int test_bit(char bit_array[],unsigned bit_number)
{
if(bit_array[bit_number] != 0){
return 1;
}else {
return 0;
}
}
编写一个函数,实现位数组
最新推荐文章于 2021-07-20 11:14:18 发布