[C++] Bit shifting

Started by
3 comments, last by Mybowlcut 14 years, 3 months ago
I'd like to store the type of an object that an ID belongs to in the ID:
enum
{
    TYPE_VEHICLE   = 0x0002,
    TYPE_STRUCTURE = 0x0040
};

int main(int argc, char* argv[])
{
    size_t vehicle_id = TYPE_VEHICLE | 0;
    size_t structure_id = TYPE_STRUCTURE | 0;

    size_t another_vehicle_id = TYPE_VEHICLE | 1;
    size_t another_structure_id = TYPE_STRUCTURE | 1;

    return 0;
}
It seems to work, but was just wondering if anyone can see any potential issues with it? For example, if I change the enum around (I am basing it off what I have seen in other libraries), I can have two different IDs of the same "type" that have the same value:
enum
{
    TYPE_VEHICLE   = 0x0001,
    TYPE_STRUCTURE = 0x0002
};
vehicle_id and another_vehicle_id would both be 1. Will this issue come up again now that I have changed the enum values? Are any other issues likely to arise? Cheers.

Advertisement
If you really wanted to do this, I would suggest that you reserve the high order bits for the type, and the low order bits for the ID.

From your example it seems like you want to use short values, which will allow you to have a maximum of 256 objects of each type:

enum{	TYPE_VEHICLE   = 0x0100,	TYPE_STRUCTURE = 0x0200,	...	TYPE_LAST      = 0xFF00};int main(int arg_count, char** args){	size_t vehicle_id = TYPE_VEHICLE | 0;			// id = 0x0100	size_t structure_id = TYPE_STRUCTURE | 0;		// id = 0x0200	size_t another_vehicle_id = TYPE_VEHICLE | 1;		// id = 0x0101	size_t another_structure_id = TYPE_STRUCTURE | 1;	// id = 0x0201	size_t last_vehicle_id = TYPE_VEHICLE | 255;		// id = 0x01FF	size_t last_structure_id = TYPE_STRUCTURE | 255;	// id = 0x02FF	return 0;}


Just be sure that you don't have more than 256 objects and you can do this fine, otherwise use unsigned integers instead.
Quote:Original post by Wavarian
If you really wanted to do this, I would suggest that you reserve the high order bits for the type, and the low order bits for the ID.

From your example it seems like you want to use short values, which will allow you to have a maximum of 256 objects of each type:

*** Source Snippet Removed ***

Just be sure that you don't have more than 256 objects and you can do this fine, otherwise use unsigned integers instead.

What do you mean by use unsigned integers? size_t is an unsigned int, right?

Is 256 objects of each type the limitation that I have currently? What are the highest order bits that can be used to allow the maximum amount of IDs?

Cheers.

How many types do you want?

Maximum of 256 types:

enum{	TYPE_FIRST     = 0x00000000,	TYPE_VEHICLE   = 0x01000000,	TYPE_STRUCTURE = 0x02000000,	...	TYPE_LAST      = 0xFF000000};


Then you have 3 bytes left to contain the object's ID, 256^3 = 16777216 object IDs:

int main(int arg_count, char** args){	size_t vehicle_id = TYPE_VEHICLE | 0;			// id = 0x01000000	size_t structure_id = TYPE_STRUCTURE | 0;		// id = 0x02000000	size_t another_vehicle_id = TYPE_VEHICLE | 1;		// id = 0x01000001	size_t another_structure_id = TYPE_STRUCTURE | 1;	// id = 0x02000001	size_t last_vehicle_id = TYPE_VEHICLE | 16777215;	// id = 0x01FFFFFF	size_t last_structure_id = TYPE_STRUCTURE | 16777215;	// id = 0x02FFFFFF	return 0;}
Oops, sorry... meant to say object IDs instead of objects of each type. I'll probably only ever need 2 types. So I'll change my enum to:

enum{    TYPE_VEHICLE   = 0x01000000,    TYPE_STRUCTURE = 0x02000000};


Thanks!

This topic is closed to new replies.

Advertisement