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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How do you define flags?

How do you define flags?

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 3 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.
  • D Offline
    D Offline
    Dusan Paulovic
    wrote on last edited by
    #1

    There are many options to define values for flags, here is a list of known by me: - Hexadecimal: 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, ... - Decimal: 1, 2, 4, 8, 16, 32, 64, 128, ... - Bit Shifting of 1: 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, ... - 2 * previous a=1, b=a*2, c=b*2, d=c*2, e=d*2, ... Do you know any other? Which do you consider as the best, and why? Do you define (give it a name) an empty member (0) in flags?

    L A 2 Replies Last reply
    0
    • D Dusan Paulovic

      There are many options to define values for flags, here is a list of known by me: - Hexadecimal: 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, ... - Decimal: 1, 2, 4, 8, 16, 32, 64, 128, ... - Bit Shifting of 1: 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, ... - 2 * previous a=1, b=a*2, c=b*2, d=c*2, e=d*2, ... Do you know any other? Which do you consider as the best, and why? Do you define (give it a name) an empty member (0) in flags?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2
      1. Any other is just a variation of the above, which are all basically the same. The only 'difference' is the visual representation.
      2. Hexadecimal is best as it makes it obvious what each bit represents, and is easy to visualise when flags are used in combination.
      3. Only define a zero value if it has any specific meaning.
      1 Reply Last reply
      0
      • D Dusan Paulovic

        There are many options to define values for flags, here is a list of known by me: - Hexadecimal: 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, ... - Decimal: 1, 2, 4, 8, 16, 32, 64, 128, ... - Bit Shifting of 1: 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, ... - 2 * previous a=1, b=a*2, c=b*2, d=c*2, e=d*2, ... Do you know any other? Which do you consider as the best, and why? Do you define (give it a name) an empty member (0) in flags?

        A Offline
        A Offline
        Albert Holguin
        wrote on last edited by
        #3

        I mostly use hex flags, I think that's the most common method. It's probably because it's a lot clearer which bits are actually set (for that same reason, I don't think I've ever seen decimal used for flags since the conversion isn't as neat). Empty flags have a definition if they have a meaning (i.e. not necessary but could be labeled for clarity).

        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