using a byte as a bit mask.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bob

    using a byte as a bit mask.

    Hi,
    Having trouble determining if bit n in a byte is on or off.
    Seem to remember being able to do this with the logical And operator
    something like if (2 & mybyte) { do something}
    Maybe I am dreaming of VB days.
    Is there a succint way of doing this?
    thanks
    Bob

  • TAB

    #2
    Re: using a byte as a bit mask.

    Hi Bob

    You are on the right track, try something like this
    if ((bits & 1) != 0) // bit is set if not zero

    where bits is an byte, uint or ulong.
    Exhange 1 for the bit you would like to check, i.e. 1,2,4,8,16,32, ....

    "bob" <startatbob_cle gg@cutthis.adri ley.co.nzskrev i meddelandet
    news:als5f4pfeo 9d8je90kma4vfrc tsluep14v@4ax.c om...
    Hi,
    Having trouble determining if bit n in a byte is on or off.
    Seem to remember being able to do this with the logical And operator
    something like if (2 & mybyte) { do something}
    Maybe I am dreaming of VB days.
    Is there a succint way of doing this?
    thanks
    Bob
    >

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: using a byte as a bit mask.

      On Oct 13, 2:10 pm, bob <startatbob_cl. ..@cutthis.adri ley.co.nz>
      wrote:
      Hi,
      Having trouble determining if bit n in a byte is on or off.
      Seem to remember being able to do this with the logical And operator
      something like if (2 & mybyte) { do something}
      Maybe I am dreaming of VB days.
      Is there a succint way of doing this?
      thanks
      Bob
      It's similar here , you use & or | , note that it's only one if you
      use two (&&) it's the logic operation instead of the bit operation.

      Comment

      • Peter Morris

        #4
        Re: using a byte as a bit mask.

        public bool IsBitSet(byte value, byte bitNumber)
        {
        return value & (1 << bitNumber);
        }

        Something like that anyway :-)



        --
        Pete
        ====


        Comment

        • bob

          #5
          Re: using a byte as a bit mask.

          Hi Anders,
          Spot on.
          Dunno why I couldn't see it.
          Thanks

          On Mon, 13 Oct 2008 20:49:19 +0200, "TAB" <anders.berglun d@email.com>
          wrote:
          >Hi Bob
          >
          >You are on the right track, try something like this
          if ((bits & 1) != 0) // bit is set if not zero
          >
          >where bits is an byte, uint or ulong.
          >Exhange 1 for the bit you would like to check, i.e. 1,2,4,8,16,32, ....
          >
          >"bob" <startatbob_cle gg@cutthis.adri ley.co.nzskrev i meddelandet
          >news:als5f4pfe o9d8je90kma4vfr ctsluep14v@4ax. com...
          >Hi,
          >Having trouble determining if bit n in a byte is on or off.
          >Seem to remember being able to do this with the logical And operator
          >something like if (2 & mybyte) { do something}
          >Maybe I am dreaming of VB days.
          >Is there a succint way of doing this?
          >thanks
          >Bob
          >>

          Comment

          • =?ISO-8859-1?Q?G=F6ran_Andersson?=

            #6
            Re: using a byte as a bit mask.

            Peter Morris wrote:
            public bool IsBitSet(byte value, byte bitNumber)
            {
            return value & (1 << bitNumber);
            }
            >
            Something like that anyway :-)
            >
            >
            >
            Close. ;)

            return (value & (1 << bitNumber)) != 0;

            Note, just to be overly clear: bitNumber is zero based.

            --
            Göran Andersson
            _____
            Göran Anderssons privata hemsida.

            Comment

            • Michael J. Ryan

              #7
              Re: using a byte as a bit mask.

              Effectively you just want to check a given value against x..

              int value = X; //user input, or saved value
              int check = 1<<3; //third to last bit 0x0...001000
              bool hasCheck = (value & check == check);


              I am pretty sure you can do something like the following...
              [Flags]
              public enum MyOptions : int {
              None= 0,
              First= 1,
              Second= 1<<1,
              Third= 1<<2
              }

              public bool HasOption(MyOpt ions value, MyOptions check) {
              return (value & check == check);
              }

              On 10/13/2008 11:10 AM, bob wrote:
              Hi,
              Having trouble determining if bit n in a byte is on or off.
              Seem to remember being able to do this with the logical And operator
              something like if (2 & mybyte) { do something}
              Maybe I am dreaming of VB days.
              Is there a succint way of doing this?
              thanks
              Bob
              >

              --
              Michael J. Ryan - tracker1(at)the roughnecks(dot) net - www.theroughnecks.net
              icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

              .... B5: The mind sees what it needs to see. The soul sees what the soul sees.

              Comment

              Working...