Use single bool and bit flags for other bools.
-
Hi, I have 10 bool variables in a class. Instead of creating 10 bools, how to create a single bool and bit flags for remaining 9 bool variables inorder to efficiently use memory. Can I have a code snippet in C# for this.. Thanks in Advance.
Why on earth would you need to do this? That's optimising beyond where you really need to go. A bool holds a negligible amount of memory, and the code you need to translate backwards and forwards from the bit flag would actually take up more memory than you would save in a single instance. Added to this that converting the bit flags takes clock cycles and you could end up with a less efficient program than just having nine bools.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Why on earth would you need to do this? That's optimising beyond where you really need to go. A bool holds a negligible amount of memory, and the code you need to translate backwards and forwards from the bit flag would actually take up more memory than you would save in a single instance. Added to this that converting the bit flags takes clock cycles and you could end up with a less efficient program than just having nine bools.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
I can think of plenty of reasons for this - none of which have directly to do with optimization (not for speed anyway). For example, - the flags are a field in a structure used in interop. (happens all the time) - the flags are delivered as packed flags and later have to be passed on as flags. Saves a lot of setting up and tearing down to just leave them packed. - when a switch over the packed flags (or a subset of them) is shorter and clearer than a massive tree of if's. (uncommon) - when operations done on the flags can be done by short expressions of arithmetic and bitwise operations, and doing it manually with bools would create a huge mess. (ok this one is pretty rare, unless you write emulators)
-
I can think of plenty of reasons for this - none of which have directly to do with optimization (not for speed anyway). For example, - the flags are a field in a structure used in interop. (happens all the time) - the flags are delivered as packed flags and later have to be passed on as flags. Saves a lot of setting up and tearing down to just leave them packed. - when a switch over the packed flags (or a subset of them) is shorter and clearer than a massive tree of if's. (uncommon) - when operations done on the flags can be done by short expressions of arithmetic and bitwise operations, and doing it manually with bools would create a huge mess. (ok this one is pretty rare, unless you write emulators)
But read the question asked. The wording there does not indicate that any of these are the reasons - hence why I asked why he was trying to do it.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
But read the question asked. The wording there does not indicate that any of these are the reasons - hence why I asked why he was trying to do it.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Hi, I have 10 bool variables in a class. Instead of creating 10 bools, how to create a single bool and bit flags for remaining 9 bool variables inorder to efficiently use memory. Can I have a code snippet in C# for this.. Thanks in Advance.
How about an enumeration?
-
Why on earth would you need to do this? That's optimising beyond where you really need to go. A bool holds a negligible amount of memory, and the code you need to translate backwards and forwards from the bit flag would actually take up more memory than you would save in a single instance. Added to this that converting the bit flags takes clock cycles and you could end up with a less efficient program than just having nine bools.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
If a whole bunch of instances of the class are made then there could be a space saving. And if the instances are serialized and sent somewhere. Regardless of concerns of "performance", grouping a bunch of related flags into an enumeration is a good design choice.
-
If a whole bunch of instances of the class are made then there could be a space saving. And if the instances are serialized and sent somewhere. Regardless of concerns of "performance", grouping a bunch of related flags into an enumeration is a good design choice.
You may note that I carefully used the phrase "single instance". The point was to get the OP to think about whether or not they really needed to perform this optimisation.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
How about an enumeration?
Exactly.
Regards, Rob Philpott.
-
Hi, I have 10 bool variables in a class. Instead of creating 10 bools, how to create a single bool and bit flags for remaining 9 bool variables inorder to efficiently use memory. Can I have a code snippet in C# for this.. Thanks in Advance.
-
If a whole bunch of instances of the class are made then there could be a space saving. And if the instances are serialized and sent somewhere. Regardless of concerns of "performance", grouping a bunch of related flags into an enumeration is a good design choice.
-
Hi, I have 10 bool variables in a class. Instead of creating 10 bools, how to create a single bool and bit flags for remaining 9 bool variables inorder to efficiently use memory. Can I have a code snippet in C# for this.. Thanks in Advance.
-
You may note that I carefully used the phrase "single instance". The point was to get the OP to think about whether or not they really needed to perform this optimisation.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Pete O'Hanlon wrote:
to get the OP to think
:laugh: You've been here long enough to know this is not possible :laugh:
No comment
-
jtstanish wrote:
Enumeration is the simplest answer.
Then, please, tell us "why ?" No, I did not down-vote you. best, Bill
"... Sturgeon's revelation. It came to him that Science Fiction is indeed ninety-percent crud, but that also—Eureka!—ninety-percent of everything is crud. All things—cars, books, cheeses, hairstyles, people and pins are, to the expert and discerning eye, crud, except for the acceptable tithe which we each happen to like." early 1950's quote from Venture Sci-Fi Magazine on the origin of Sturgeon's Law, by author Theodore Sturgeon: source Oxford English Dictionary on-line "Word-of-the-Day."
-
Hi, I have 10 bool variables in a class. Instead of creating 10 bools, how to create a single bool and bit flags for remaining 9 bool variables inorder to efficiently use memory. Can I have a code snippet in C# for this.. Thanks in Advance.
Well... I'd have to say you do not provide us enough information. If your bools are related and used internally, yeah, I would group them into a [Flags] type enum. I wouldn't expose an enum from object though. That just doesn't seem like good practice to me. I would have 10 public properties that wrap a [Flags] type enum though. Thats about the same thing as packing them with the binary operators. If you are calling native C++ code, you have to use the binary operators, so there is no choice there, but I'd still have the class expose 10 public properties and wrap all that internally. If its all managed code, well... again it depends on your situation. If you have 10 bools vs. a [Flags] type enum, then the enum is going to be more efficient in transfering on the WIRE... who cares about memory usage... not important at this level. Maybe if you are working on a CE device with limited resources, it may be, but on a PC? you are wasting your time thinking about things like this.
-
Hi, I have 10 bool variables in a class. Instead of creating 10 bools, how to create a single bool and bit flags for remaining 9 bool variables inorder to efficiently use memory. Can I have a code snippet in C# for this.. Thanks in Advance.
As stated above, use an
enum
for yourbool
s. Something like:[Flags]
public enum MyBools : int
{
MyBool1 = 1,
MyBool2 = 2,
MyBool3 = 4,
MyBool4 = 8,
...
MyBool10 = 512,
}
public bool HasBool(int sumOfBools, MyBools specificBool)
{
return ((sumOfBools & specificBool) > 0);
} -
PIEBALDconsult wrote:
Regardless of concerns of "performance", grouping a bunch of related flags into an enumeration is a good design choice.
The question implies unrelated booleans, as a compression technique.
Bastard Programmer from Hell :suss:
Does it?
-
As stated above, use an
enum
for yourbool
s. Something like:[Flags]
public enum MyBools : int
{
MyBool1 = 1,
MyBool2 = 2,
MyBool3 = 4,
MyBool4 = 8,
...
MyBool10 = 512,
}
public bool HasBool(int sumOfBools, MyBools specificBool)
{
return ((sumOfBools & specificBool) > 0);
}Bernhard Hiller wrote:
return ((sumOfBools & specificBool) > 0);
As a general test I prefer to test against the provided value (specificBool) rather than 0, because the specificBool may have multiple bits set. Plus, in .net 4 (if you use that), there is the built-in Enum.HasFlag Method.
-
Well... I'd have to say you do not provide us enough information. If your bools are related and used internally, yeah, I would group them into a [Flags] type enum. I wouldn't expose an enum from object though. That just doesn't seem like good practice to me. I would have 10 public properties that wrap a [Flags] type enum though. Thats about the same thing as packing them with the binary operators. If you are calling native C++ code, you have to use the binary operators, so there is no choice there, but I'd still have the class expose 10 public properties and wrap all that internally. If its all managed code, well... again it depends on your situation. If you have 10 bools vs. a [Flags] type enum, then the enum is going to be more efficient in transfering on the WIRE... who cares about memory usage... not important at this level. Maybe if you are working on a CE device with limited resources, it may be, but on a PC? you are wasting your time thinking about things like this.
SledgeHammer01 wrote:
I wouldn't expose an enum from object though ... I would have 10 public properties
I have either one public property or use a parameter (for a method or the constructor as appropriate) to pass in an Options enumerated value, as with passing options to a Regex.
-
PIEBALDconsult wrote:
Regardless of concerns of "performance", grouping a bunch of related flags into an enumeration is a good design choice.
The question implies unrelated booleans, as a compression technique.
Bastard Programmer from Hell :suss: