|
-
Jul 14th, 2003, 08:23 AM
#1
Thread Starter
Hyperactive Member
power of 2 and binary numbers...
In VB, I'm going to have a set of flag constants that are powers of 2 (1,2,4,8,16,32,64, etc) so that my flag variable (a long integer) can have many combinations of those flags (using 'Or' to build the value and 'And' to read it)
Anyways, how can I take a number that's made up of 2 or more of these flags, and then find out which bits in the number are set to 1
Example..
VB Code:
const flag1 = 2 '0000 0010
const flag2 = 8 '0000 1000
Dim L As Long
Dim I As Integer
L = flag1 Or flag2 ' L is 0000 1010
If (L And flag1) = flag1 Then
I = ' code to find which bit in flag1 is turned on (should return 2)
Msgbox "The " & i & "nd bit is on"
End If
If (L And flag2) = flag2 Then
I = ' code to find which bit in flag2 is turned on (should return 4)
Msgbox "The " & i & "th bit is on"
End If
So basically, I need to know how to know which power of 2 a number is.
And I, for one, welcome our new insect overlords. I'd like to remind them as a trusted TV personality, I can be helpful in rounding up others to toil in their underground sugar caves.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|