char 数组中的位操作
Bit manipulation inside a char array
我正在为无符号字符数组中的 set/unset 位编写一个函数。我收到了以下说明。
In this task, bit 0 is the most significant bit. It is also assumed
that unsigned char is exactly 8 bits (1 byte). Thus, for example, bit
8 is a leftmost bit in second unsigned char byte and bit 17 is the
second highest bit in the third unsigned char byte. Thus, examining
the number 170 (0xAA hexadecimal format, 10101010 in binary), the most
significant bit, ie bit 0 has a value of 1.
基于此,我写了以下内容。
/* NOTE:
* -----------
* The parameter binary data (const unsigned char*) in all the functions
* below is likely to hold more than 8 bits. You should take this into
* account when implementing your functions.
*/
/* DESCRIPTION:
* ------------
* The function sets a bit with index i as active (1) in the parameter
* binary data.
*
* PARAMETERS:
* ------------
* unsigned char* data: an array of binary data.
* int i: the index of the bit which to set as active (1).
*
* RETURNS:
* ------------
* Nothing.
*
*/
void op_bit_set(unsigned char *data, int i)
{
data[i/32] |= 1 << (i%32);
}
/* DESCRIPTION:
* ------------
* The function sets a bit with index i as inactive (0) in the parameter
* binary data.
*
* PARAMETERS:
* ------------
* unsigned char* data: an array of binary data.
* int i: the index of the bit which to set as active (1).
*
* RETURNS:
* ------------
* Nothing.
*
*/
void op_bit_unset(unsigned char *data, int i)
{
data[i/32] &= ~(1 << (i%32));
}
当我用给定的数据进行测试时,我的答案是错误的。
unsigned char arr[2] = {0, 0};
op_bit_set(arr, 0);
*** Testing your op_bit_set function..
At first arr[0] == 0x00 and arr[1] == 0x00
Setting bit 0 arr[0] is 0x01, should be 0x80
arr[1] is 0x00, should be 0x00
我的回答基于我在此处阅读的内容:http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/bit-array.html
正如 Thomas 所提到的,您使用的是 8 位的无符号字符。所以你确定索引和位数的逻辑应该使用 8 而不是 32.
此外,由于最高有效位是最左边的位,因此您必须将 0x80(或 128)向右移动 i,这为您提供了要 set/unset 的特定位的掩码。
最终代码应该是这样的。
void op_bit_set(unsigned char *data, int i)
{
data[i/8] |= 0x80 >> (i%8);
}
void op_bit_unset(unsigned char *data, int i)
{
data[i/8] &= ~(0x80 >> (i%8));
}
我正在为无符号字符数组中的 set/unset 位编写一个函数。我收到了以下说明。
In this task, bit 0 is the most significant bit. It is also assumed that unsigned char is exactly 8 bits (1 byte). Thus, for example, bit 8 is a leftmost bit in second unsigned char byte and bit 17 is the second highest bit in the third unsigned char byte. Thus, examining the number 170 (0xAA hexadecimal format, 10101010 in binary), the most significant bit, ie bit 0 has a value of 1.
基于此,我写了以下内容。
/* NOTE:
* -----------
* The parameter binary data (const unsigned char*) in all the functions
* below is likely to hold more than 8 bits. You should take this into
* account when implementing your functions.
*/
/* DESCRIPTION:
* ------------
* The function sets a bit with index i as active (1) in the parameter
* binary data.
*
* PARAMETERS:
* ------------
* unsigned char* data: an array of binary data.
* int i: the index of the bit which to set as active (1).
*
* RETURNS:
* ------------
* Nothing.
*
*/
void op_bit_set(unsigned char *data, int i)
{
data[i/32] |= 1 << (i%32);
}
/* DESCRIPTION:
* ------------
* The function sets a bit with index i as inactive (0) in the parameter
* binary data.
*
* PARAMETERS:
* ------------
* unsigned char* data: an array of binary data.
* int i: the index of the bit which to set as active (1).
*
* RETURNS:
* ------------
* Nothing.
*
*/
void op_bit_unset(unsigned char *data, int i)
{
data[i/32] &= ~(1 << (i%32));
}
当我用给定的数据进行测试时,我的答案是错误的。
unsigned char arr[2] = {0, 0};
op_bit_set(arr, 0);
*** Testing your op_bit_set function..
At first arr[0] == 0x00 and arr[1] == 0x00
Setting bit 0 arr[0] is 0x01, should be 0x80
arr[1] is 0x00, should be 0x00
我的回答基于我在此处阅读的内容:http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/bit-array.html
正如 Thomas 所提到的,您使用的是 8 位的无符号字符。所以你确定索引和位数的逻辑应该使用 8 而不是 32.
此外,由于最高有效位是最左边的位,因此您必须将 0x80(或 128)向右移动 i,这为您提供了要 set/unset 的特定位的掩码。
最终代码应该是这样的。
void op_bit_set(unsigned char *data, int i)
{
data[i/8] |= 0x80 >> (i%8);
}
void op_bit_unset(unsigned char *data, int i)
{
data[i/8] &= ~(0x80 >> (i%8));
}