PDA

View Full Version : Compute Bit Mask



harmand
- 6th March 2008, 03:28
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.

skimask
- 6th March 2008, 15:23
I still don't use .net, so I don't know the NEW, IMPROVED syntax of it all...
(VB6 'till the end!!!)

x = bit position to be set to a 1
y = value in which a bit is to be set
y = y + ( 2 ^ x )
or
y = y OR ( 2 ^ x )

If you're bit is at position 11, then your value is 2 ^ 11, or 2048 (2^11)...
If the original value is 47 00000101111
and you add 2048 (2 ^11)
you get
2095 or 100000101111