Unique ID

Started by
21 comments, last by ultramailman 11 years ago

Well the solution is to mask out the bits you don't want before you do the shift... looks like your x, y are 8 bits and n is 16 bits so do

int32_t id = (n & 0xffff) | ((cell_x & 0xff) << 16) | ((cell_y & 0xff) << 24)

EDIT: Missed an effing eff

Ahhh would that work? By masking them out, the signedness of the three fields would be lost, wouldn't it? I do intend for them to be signed numbers.

For now I am using this:

// cast to unsigned
uint32_t n = uid.n, cell_x = uid.cell_x, cell_y = uid.cell_y;
uint32_t id = n | (cell_x << 16) | (cell_y << 24);
I am fine with storing the id as unsigned, as long as the bits of the three fields don't get changed by the casting.
Advertisement

It's not going to work for signed numbers since negative numbers are all 1s in the most significant bits.

If you cast them to signed char or signed short when you extract them it will be ok though.

Try it anyway... the wonders of printf or cout will show you what is going on...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Haha thanks, I didn't think of printing. You are right, masking does the job.

This topic is closed to new replies.

Advertisement