Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Use single bool and bit flags for other bools.

Use single bool and bit flags for other bools.

Scheduled Pinned Locked Moved C#
csharpperformancetutorial
27 Posts 12 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Pete OHanlon

    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

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #7

    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.

    P L 2 Replies Last reply
    0
    • P PIEBALDconsult

      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.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #8

      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

      N 1 Reply Last reply
      0
      • P PIEBALDconsult

        How about an enumeration?

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #9

        Exactly.

        Regards, Rob Philpott.

        1 Reply Last reply
        0
        • V V K 2

          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.

          S Offline
          S Offline
          SilimSayo
          wrote on last edited by
          #10

          I'd rather have 10 bulls bools than just 1 :)

          1 Reply Last reply
          0
          • P PIEBALDconsult

            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.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #11

            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:

            P L 2 Replies Last reply
            0
            • V V K 2

              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.

              J Offline
              J Offline
              jtstanish
              wrote on last edited by
              #12

              Yo dude. Enumeration is the simplest answer.

              joe

              B 1 Reply Last reply
              0
              • P Pete OHanlon

                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

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #13

                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

                1 Reply Last reply
                0
                • J jtstanish

                  Yo dude. Enumeration is the simplest answer.

                  joe

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #14

                  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."

                  1 Reply Last reply
                  0
                  • V V K 2

                    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.

                    S Offline
                    S Offline
                    SledgeHammer01
                    wrote on last edited by
                    #15

                    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.

                    P 1 Reply Last reply
                    0
                    • V V K 2

                      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.

                      B Offline
                      B Offline
                      Bernhard Hiller
                      wrote on last edited by
                      #16

                      As stated above, use an enum for your bools. 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);
                      }

                      P 1 Reply Last reply
                      0
                      • L Lost User

                        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:

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #17

                        Does it?

                        L 1 Reply Last reply
                        0
                        • B Bernhard Hiller

                          As stated above, use an enum for your bools. 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);
                          }

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #18

                          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.

                          1 Reply Last reply
                          0
                          • S SledgeHammer01

                            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.

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #19

                            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.

                            S 1 Reply Last reply
                            0
                            • L Lost User

                              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:

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #20

                              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.

                              1 Reply Last reply
                              0
                              • V V K 2

                                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.

                                B Offline
                                B Offline
                                BobJanova
                                wrote on last edited by
                                #21

                                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.

                                P 1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  Does it?

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #22

                                  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 :suss:

                                  P 1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    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.

                                    S Offline
                                    S Offline
                                    SledgeHammer01
                                    wrote on last edited by
                                    #23

                                    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.

                                    P 1 Reply Last reply
                                    0
                                    • L Lost User

                                      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 :suss:

                                      P Offline
                                      P Offline
                                      PIEBALDconsult
                                      wrote on last edited by
                                      #24

                                      Does not -- they may very wel be related.

                                      L 1 Reply Last reply
                                      0
                                      • B BobJanova

                                        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.

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #25

                                        But I think that can only test one bit at a time.

                                        1 Reply Last reply
                                        0
                                        • S SledgeHammer01

                                          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.

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #26

                                          SledgeHammer01 wrote:

                                          flag enums don't play nicely with ... Winforms.

                                          I haven't had that problem.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups