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.

    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