Bit Manipulation using VBA

spurs

Active Member
Joined
Oct 18, 2006
Messages
479
Office Version
  1. 2016
  2. 2013
  3. 2010
  4. 2007
  5. 2003 or older
Platform
  1. Windows
I have a variable called ReadIO that reads the value of an Input output module.

The decimal value for ReadIO ranges from 0 to 255 representing and 8 digit binary number

i.e. 1 1 1 1 1 1 0 1 is decimal 253

Is there any known vba code that I can use to take the value from the variable Read IO

then determine what the decimal value should be if I want to turn on or off an individual bit?

For example

suppose the value for ReadIO is 63 corresponding to binary 0 0 1 1 1 1 1 1

I want to create code to take that value (whatever it is 63 is just an example) and choose to change the value of bit 4 to a low state i.e. 0

Therefore the binary code will change to 0 0 1 1 0 1 1 1 or decimal 55

I imagine this is routine code that must exist somewhere in vb
 
Are you back in formulas? I thought you wanted to do this in VBA.
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Is there a way to isolate bit 4 to figure out if its a 0 or a 1 ?
 
Upvote 0
I am using vba
the vba code you posted did not work but here is what I ended up with and it works


bit = 7 ' output 7 on IO Module is the mandrel
I = Mid(Application.WorksheetFunction.Dec2Bin(ReadIO, 8), 8 - bit + 1, 1) ' returns the value of the desired bit in ReadIO
If I = 1 Then
X = ReadIO - Choose(bit, 1, 2, 4, 8, 16, 32, 64, 128)
Else
' Do nothing
End If
 
Upvote 0
In VBA, and assuming bits are numbered 0 to 7 in a byte (and they are),

Code:
if i and 2^4 then
  'it's a 1
else
  ' it's not

All logical operations in VBA are done bit-wise; you don't need all of those worksheet functions.
 
Upvote 0
Thank you all for your help

From my code above you can see I got it working. When I want to set the bit to 0 I simply change the 1 to a 0 in the if statement and add the equation instead of subtract it.

the only thing that doesn't look elegant to me is the use of application.worksheetfunction for the dec2bin conversion
is there a built in vba syntax that doesn't use the worksheet function?
 
Upvote 0
Yes, Shg is trying to show it to you.
I'd recommend forgetting all the stuff I tried to use.
Shg's way is much better.
 
Upvote 0
Ok got it

revised my code as follows

Output = 7 ' output 7 on IO Module

' test to see if the desired output bit is a 1 or else its a 0
If ReadIO And 2 ^ (Output - 1) Then ' the output bit is therefore a 1
X = ReadIO - Choose(Output, 1, 2, 4, 8, 16, 32, 64, 128)
Else
' Do nothing since the output bit is already a 0 and the mandrel is already expanded
End If
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,794
Members
449,468
Latest member
AGreen17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top