1

I'm writing a function to set/unset bits in an unsigned char array. I've been given the following instructions.

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.

based on that, I wrote the following.

/* 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)); 
}

When I test it with the given data, my answer is wrong.

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

I based my answer on what I read here: http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/bit-array.html

6
  • i/32 should be i/8. Commented Jul 24, 2020 at 14:55
  • And i % 32 should be i % 8 Commented Jul 24, 2020 at 14:56
  • You use unsigned char, but then treat is as a uint32_t, with 32 bits of contents. If it is supposed to be using unsigned char, then all the 32 should be 8. Commented Jul 24, 2020 at 14:56
  • 2
    - and the bit offset to shift with should be reversed (7-(i%8)) since the most significant bit has the lowest index here. Commented Jul 24, 2020 at 14:57
  • The code you copied from uses unsigned int arrays, not unsigned char arrays. Commented Jul 24, 2020 at 14:57

1 Answer 1

2

As Thomas mentioned, you are using an unsigned char which has 8 bits. So your logic for determining the index and the bit number should use 8 instead of 32.

Also, since the most significant bit is the left most bit, you have to shift right 0x80(or 128) by i which gives you a mask for the specific bit that you want to set/unset.

In the end the code should look like this.

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)); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.