Compute Bit Mask

Discussion in 'Programmer's Corner' started by harmand, Mar 5, 2008.

  1. harmand

    Thread Starter New Member

    Mar 5, 2008
    I have a app in that I am doing maintenance on and as always not enough documentation.

    The app is for a Human Machine Interface that uses a visioning system to look for pins when a curcuit board is placed in a ficture. When the value comes out of the PLC the leading 3 bits must be set to 0 and the result is compared to a value in a DB table.

    I used a BitArray and a StreamWriter to come up with a string rep of the number 47;


    I know that the MSB is at pos 11.

    I want to set the bit mask in the form 'And 2 ^ x'.

    How do I compute x?

    Or am I going about this the wrong way.
  2. rwmoekoe

    Active Member

    Mar 1, 2007
    and-ing with 2^x will always mask one bit. do you really need only one bit?
    from what you wrote, i guess you wanted to set bits 15,14, and 13 to 0.
  3. SgtWookie


    Jul 17, 2007
    Just AND it with a mask that has the top three bits set to zero, and the remaining bits set to ones.
    The result will be the input less the top three bits.

    If the most significant bit you wish to examine is at position 11, then that number in decimal is represented by 1024, or in binary by "10000000000". Multiply by two, subtract one, and you wind up with 2047, which will give you binary "11111111111", which is the mask you need.

    Or have you not explained the situation correctly?