Compute Bit Mask

Thread Starter

harmand

Joined Mar 5, 2008
1
I have a app in vb.net 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;

'0000000000101111'

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.
 

rwmoekoe

Joined Mar 1, 2007
172
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.
 

SgtWookie

Joined Jul 17, 2007
22,230
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?
 
Top