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. Quick bit field question

Quick bit field question

Scheduled Pinned Locked Moved C#
questioncsharpc++
3 Posts 2 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.
  • B Offline
    B Offline
    benglish72
    wrote on last edited by
    #1

    Hi, how do I extract values from a bit field in c#? I tried it a la C++ if(flag.value & enum.value) { } but it wouldn't let me go that way, thought it was boolean or something odd. Anyway, I've had a quick look thru previous posts (takes a bloody long time to load pages!) but forgive me if it's been covered. Thanks, Brian. "Ergo huffabo et puffabo et tuam domum inflabo" ait magnus malus lupus. "Non per comam men-men-menti!" ait porcellus.

    M 1 Reply Last reply
    0
    • B benglish72

      Hi, how do I extract values from a bit field in c#? I tried it a la C++ if(flag.value & enum.value) { } but it wouldn't let me go that way, thought it was boolean or something odd. Anyway, I've had a quick look thru previous posts (takes a bloody long time to load pages!) but forgive me if it's been covered. Thanks, Brian. "Ergo huffabo et puffabo et tuam domum inflabo" ait magnus malus lupus. "Non per comam men-men-menti!" ait porcellus.

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      It's the other way round: the if statement requires the condition to evaluate to type bool in C#. You can't simply test an integer. If enum.value represents a single flag, use

      if( ( flag.value & enum.value ) == enum.value )
      {
      }

      If it represents a mask of flags, and you want to know if any of the masked flags are set, use

      if( ( flag.value & enum.value ) != 0 )
      {
      }

      You might need to add the FlagsAttribute to your enum for the latter to work. Stability. What an interesting concept. -- Chris Maunder

      B 1 Reply Last reply
      0
      • M Mike Dimmick

        It's the other way round: the if statement requires the condition to evaluate to type bool in C#. You can't simply test an integer. If enum.value represents a single flag, use

        if( ( flag.value & enum.value ) == enum.value )
        {
        }

        If it represents a mask of flags, and you want to know if any of the masked flags are set, use

        if( ( flag.value & enum.value ) != 0 )
        {
        }

        You might need to add the FlagsAttribute to your enum for the latter to work. Stability. What an interesting concept. -- Chris Maunder

        B Offline
        B Offline
        benglish72
        wrote on last edited by
        #3

        Cool, thanks heaps! "Ergo huffabo et puffabo et tuam domum inflabo" ait magnus malus lupus. "Non per comam men-men-menti!" ait porcellus.

        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