Straight of the bat I will say I barely know what I'm doing here - I'm having a major trouble grasping bitwise operators in C. As an exercise in one of my courses I'm supposed to hide a number (unsigned int) in large pointer array (unsigned char) containing numbers. I'm doing it using srand (with key, so that I can decode it later) to choose specific elements of the array and then take one bit of the number I'm supposed to hide (iterating through all the bits) and changing the least significant bit of the array element. The choosing elements.
While I get the general idea I cannot, despite googling, figure out the bit operations. So having the size
that I'm supposed to encode in i-th run of the loop (i-th
bit of size) and randomly chosen current_element
this is what I came up with to get the bit and then alter the element.
for (i=0; i<32; i++){
tmp = rand() % max;
current_element = array[tmp];
current_element ^= ((size >> i) & 0x01)<<7;
}
To decode I would write it analogically (where size is wiped unsigned char that I'm trying to write the decoded number to):
for (i=0; i<32; i++){
tmp = rand() % max;
current_element = array[tmp];
size = size ^ ((current_pixel.blue<<0)<<7);
}
These two are in diffrent functions and srand()
is seeded anew in each of them beforehand.
But these are clearly not working and I don't even know which one (I can only check if it decoded correctly). Truth be told these are mostly copied from other things I found online as so far operating on individual bits eludes me. So I'd be grateful for some sort of advice on what is wrong here (and I'm aware probably everything is wrong here and it's all gibberish but I've been trying to fix it to no avail for quite a while now).