Thanks for the additional comments.
Victor said:
*** What does this line do? i.e., How does it translate into the format
"[l-value] = [something]"?
*(reinterpret_cast<char*>(&a) + sizeof(a) - 1) ^= 0x80;
If P is a char*, then P + N is also a char*. *(P + N) is a char&
(a reference to a char).
Yes... But here it seems we have *(char* + int), not *(char* + int*):
*(reinterpret_cast<char*>(&a) + sizeof(a)-1) ^= 0x80;
If I could see how we *do* have *(char* + int*), then I could understand
why adding a mere value of "sizeof(a) - 1" accomplishes a bit shift (see
below).
** Why does it accomplish a bit shift??????
That's the _last_, the most significant, byte of 'a' (if your
system is little-endian). What else would I use?
I was/am confused why you are adding the mere value "sizeof(a)-1". This
seems to be accomplishing a bit shift of "(sizeof(a)-1)" bytes (bytes,
not bits!) left, because of what I said above.
OK, you could write
char *pa = reinterpret_cast<char*>(&a) + sizeof(a) - 1; // address
char &ra = *pa; // get a reference from the address
ra = ra ^ 0x80; // flip the bit in the char using the reference.
I just wrote it in one statement. (Of course this works only on
a system that is little-endian and has 8-bit chars)
I supposed you need to read about compound assignment operators
as well. Have you ever seen a += b? If yes, why a ^= b looks
so foreign to you?
Yes, of course I've seen "a += b". That's in piddly Java, my 1st piddly
language. I've just never seen "(a + c) += b", which is more similar to
your "(*(reinterpret_cast<char*>(&a) + sizeof(a)-1) ^= 0x80)". (a + c)
is not an l-value, so I didn't see how (*(reinterpret_cast<char*>(&a) +
sizeof(a)-1) could be an l-value. Seems this is one more rule that is
different for pointers than for primitives.
** Why is (*(reinterpret_cast<char*>(&a) + sizeof(a)-1) an l-value,
allowed to be used on the LHS of an assignment?????
I took a guess, thinking that one would unroll "(a + c) += b" (if it
were legal, which it's *not*) as "a = (a + c) + b" (where the pluses
don't necessarily have to be the same operator). So, I unrolled
"(*(reinterpret_cast<char*>(&a) + sizeof(a)-1) ^= 0x80)" as "a =
If not, well, open your textbook...
If only I knew what to look up.
It's all about knowing the buzz
words, the Google terms, the jargon. I've got "The C++ Programming
Language" by Bjarne Stroustrup, but I haven't seen anything more than
the basic "a += b" in that.
I ask questions only when I don't know what to look up, or when I'm
confused by what I've looked up. Having been a teaching assistant, I
understand the annoyance of having students ask questions unprepared.
I'll get it eventually...
Suzanne