Frage

I'm building a data structure which sets into 64 bit integers a set bit every 2 or 3 bits.

It would be convenient for me if I could alternately also store a pointer in this structure (when they are terminal nodes in a tree, but that's not relevant).

For the purposes of my representation (which guarantees either the highest or second highest bit are set), if it can be assumed that pointers never have their highest two bits set, i.e. this assertion holds:

void *sixty_four_bit_pointer = a_valid_address(); 
bool always_zero = 0xC000000000000000 & sixty_four_bit_pointer;

then I can do this trick!

War es hilfreich?

Lösung

This is not safe in general. It's programs that pull tricks like this that led to the /LARGEADDRESSAWARE flag in Windows. (Aka "this is why we can't have nice things.")

What you can do is use the bottom bits instead of the top bits. Since your structure contains a pointer, it is already 8-byte aligned, which means that the bottom three bits are always 0, so you can use those bits as tag bits.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top