Weird bit-shift behavior on void*.
C. Dunn
cdunn2001 at gmail.com
Thu Aug 9 18:08:14 PDT 2007
I wish I could provide a testcase, but the problem is too weird:
void* q = cast(void*)0x402A8000;
writefln("%x %x %d %d %x %x", q, v, q.sizeof, v.sizeof, (cast(size_t)q >> 48),(cast(size_t)v >> 48));
'v' is a void* too. It is set during the program, and it is a real pointer address. According to the output, q and v have the same value and the same data-type size:
402A8000 402A8000 4 4 0 402a
(This output comes directly from the writefln as shown.)
So why do they have different bit-shifted results? The weird thing is, the bit-shift operand on 'v' is taken mod 32. It gives '0' for 'v >> 32' and repeats the previous sequence up to 'v >> 64', etc.
But I cannot produce a testcase with this behavior because when I create a void* from the value of 'v' (e.g. 'q') I do not see the weird behavior. The issue is not '>>' v. '>>>'; I've tried both.
The solution is to use (cast(ulong)v >> 48). (Again, '>>' v. '>>>' makes no difference.) Then I get:
402A8000 402A8000 4 4 0 0
as expected. But the cast(size_t) on 'q' works fine.
Does anyone have any idea what is going on here? It is very magical, and it cost me a TON of debug time.
More information about the Digitalmars-d
mailing list