I then want to mask out bits of the binary number.
For example I want to obtain only bits 3 to 6 say.
How can I do this?
I use Excel 97.
These are integers, right? VB doesn't have unsigned integers, so the values
should be in the range -32768 to 32767. Is this a problem? Assuming not,
Function MyFunction(aDouble As Double)
Const Mask = 8 + 16 + 32 + 64
MyFunction = Int(aDouble) And Mask
End Function
If you want to round any fractional part, use CInt instead of Int.
The following function will return a string of 0's and 1's. You can
then use string functions to select the bits you want. You should
insert error checking...
Function Binary(NumberIn As Long)
Dim NumberOut As String
Dim NumberOct As String
Dim StringIndex As Integer
NumberOct = Oct(NumberIn)
For StringIndex = 1 To Len(NumberOct)
Select Case Mid(NumberOct, StringIndex, 1)
Case "0"
NumberOut = NumberOut & "000"
Case "1"
NumberOut = NumberOut & "001"
Case "2"
NumberOut = NumberOut & "010"
Case "3"
NumberOut = NumberOut & "011"
Case "4"
NumberOut = NumberOut & "100"
Case "5"
NumberOut = NumberOut & "101"
Case "6"
NumberOut = NumberOut & "110"
Case "7"
NumberOut = NumberOut & "111"
End Select
Next
Binary = String(16 - Len(NumberOut), "0") & NumberOut
End Function
In article <37b0...@freenet.dnant>,
"michael doherty" <mdoh...@nireland.com> wrote:
> I need to convert decimal values into 16 bit binary numbers.
>
> I then want to mask out bits of the binary number.
> For example I want to obtain only bits 3 to 6 say.
>
> How can I do this?
> I use Excel 97.
>
>
--
Neil Swanson
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
You can by using:
a): the analisys toolpack formula DEC2BIN but that is limited to 10 places
(in XL 5, that is)
b): a user defined formula like this:
Option Explicit
Function MyDec2Bin(oRange As Object, iNumChar As Integer)
Dim iMaxChar As Integer
Dim lDecNum As Long
Dim lDecRest As Long
Dim sBinNum As String
Dim counter As Integer
lDecNum = oRange.Value
lDecRest = lDecNum
iMaxChar = Int(Log(lDecNum) / Log(2) + 0.5)
For counter = iMaxChar To 0 Step -1
If 2 ^ counter > lDecRest Then
sBinNum = sBinNum & "0"
Else
sBinNum = sBinNum & "1"
lDecRest = lDecRest - 2 ^ counter
End If
Next counter
If iNumChar - Len(sBinNum) > 0 Then
For counter = 1 To iNumChar - Len(sBinNum)
sBinNum = "0" & sBinNum
Next counter
Else
If iNumChar - Len(sBinNum) < 0 Then
sBinNum = Right(sBinNum, iNumChar) & " Truncated!!"
End If
End If
MyDec2Bin = sBinNum
End Function
Enter this function as follows into your sheet:
=MyDec2Bin(A1,16)
> I then want to mask out bits of the binary number.
> For example I want to obtain only bits 3 to 6 say.
You can use the MID function for that.
--
Kind regards,
Jan Karel Pieterse
jkpie...@compuserve.com
Are there any web sites which would have similar
basic building block functions?
For example hex or binary number manipulation,
Hex to 16 or 32 bit binary, Binary Coded Decimal to decimal etc.
Digital filters using Excel, Engineering calculations?
Michael
Long SelectedBits
SelectedBits = X AND (2^2 + 2^3 + 2^4 + 2^5)
will return 60 if bits 3...6 are on. This should be independent of the
particular numeric data type of X, judging from the Help for "and". (If
you're using the data type "Decimal", i have no clue as to what would
happen. That's one of the ambiguities referred to...)
If you want to build an array of boolean values,
dim bits() As Boolean
dim nbits As Integer
dim n As Integer
y = 60 ' This would prob'ly be a sub or function, with y passed in...
nbits = len(y) * 8 ' get bit size of y
redim bits(len(y) * 4 - 1) ' size array properly
for n = 0 To nbits - 1
bits(n) = y And 2 ^ n ' look at nth bit
next n
I didn't test this, but I think its close enough that if it is incorrect
you can fix it.
Incidentally, its probably best to think of these as bits zero through 15,
rather than 1 through 16. "Counting starts at zero". I sort of mixed
counting metaphors here, unfortunately; if counting starts a zero, bits
3...6 are 120, not 60.
Hope this is some help, and not too murky.
Herb Munson
michael doherty <mdoh...@nireland.com> wrote in article
<37b0...@freenet.dnant>...
> I need to convert decimal values into 16 bit binary numbers.
>
> I then want to mask out bits of the binary number.
> For example I want to obtain only bits 3 to 6 say.
>
>
Long SelectedBits
SelectedBits = X AND (2^2 + 2^3 + 2^4 + 2^5)
will return 60 if bits 3...6 are on. This should be independent of the
particular numeric data type of X, judging from the Help for "and". (If
the dta type is "Decimal", all bets are off!)
If you want to build an array of boolean values,
dim bits() As Boolean
dim nbits As Integer
dim n As Integer
y = 60 ' This would prob'ly be a sub or function, with y passed in...
nbits = len(y) * 8 ' get bit size of y
redim bits(len(y) * 4 - 1) ' size array properly
for n = 0 To nbits - 1
bits(n) = y And 2 ^ n ' look at nth bit
next n
I didn't test this, but I think its close enough that if it is incorrect
you can fix it.
Incidentally, its probably best to think of these as bits zero through 15,
rather than 1 through 16. "Counting starts at zero". I sort of mixed
counting metaphors here, unfortunately; if counting starts a zero, bits
3...6 are 120, not 60.
Convoluted enough?