Way to encode bits in signed integer

B

Balban

Hi,

I use a signed integer as some primary id. I want to associate a
second id with this - If the primary id is 32 bits, I could use one
byte of it to encode a secondary id.

E.g. If you think it was unsigned:

0x00FF FFFF -> This could be the primary id mask
0xFF00 0000 -> Could be the secondary id mask

I could easily encode the secondary id by bitwise shifting/anding/
orring.

If the number is signed, is there any elegant way I could achieve the
same thing?

Thanks,

Bahdir
 
B

Balban

Hi,

I use a signed integer as some primary id. I want to associate a
second id with this - If the primary id is 32 bits, I could use one
byte of it to encode a secondary id.

E.g. If you think it was unsigned:

0x00FF FFFF -> This could be the primary id mask
0xFF00 0000 -> Could be the secondary id mask

I could easily encode the secondary id by bitwise shifting/anding/
orring.

If the number is signed, is there any elegant way I could achieve the
same thing?


I think I am going to encode unsigned special values for any negative
usage of the signed integer, and convert the integer to an unsigned
integer. That seems to be the simplest solution. E.g.

#define ID_INVALID 0xFFFFFFFF
#define ID_SPECIAL 0xFFFFFFFE

And use ids, with secondary and primary as normal, e.g. 0x10000001,
0x20000001, 0x30000001, where primaries are all 1, secondaries are 1,
2, 3

Thanks,

Bahadir
 
E

Eric Sosman

Balban said:
Hi,

I use a signed integer as some primary id. I want to associate a
second id with this - If the primary id is 32 bits, I could use one
byte of it to encode a secondary id.

E.g. If you think it was unsigned:

0x00FF FFFF -> This could be the primary id mask
0xFF00 0000 -> Could be the secondary id mask

I could easily encode the secondary id by bitwise shifting/anding/
orring.

If the number is signed, is there any elegant way I could achieve the
same thing?

No, I don't think so. Inelegant ways abound, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top