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.
  • 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
                          • P PIEBALDconsult

                            Does not -- they may very wel be related.

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

                            The fact that they "may" be related doesn't create a relation.

                            Bastard Programmer from Hell :suss:

                            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