Sorry for posting after so long of other posts but i have a question.
Why don't we use unions if we want to manipulate specific bits in int or any kind of data and we use this pointer configuration which, at least for me, makes the program hard to understand??
I suppose that it is better if you want to change only one value but i think that if you want to do manyand different changes it would be difficult to use this syntax all the time... And it also makes it harder to find a logical error...
A union is a limited-class that has different member variables. But you can only ever assign a value to 1 of them at a time. So if you had union with a double and an int. Only one of them could hold a value at any time. This is because they both share the same memory space.
There really isn't any relation to a union and manipulating the bits of another type? Unless you want to clarify the relationship you see?
Most bit comparison/modifcation is done using the | or & or << or >> operators (bitwise operators).
Edit: 1 Char is 1 Byte, 1 Byte = 8 Bits.
unsigned int = 4 bytes = 32 Bits. :)
Sorry about that, i corrected it...
What i mean is having a union like this one:
1 2 3 4
union num_char{
unsignedint num;
unsignedchar ch[sizeof(unsignedint)];
};
You can change the int and the same time access its bits with the character. It is almost what you did before but it uses union instead of moving a pointer.