How to use a bitflag on an unsigned int in order to store an additional bool value in it?

The fastest and cleanest way is probably to avoid bitfields, and simply define a struct: struct Plane { bool isFlying; unsigned int number; } ... std::vector planes; Plane p; p. IsFlying = true; p. Number = 777; planes.

Push_back(p) This method will use more memory than trying to cram the flag into the same word, but will take less work to get/set the fields. Unless you're memory-constrained, I would strongly recommend avoiding trying to pack everything tightly You could even consider using an enum rather than a bool for the state.

The fastest and cleanest way is probably to avoid bitfields, and simply define a struct: struct Plane { bool isFlying; unsigned int number; } ... std::vector planes; Plane p; p. IsFlying = true; p. Number = 777; planes.

Push_back(p); This method will use more memory than trying to cram the flag into the same word, but will take less work to get/set the fields. Unless you're memory-constrained, I would strongly recommend avoiding trying to pack everything tightly. You could even consider using an enum rather than a bool for the state.

I admit, a struct is easier to handle. Do you know anything about the speed difference? I mean turning a bit directly sounds to be faster than involving all the code a struct carries around.

– Lumpi Nov 7 at 19:52 3 @Lumpi: A struct doesn't require any code at all. Accessing a field via a struct is simply a memory access. Bitfields require a memory access + bit manipulation (two memory accesses for a write, in fact).

– Oli Charlesworth Nov 7 at 19:54 @Lumpi: To be honest, the likelihood that this will be your bottleneck is small. But bit-packing implies you're micro-optimising, and the struct is unlikely to be slower (unless you're doing large amounts of memory copying). It's also cleaner.

– Oli Charlesworth Nov 7 at 19:55 Ok, so using bits is more an advantage when memory space is precious, and not when speed is the issue!? – Lumpi Nov 7 at 19:58 @Lumpi: Using more bits is obviously a disadvantage if memory is precious.It's also a disadvantage if your bottleneck is memory bandwidth. If your only bottleneck is CPU bandwidth, then the struct will be faster.

– Oli Charlesworth Nov 7 at 20:01.

Struct Stuff { unsigned int Boing: 31; unsigned int isFlying: 1; }; . . .

Stuff myStuff; myStuff. Boing = 777; myStuff. IsFlying = false; More on bitfields.

One simple way would be to just make the number negative if not flying.

Nice and simple. :) – Rontologist Nov 7 at 19:49 It's nice and simple, but it's also needless ugly (think of the code required to set and get the individual fields) and CPU-intensive. – Oli Charlesworth Nov 7 at 19:51.

Assuming you never use the full range of values available in your unsigned int (a reasonable possibility, but far from absolutely certain), you could just limit the range to one fewer bits than an unsigned in contains, and then use the most significant bit to store the "flying" state. In that case, you could do something like: // This theoretically isn't required to work, but will for most reasonable machines. Unsigned flying_bit = 1 = 0; } Another possibility would be to use actual bitfields: struct plane { uint32_t model: 31; uint32_t flying: 1; }; In this case, you just assign values directly, like: plane myplane = {777, 0}; myplane.

Flying = 1; // take off myplane. Flying = 0; // land.

Positive values are flying, negative are not, and zero is invalid.

Use a struct or a class to combine the information "number" and "state", and whatever else you want a plane to be. If you use bit operations on the int (which is possible, just not advisable), changing the state would change the actual number of your plane.

You can pack both the state and the plane number in a unsigned int. Assuming unsigned int is 32bit on your platform unsigned int myplane; int planenumber = (myplane & 0xffff ) //can be the plane number int booleanstate = ((myplane >> 16) & 0xffff) //can be the boolean state.

If you do not need more states then you are done. If you need to you can also use for each state (e.g. Crashed, repair needed a separate bitvector so you use exactly one bit for each plane state you actually need. But I doubt that speed well be your main concern if you deal with a game graphics and physics will eat up most of your CPU time compared to operations on your plane states.

#include #include using namespace boost; class State { public: State(int nPlanes):_States(dynamic_bitset(nPlanes)) { } void SetState(int nPlane, bool bFly) { _StatesnPlane = bFly; } void Dump() { for (boost::dynamic_bitset::size_type I = 0; I SetState(0, true); planes. SetState(5,true); planes.Dump(); return 0; }.

Let's say your unsigned int has 32 bits: #define SET_FLYING(x) (x) |= (1If anyone wants to tell me "No no! Don't use macros, this is C++, use this overly complicated thing I wrote", save your breath. Just give the -1 and move on.

Edit: What I wrote above was the answer to your question: "How to use the sign bit as a flag". If you want to do this in a better way, yet don't expand your memory usage (by adding a bool), you can always write something like this: struct airplane { unsigned int id: 31; unsigned int is_flying: 1; }; airplane Boing = {777, false}; Then, assigning to and reading from id or is_flying does the bit operations, but they are handled by the compiler.

Use macros, by all means! But enclose them in parentheses, otherwise they won't work as expected. In particular, it's a really really good idea to define IS_FLYING(x) as ((x) & (1– TonyK Nov 7 at 20:12 Consider IS_FLYING(x) == IS_FLYING(y).

With your "not too fancy" macros, it's just not going to work -- or rather, gives wrong results but the bug stays hidden. With those "overly complicated" inline functions everything would work as expected, and it wouldn't cost anyone anything.P.S. There's only one way to completely avoid criticism on this site... – eq- Nov 7 at 20:15 P.P.S.If you mean sizeof x * 8 - 1, why don't you just write it that way? The value's returned by the sizeof expression is (in this case anyway) a compile-time constant, and there's nothing to micro-optimize.

Seemingly not grasping what can and what cannot be micro-optimized to begin with simply screams "bad at C" as much as does not parenthesizing one's macros. – eq- Nov 7 at 20:28 @eq-, about parenthesizing them, I have to admit I wrote the post in a hurry and left it out (call for dinner). You could see I am used to parenthesizing by all the (x)s.

Second, if you see ((sizeof(x)Many things in C result have a shared result, but convey different meaning. If what you're trying to accomplish in multiplication, use the multiplication operator (i.e. *).

It's that simple. – eq- Nov 7 at 22:12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions