Announcement

Collapse
No announcement yet.

Bit-Masking Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bit-Masking Help

    I swore to myself that I would never have to use Boolean Algebra but it seems that my computer science teacher was right. What I'm writing is the windows software componet to control a DAQ/2000 that will control 16 printed circut boards for testing. This project is not going to be written in PB/DLL 6.0 but I think I might write some functions in it like the math functions. What I need help with is bit-masking. It just seems that I can't grasp the idea good enough. right now I have a test circut board that is set up with 48 LEDs and and it has 6 ports on it and each port is 8 bits. Let me explain the problem I'm having:

    ok we have 8 leds and each leds is controlled by a boolean value true or false

    10001000 <--- led 1 & 5 is on
    lets turn led 8 on too so I would do this:
    10001000 <--- Last value entered
    or <--- or
    00000001 <--- new bit to turn on.
    10001001 <--- new bit to send to Daq/2000

    now this is the problem I having... lets say I want to turn OFF bits 1 and 8, how would I do that?

    The other question is I understand the differences between or/xor but when would I want to use one over the other? I am finding that sometimes if I use xor I get the same result?

    the main problem that I'm having so fair is that I have this arrangement:

    11100000 <---- bits 1 & 2 & 3 are on
    11111000 <---- bit 4 & 5 are added
    11100000 <---- how would I remove bits 4 & 5 without messing up anything with bits 1-3?

    Thanks


    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

  • #2
    00000001 <--- new bit to turn on.
    10001001 <--- new bit to send to Daq/2000

    now this is the problem I having... lets say I want to turn OFF bits 1 and 8, how would I do that?
    >>>

    You just use NOT to flip all the bits and AND it with the value you want to change.

    10000001 <--- Turn Off 1 and 8
    NOT( 10000001 ) = 01111110
    01111110 AND 10001001 = 00001000

    In other words:

    NOT(10000001) And 10001001 = 00001000

    XOR is useful in Assembly programming to clear a register as in:
    XOR EAX, EAX

    Register EAX now has a 0 in it.

    It is also useful for encoding data.

    A = Number
    Key = A password key
    B = Scrambled number

    B = A XOR Key

    Send the scrambled number to your friend, they would unscrammble like this:

    A = B XOR Key

    Tim

    Comment


    • #3
      On thing that you must remember, when working with most compilers and most chips the bits (8 bits) are numbered 0 to 7.

      2^0 = 00000001 <-- Bit 0
      2^1 = 00000010
      2^2 = 00000100
      2^3 = 00001000
      2^4 = 00010000
      2^5 = 00100000
      2^6 = 01000000
      2^7 = 10000000 <-- Bit 7

      You might have LED 1 hooked up to Bit 7 and that might be correct, but it might be confusing.

      Tim

      Comment


      • #4
        The answer sent by Tim is good.

        If you store your variables into boolean value. You can use a binary way to calculate witch bit will be on. It is slower but easier to read.

        To put bit 1 and bit 3 on:
        Bytes = (2^0) OR (2^3)

        or... Bytes = ((2^0) * (BoolBit1 * -1)) OR ((2^1) * (BoolBit2 * -1)) OR ((2^2) * (BoolBit3 * -1)) OR [...] ((2^7) * (BoolBit8 * -1))

        Regards

        [This message has been edited by Francis Beaulieu (edited June 27, 2000).]
        Francis Beaulieu
        Prog senior of 17 years.
        Microsoft Specialist

        Comment


        • #5
          BIT STATMENT
          Purpose
          Manipulate individual bits of an integer variable or bit array, for quickly and easily storing true/false (flag) settings.
          Syntax
          BIT {SET|RESET|TOGGLE} intVar, bitNumber

          Remarks
          Integer var must be one of the integer types: byte, word, (short) integer, double word, long integer, or quad integer. The allowable range for the parameter bitNumber is the same as that of a long integer (making it possible to have implicit bit arrays of more than 2 billion bits in size. Bits 0 to 15 are in the first word starting at intVar, bits 16-31 are in the next word, etc.). The first bit position is always zero.

          SET Sets the indicated bit to one
          RESET Sets the indicated bit to zero
          TOGGLE Toggles the indicated bit: one to zero; zero to one.

          BIT FUNCTION
          Purpose
          Return the value of a particular bit in an integer variable (or bit array).
          Syntax
          flag = BIT(intVar, bitNumber)
          Remarks
          The BIT function is used to determine the value of one particular bit in an integer variable or implied bit array. The parameter intVar must be a variable, not an expression. The BIT function returns either 0 or 1 to indicate the value of the specified bit for the position specified by bitNumber.
          bitNumber Is the bit in question. The allowable range for the parameter is the same as that of a long integer (making it possible to have implicit bit arrays of more than 2 billion bits in size. Bits 0 to 15 are in the first word starting at intVar, bits 16-31 are in the next word, etc.). The first bit position is always zero.

          [This message has been edited by Fred Oxenby (edited June 28, 2000).]
          Fred
          mailto:[email protected][email protected]</A>
          http://www.oxenby.se

          Comment


          • #6
            Thanks a lot guys!!! I knew I would remember it sooner or later.
            I'm so happy I nolonger have to make equates for the pre-calculated
            bit-masks. I now have functions to turn on an individual led and
            turning it off without messing up any of the other leds.

            Again thanks a lot.



            ------------------
            -Greg
            -Greg
            [email protected]
            MCP,MCSA,MCSE,MCSD

            Comment

            Working...
            X