In this article we will learn about some of the frequently asked Visual Basic programming questions in technical like “vba test if specific bits set in byte” Code Answer’s. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error handling in VBA is simple. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in Visual Basic. Below are some solution about “vba test if specific bits set in byte” Code Answer’s.
vba test if specific bits set in byte
xxxxxxxxxx
1
'Fast VBA function to test if specific bits are set
2
'within a Byte. If ALL specified bits are set then this function
3
'returns True. If ANY of the specified bits are not set then
4
'this function returns False.
5
6
'Bits are numbered from 0 to 7, so the first (least significant) bit
7
'is bit 0.
8
9
'Note: bits can be specified in any order.
10
11
12
Function ByteBitsAreSet(theByte As Byte, ParamArray bits()) As Boolean
13
Static i&, b() As Byte
14
If UBound(bits) = -1 Then Exit Function
15
If (Not Not b) = 0 Then
16
ReDim b(0 To 7)
17
For i = 0 To 7
18
b(i) = 2 ^ i
19
Next
20
End If
21
For i = 0 To UBound(bits)
22
If bits(i) < 0 Then Exit Function
23
If bits(i) > 7 Then Exit Function
24
If (theByte And b(Int(bits(i)))) = 0 Then Exit Function
25
Next
26
ByteBitsAreSet = True
27
End Function
28
29
30
'--------------------------------------------------------------------------
31
MsgBox ByteBitsAreSet(255, 7) '<--displays: True 255 = 11111111
32
MsgBox ByteBitsAreSet(230, 0) '<--displays: False 230 = 11100110
33
MsgBox ByteBitsAreSet(85, 0, 2, 6, 4) '<--displays: True 85 = 01010101
34
MsgBox ByteBitsAreSet(85, 0, 2, 5, 4) '<--displays: False 85 = 01010101
35
'
36
'
37
'
38
39
40
excel vba set bit in byte
xxxxxxxxxx
1
'Extremely fast VBA function to test if a specified bit is set
2
'within a Byte.
3
4
'Bits are numbered from 0 to 7, so the first (least significant) bit
5
'is bit 0.
6
7
Function ByteBitIsSet(theByte As Byte, bit As Byte) As Boolean
8
Static i&, b() As Byte
9
If (Not Not b) = 0 Then
10
ReDim b(0 To 7)
11
For i = 0 To 7
12
b(i) = 2 ^ i
13
Next
14
End If
15
If bit < 8 Then ByteBitIsSet = theByte And b(bit)
16
End Function
17
18
'------------------------------------------------------------------
19
MsgBox ByteBitIsSet(255, 7) '<--displays: True
20
MsgBox ByteBitIsSet(230, 0) '<--displays: False
21
22
23
24