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. bytes, bits, trues and falses..

bytes, bits, trues and falses..

Scheduled Pinned Locked Moved C#
helptutorialquestion
9 Posts 5 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
    dazinith
    wrote on last edited by
    #1

    hey there.. i have a problem working with an older data type that i dont know how to handle.. i have a byte (8 bits) which tells me if 8 different options are turned on or off.. so when i read in a value of 36 i know that bit 6 and bit 3 are on (32 + 4 = 36).. im trying to figure out an easy way to get if a bit is on or off without having to write some sort of code to parse the byte value.. so if i have:

    byte bByte = GetByte(); // bByte=36 in this example

    i want to be able to write a function (or use one that exists) to call something like:

    if (IsOption1On(bByte))
    ...

    can someone help me understand how this works? thanks in advance! still a newb.. cut me some slack :P -dz

    T J L W 4 Replies Last reply
    0
    • D dazinith

      hey there.. i have a problem working with an older data type that i dont know how to handle.. i have a byte (8 bits) which tells me if 8 different options are turned on or off.. so when i read in a value of 36 i know that bit 6 and bit 3 are on (32 + 4 = 36).. im trying to figure out an easy way to get if a bit is on or off without having to write some sort of code to parse the byte value.. so if i have:

      byte bByte = GetByte(); // bByte=36 in this example

      i want to be able to write a function (or use one that exists) to call something like:

      if (IsOption1On(bByte))
      ...

      can someone help me understand how this works? thanks in advance! still a newb.. cut me some slack :P -dz

      T Offline
      T Offline
      Tomas Petricek
      wrote on last edited by
      #2

      You can use & operator to get what 'flags' options are set.

      if ((bByte&0x01)!=0) {} // First bit is set
      if ((bByte&0x02)!=0) {} // Second bit is set
      if ((bByte&0x04)!=0) {} // Third bit is set
      //...

      You can use this function to get whether is option set (nOpt is value from 0 to 7)

      bool IsOptionOn(byte bByte,byte nOpt)
      {
      return ((bByte&(1<
      Hope this helps :rolleyes:

      i'm only pointer to myself

      D 1 Reply Last reply
      0
      • D dazinith

        hey there.. i have a problem working with an older data type that i dont know how to handle.. i have a byte (8 bits) which tells me if 8 different options are turned on or off.. so when i read in a value of 36 i know that bit 6 and bit 3 are on (32 + 4 = 36).. im trying to figure out an easy way to get if a bit is on or off without having to write some sort of code to parse the byte value.. so if i have:

        byte bByte = GetByte(); // bByte=36 in this example

        i want to be able to write a function (or use one that exists) to call something like:

        if (IsOption1On(bByte))
        ...

        can someone help me understand how this works? thanks in advance! still a newb.. cut me some slack :P -dz

        J Offline
        J Offline
        Jim Stewart
        wrote on last edited by
        #3

        Here's an article[^] (which I wrote :-O) which does just this thing. I wrapped the int in a struct which made it much easier to get at the bits.

        α.γεεκ

        Fortune passes everywhere.
        Duke Leto Atreides

        1 Reply Last reply
        0
        • D dazinith

          hey there.. i have a problem working with an older data type that i dont know how to handle.. i have a byte (8 bits) which tells me if 8 different options are turned on or off.. so when i read in a value of 36 i know that bit 6 and bit 3 are on (32 + 4 = 36).. im trying to figure out an easy way to get if a bit is on or off without having to write some sort of code to parse the byte value.. so if i have:

          byte bByte = GetByte(); // bByte=36 in this example

          i want to be able to write a function (or use one that exists) to call something like:

          if (IsOption1On(bByte))
          ...

          can someone help me understand how this works? thanks in advance! still a newb.. cut me some slack :P -dz

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          I suggest a Flagged Enum too. Else just use bitwise operators if you are feeling comfortable with them. Hey leppie! Your "proof" seems brilliant and absurd at the same time. - Vikram Punathambekar 28 Apr '03

          1 Reply Last reply
          0
          • T Tomas Petricek

            You can use & operator to get what 'flags' options are set.

            if ((bByte&0x01)!=0) {} // First bit is set
            if ((bByte&0x02)!=0) {} // Second bit is set
            if ((bByte&0x04)!=0) {} // Third bit is set
            //...

            You can use this function to get whether is option set (nOpt is value from 0 to 7)

            bool IsOptionOn(byte bByte,byte nOpt)
            {
            return ((bByte&(1<
            Hope this helps :rolleyes:

            i'm only pointer to myself

            D Offline
            D Offline
            dazinith
            wrote on last edited by
            #5

            this helped a bunch.. but im doing something wrong.. i dunno what it is.. it seems to work fine until i get into higher numbers.. here is whats happening:

            byte myByte = 45;
            bool bOn = (myByte&0x64)!=0;

            b0n should be false since 64 is bigger than 45, so bit 7 should be off.. but its returning true.. what am i doing wrong? still a newb.. cut me some slack :P -dz

            T 1 Reply Last reply
            0
            • D dazinith

              this helped a bunch.. but im doing something wrong.. i dunno what it is.. it seems to work fine until i get into higher numbers.. here is whats happening:

              byte myByte = 45;
              bool bOn = (myByte&0x64)!=0;

              b0n should be false since 64 is bigger than 45, so bit 7 should be off.. but its returning true.. what am i doing wrong? still a newb.. cut me some slack :P -dz

              T Offline
              T Offline
              Tomas Petricek
              wrote on last edited by
              #6

              Problem is that number 0x64 (this is 16*6+4*1 = 100) is in hexadecimal and 64 is decimal. To learn more about this I recomend you this article[^]. i'm only pointer to myself

              D 1 Reply Last reply
              0
              • T Tomas Petricek

                Problem is that number 0x64 (this is 16*6+4*1 = 100) is in hexadecimal and 64 is decimal. To learn more about this I recomend you this article[^]. i'm only pointer to myself

                D Offline
                D Offline
                dazinith
                wrote on last edited by
                #7

                stupid hexidecimal.. :) i was wondering what was happening.. is there any reason why you chose to use hex in your example you gave me? the following works perfect:

                bool bOn1 = (bByte&01);
                bool bOn2 = (bByte&02);
                bool bOn3 = (bByte&04);
                bool bOn4 = (bByte&08);
                bool bOn5 = (bByte&16);
                bool bOn6 = (bByte&32);
                bool bOn7 = (bByte&64);
                bool bOn8 = (bByte&128);

                thanks for all of your help!! :beer: still a newb.. cut me some slack :P -dz

                T 1 Reply Last reply
                0
                • D dazinith

                  stupid hexidecimal.. :) i was wondering what was happening.. is there any reason why you chose to use hex in your example you gave me? the following works perfect:

                  bool bOn1 = (bByte&01);
                  bool bOn2 = (bByte&02);
                  bool bOn3 = (bByte&04);
                  bool bOn4 = (bByte&08);
                  bool bOn5 = (bByte&16);
                  bool bOn6 = (bByte&32);
                  bool bOn7 = (bByte&64);
                  bool bOn8 = (bByte&128);

                  thanks for all of your help!! :beer: still a newb.. cut me some slack :P -dz

                  T Offline
                  T Offline
                  Tomas Petricek
                  wrote on last edited by
                  #8

                  Hexadecimal rox. I think in hexadecimal :omg: I'm sorry if I confused you. i'm only pointer to myself

                  1 Reply Last reply
                  0
                  • D dazinith

                    hey there.. i have a problem working with an older data type that i dont know how to handle.. i have a byte (8 bits) which tells me if 8 different options are turned on or off.. so when i read in a value of 36 i know that bit 6 and bit 3 are on (32 + 4 = 36).. im trying to figure out an easy way to get if a bit is on or off without having to write some sort of code to parse the byte value.. so if i have:

                    byte bByte = GetByte(); // bByte=36 in this example

                    i want to be able to write a function (or use one that exists) to call something like:

                    if (IsOption1On(bByte))
                    ...

                    can someone help me understand how this works? thanks in advance! still a newb.. cut me some slack :P -dz

                    W Offline
                    W Offline
                    Wesner Moise
                    wrote on last edited by
                    #9

                    You can use System.Collections.BitArray. Also, in Systems.Collections.Specialized, there is a 32-bit vector, which is essentially an integer wrapped in a struct.

                    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