Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / June 2004



Tip: Looking for answers? Try searching our database.

fast bit check

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin! - 18 May 2004 12:21 GMT
occasionally i would like to check if in a Long integer a bit is
checked or not

i use now:
B = CBool(x And 2 ^ j)  'where x and j are integers.

i suspect that calculating 2^j is rather time consuming and would like
to test other/faster methods. all suggestions/remarks are appreciated.

thanks and regards,

Martin
Mike Williams - 18 May 2004 14:01 GMT
> . . . . . I suspect that calculating 2^j is rather time consuming
> and would like to test other/faster methods . . . . .

It depends on what you mean by "time consuming". On my machine (2 ^ j) takes
about a quarter of a microsecond, so unless I am using it a very large
number of times in some very time critical code then I wouldn't worry about
it. If you want a faster method though you could do a lot worse than a
simple look up table (which on testing is about a hundred times faster):

Option Explicit
Private Bit(0 To 15) As Long

Private Sub Form_Load()
 Dim n As Long
 For n = 0 To 15
   Bit(n) = 2 ^ n
 Next n
End Sub

Then when you want to check a specific bit you can use:

B = CBool(x And Bit(j))

You could even eliminate the CBool and simply test for a non zero result,
but in practice that doesn't make much difference.

Mike
Michael B. Johnson - 18 May 2004 15:32 GMT
>> . . . . . I suspect that calculating 2^j is rather time consuming
>> and would like to test other/faster methods . . . . .
[quoted text clipped - 4 lines]
>it. If you want a faster method though you could do a lot worse than a
>simple look up table (which on testing is about a hundred times faster):

Mike is right. And didn't Donald Knuth say premature optimization is
the root of all evil?

Having said that, (and noting that programmer time is more expensive
than computer time), you could take advantage of the fact that
2^n=n*n, because multiplication can be faster than exponentials. But
the best thing would be to precalculate it using an array/table - and
trade speed for memory space.

Have you done the *timing*? Premature optimization is "EVIL".

>Option Explicit
>Private Bit(0 To 15) As Long
[quoted text clipped - 14 lines]
>
>Mike
_______________________
Michael B. Johnson
Jon - 18 May 2004 16:15 GMT
snip

| Having said that, (and noting that programmer time is more expensive
| than computer time), you could take advantage of the fact that
| 2^n=n*n, because multiplication can be faster than exponentials. But
| the best thing would be to precalculate it using an array/table - and
| trade speed for memory space.

2^n is not n*n, that would be n^2

2^n = 2 * 2 * 2 (with n twos)

:)

Jon
Michael B. Johnson - 30 Jun 2004 17:00 GMT
>| Having said that, (and noting that programmer time is more expensive
>| than computer time), you could take advantage of the fact that
[quoted text clipped - 9 lines]
>
>Jon

Quite right. Thanks, Jon for correcting my oversight.
_______________________
Michael B. Johnson
_______________________
Michael B. Johnson
preben nielsen - 18 May 2004 16:07 GMT
> occasionally i would like to check if in a Long integer a bit is
> checked or not
[quoted text clipped - 3 lines]
>
> i suspect that calculating 2^j is rather time consuming and would like

Not necessarily ! 2 ^ j can be done as bitshifting by the
processor and is probably extremely fast.

Try a test. See it you can get it to do it a 100.000 times a
second or more :-) It's probably a hundred times faster than
CBool() anyway...

Signature

/\ preben nielsen
\/\ prel@post.tele.dk

EBS - 18 May 2004 16:18 GMT
Anybody for addition only?

Private Bit(0 To 15) As Long

Private Sub cmdBits_Click()
  Dim b As Long
  Dim BitValue As Long
 
  BitValue = 1
  For b = 0 To 15
     Bit(b) = BitValue
     Debug.Print "Bit(" & b & ") = " & BitValue
     BitValue = BitValue + BitValue
  Next
End Sub

Eric

>> occasionally i would like to check if in a Long integer a bit is
>> checked or not
[quoted text clipped - 11 lines]
>second or more :-) It's probably a hundred times faster than
>CBool() anyway...
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.