Excel code for breaking values into bit fields

sparky_289

Member
Join Date
Jun 2008
Location
Hilmar
Posts
4
I'm looking for a fast easy Excel code to move whole integer values into separate bit fields. Does anyone know of a relatively easy code?🔨
 
This formula should work:
=MID(TEXT(DEC2BIN(123,8),"00000000"),X,1)

where 123 is the number (could be reference to another cell) and in this case 8 is the number of bits you're working with. X is the position of the binary digit you want to extract (counting from the left/MSB, not the right). In order for the DEC2BIN function to work, you need to have the Analysis Toolpak enabled.
 
The attached spreadsheet has a little bit of legerdemain in it in the form of a custom function in VBA.

The function is:

Public Function BITAND(x As Long, y As Long)
BITAND = x And y
End Function

and it is located in Module 1 on the VBA sheets. You will need to have Excel security set to allow macros - I recommend medium so that you get a prompt before opening.

With the custom function any integer can be bitwise ANDed with any other. By ANDing it with 2^n, where n is a bit position, we can determine if that bit is TRUE or FALSE, and return the value 1 or 0 with the Excel IF function.

Just another way of doing it without using TEXT functions. It handles unsigned integers.

Also there is a useful tool called LL-SOLVER that is a free download, you can get it from the dowload section at MRPLC, which is useful for converting and viewing 16 bit integers in hex, decimal, and binary - much simpler than EXCEL for the occasional quick binary conversion.

http://forums.mrplc.com/index.php?download=606
 
Last edited:
A different approach - no vba

abcdefghijklmnopq1bit151413121110987654321023276816384819240962048102451225612864321684213111111111111111111111111878787872323773104=if(b6=1,a3-b2,a3)56=c3,1,0)">0=d3,1,0)">0=e3,1,0)">0=f3,1,0)">0=g3,1,0)">0=h3,1,0)">1=i3,1,0)">0=j3,1,0)">0=k3,1,0)">0=l3,1,0)">1=m3,1,0)">0=n3,1,0)">1=o3,1,0)">0=p3,1,0)">1=q3,1,0)">1=r3,1,0)">17=b2,1,0)">=if(a3>=b2,1,0)


formula listed below the start of cell/row so visible in this copy. Formulas copied across b3 thru q3 and b6 thru q3. Number to parse goes in a3, row 6 gives bits. Formulas could probably be combined and row 2 numbers hardcoded into formula if you need to get to one line.

Mike
 
And this is my code which I use for a long time.


Code:
Function KM(ByVal H As Long, L As Long) As String
' this function returns binary sequence as string
' H - input integer value
' L - length of returned string 
Dim k As Long
Dim E As Long
KM = "B"
1:
    E = H Mod 2
        If E = 0 Then
            KM = "0" + KM
        Else
            KM = "1" + KM
        End If
        
    H = Int(CDbl(H) / 2)
    If H >= 1 Then GoTo 1
    
k = 1 + L - Len(KM)
While k <> 0
KM = "0" + KM
k = k - 1
Wend
    
End Function

I was late a little, but I will be glad if it be useful for someone.
 
Some more skinned cats

I normally use the ^ (power of) operator when decoding binary to/from decimal. There are three functions on the sheet, Decimal to binary,Binary to decimal and a function to check a bit status in a word.
 

Similar Topics

Hello friends; I found a code for VBA use in excel to read the Tags values from RSlogix 5000 through RSlink. The code is here: Dim OPCServer1 As...
Replies
9
Views
3,725
Hi, I use Excel to write some routines and was wondering if it is possible to add rung comments when doing so? Excel function: ="MOV "&[@[Range...
Replies
2
Views
2,542
I need to create and write an Excel Application using VBA code using Factory Talk View 7.0. Thanks a lot.
Replies
0
Views
2,595
I am using rslinx to populate row 2 of a Excel sheet. I have a macro that will copy the real time data from row 2 and insert a new row below and...
Replies
16
Views
5,631
I am trying to get a digital output based on a barcode read. The scan talks to an IBM AS400 where it is verified as good or bad, if it is a bad...
Replies
5
Views
5,221
Back
Top Bottom