Help needed in an Interview question

The question : You need to write a function “changebit”. The parameters passed to this function are a void pointer variable that points to the address of some content - This content can be an integer array, string, floats etc. You also are specified the length of this content. You are also given another parameter called, say, “i”. You are supposed to flip the i- th bit of the content given before (first parameter). How can I do this? Please help. I hope the question description I provided is clear.

I guess this would be one approach :

#include <iostream>
#include <cassert>

/*
 * Flips ith bit assuming 0-indexing
 */

void changeBit (void* ptr, const int n, const int i)
{
    assert (i < n);
    
    char* charArr = static_cast<char *> (ptr);
    const int charIdx = (i - 1) >> 3;
    const int bitIdx = (i - 1) & (0b111);
    
    charArr[charIdx] ^= (1 << bitIdx);
}

int main (void)
{
    long long x = 1;
    std::cout << "before : " << x;
    
    changeBit (&x, sizeof (x) * 8, 2);
    std::cout << " after " <<  x << std::endl;
    
    double y = 1;
    std::cout << "before : " << y;
    
    changeBit (&y, sizeof (y) * 8, 62);
    std::cout << " after " <<  y << std::endl;
    
    int z[10];
    
    std::cout << "before : ";
    for (int i = 0; i < 10; ++i)
        std::cout << z[i] << ' ';
    
    changeBit (z, sizeof (z) * 8, 204);
    std::cout << "after : ";
    for (int i = 0; i < 10; ++i)
        std::cout << z[i] << ' ';
    std::cout << std::endl;
    
    return 0;
}

Can someone please help me with this:

“This content can be an integer array, string, floats” What do we mean by the flipping ith bit of a string or float. Like if string is “abcdefg” and we have to flip the 2 bit how will we do that. I guess flipping bit is in context to the binary representation of anything but when array and string come into the picture what is the binary representation.
If i know the binary representation of anything i can change the ith bit by using :

x=x^1<<i

This might be a silly question but I am new to Bit Manipulation.
Please Help.

Can you please cite any resource where i can read more about it(related to your ans).

Link to bit manipulation : Topcoder

Link to representation of numbers : Topcoder

I’m not quite certain what part of changeBit() you’re having trouble understanding.

charIdx = (i - 1) >> 3 <==> (i - 1) / 8

bitIdx = (i - 1) & 0b111 <==> (i - 1) % 8

In my approach, I’m simply toggling the bitIdx-th bit of charIdx-th byte (char). The 0-th bit and byte are respectively the first bit and byte.

@yoda_101 yes it is bit clear now but this method of toggling is new for me so I didn’t get the part of dividing 8 and modulo with 8.

What I have known till now is that just take a mask(value is 00000…01 initially) and shift the number 000…01 by the position we want to flip the bit and then take xor with the actual number on which we are working with.

x = x^(1<<position)

I guess you are doing something similar but the process is not completely clear to me.
Am I right about comparing my logic to what you have done. Please correct if wrong. Thanks