位数组 帮忙改一下 这个代码怎么才能实现具体位的置1(c和指针的一道题)
程序代码:
#include <stdio.h> #include <limits.h> unsigned character_offset( unsigned bit_number ) { int c1=bit_number / CHAR_BIT; return c1; //计算bit_number第几个字节内 } unsigned bit_offset( unsigned bit_number ) { int j=bit_number % CHAR_BIT; return j; //计算bit_number 在最高字节里是第几位 } void set_bit( char bit_array[], unsigned bit_number ) //把指定的位设置为 1 { printf("char %c\n",bit_array[character_offset(bit_number)]); printf("int %d\n",bit_array[character_offset(bit_number)]); bit_array[ character_offset(bit_number) ] |= ( 1 << bit_offset(bit_number) ); printf("char %c\n",bit_array[character_offset(bit_number)]); printf("int %d\n",bit_array[character_offset(bit_number)]); } int main() { char a[20] = "10001100001"; printf("int a[0]=%d\nchar a[0]=%c\n",a[0],a[0]); printf("CHAR_BIT: %d\n",CHAR_BIT); set_bit(a, 10); printf( "%s\n",a ); return 0; }