|
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
|
|
|
|
|
|
OP wrote: 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.
It does.
Bastard Programmer from Hell
|
|
|
|
|
Does not -- they may very wel be related.
|
|
|
|
|
The fact that they "may" be related doesn't create a relation.
Bastard Programmer from Hell
|
|
|
|
|
Personally I don't think his question implies that much, so I'd like to hear the OP's explanation.. not that it looks like he's going to give one.
|
|
|
|
|
How about an enumeration?
|
|
|
|
|
Exactly.
Regards,
Rob Philpott.
|
|
|
|
|
I'd rather have 10 bulls bools than just 1
|
|
|
|
|
Yo dude. Enumeration is the simplest answer.
joe
|
|
|
|
|
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."
|
|
|
|
|
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.
|
|
|
|
|
Bad idea (sometimes). Property grids (i.e. in designers) don't play nicely with flag enums. Also, flag enums don't play nicely with data binding if you are using WPF or even Winforms.
|
|
|
|
|
SledgeHammer01 wrote: flag enums don't play nicely with ... Winforms.
I haven't had that problem.
|
|
|
|
|
As stated above, use an enum for your bool 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.
modified 25-Nov-11 10:26am.
|
|
|
|
|
If you want this to cover a flag uint from interop or something else where you don't want an enum, you can do something like
public struct Flags {
public uint Value;
public bool this[int i] {
get { return 1 & (Value >> i); }
set { uint mask = 1 << i; Value = (Value & ~mask) | (value ? 1 : 0) << i; }
}
}
... to provide indexed access to a set of flags.
|
|
|
|
|
But I think that can only test one bit at a time.
|
|
|
|
|
I have a desktop application developed in c# .net for winows 7 opearating system.Will I be able to install the same application on my tablet pc and use it.It has user actiona like button click how will this events be handled in tablet pc? Can some one help me in this front.
|
|
|
|
|
What happens when you try it?
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
A button click is still a button click whether or not a finger or a mouse pressed it.
|
|
|
|
|
hello please help me to make md4 Hashe in c#.
i Find some codes but not work Correctly.
i want C# code for Make Md4 Hash and me love Summary code
|
|
|
|
|
You do realise that MD4 was smashed like 20 years ago? If this is a new application, I would suggest you use SHA1, or at least MD5. If you really need to implement MD4 to support an older app, MD4 - Koders.com[^] is an implementation that is open source.
Hope this helps.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
yes i want MD4 realy.
but can not run this code
i want real project that can run in computer
|
|
|
|