C - operations on bits representing a structure

I am trying to write a hashset in C and I found one hash function that hashes according to the bits in the data. I have the following structure:

struct triple
{
    int a;
    int b;
    int c;
};

      

The question is how to get the bit representation from the type object struct triple

? Let's say I want to XOR its bits with an 8 bit integer. How should I do it?

+3


source to share


1 answer


Iterate over all bytes struct

and XOR each separately, for example

void bytexor(unsigned char xor_byte, void *data, size_t size) {
    unsigned char *p = data;
    while (size--) {
        *p++ ^= xor_byte;
    }
}

      

Using:



struct triple my_struct;
// ...
bytexor(0xFF, &my_struct, sizeof my_struct);

      

(Note: This answers the question of how to XOR struct

with a byte. As for implementing a generic hash function based on this, it might not be a good idea as it struct

might have padding, i.e. extra bytes with potentially undefined values ​​unrelated to actual payload field values.)

+5


source







All Articles