|
Something like this?
System.Math.Floor ( System.Math.Log ( val , 2 ) )
|
|
|
|
|
I just timed this method vs. the algorithm I initially posted, as well as a new faster alg. using a binary search scheme (see my response to the post before yours), and the algorithm that you proposed is OVER 4.5 times slower than my fast method (~3 times slower than my slow method for big integers)! That is why I am attempting to devise my own integer algorithm. The double arithmetic involved in the computation is EXTREMELY expensive and unneccessary, and the slowdown is unacceptable for my application.
Jeff
|
|
|
|
|
Hi Jeff,
Skippums wrote: Is there a better way to do this, like perhaps a binary search algorithm?
yes there is.
Seems you want the bit number of the most significant bit set.
Some CPUs have an instruction for this ("find first one" or "count leading zeroes").
Using it on x86 would mean P/Invoke to another dll (C/C++ with asm)
Without asm, the optimal approach depends on the application. So here are a few questions:
What is the range of integers you expect? (smaller range may yield faster code)
And what is the distribution? (to optimally order the operations)
final Q: is there any calculation involved in getting the input value (such as multiplying,
dividing, shifting)?
BTW: the Petes and Pauls of CP will tell you "wrong forum; try Math/Algo".
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
The input value is the size of a file, in bytes. It is provided by the client to my application (we assume they are good clients and provide the info correctly). Therefore, I expect the range to be anywhere between 1,000 (1 kB) and 1,000,000,000 (1 GB). I think the fastest I can get from C# code is as noted in my response to the first person who answered my question. I would be interested to try actually writing the asm code, and importing it into my C# project to get better performance. That sounds like a fun exercise. I will post the results of it as soon as I try it!
Jeff
|
|
|
|
|
Let me know if you get assembly in C#.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Luc Pattyn wrote: Seems you want the bit number of the most significant bit set.
That may be what he wants, but that's not what he asked for.
Hmmm, maybe it could be done recursively...
|
|
|
|
|
Yeah, those software guys often use a strange language to ask a simple question.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Sorry... I figured that nobody who started programing in C# would know what a "bit" is
Jeff
|
|
|
|
|
No, that's the VB folk. But they don't know log or base either.
|
|
|
|
|
Unless they tried to use the BitArray class...
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
what are all the features of .net
|
|
|
|
|
Moron Alert....
Regards,
Rob Philpott.
|
|
|
|
|
Are you being serious? You think someone is going to list ALL the features of .net in a forum post?
|
|
|
|
|
That's really too broad a question to answer in this forum. Do some research[^].
|
|
|
|
|
Judah Himango wrote: Do some research[^].
Reading is for sissies! Real programmers never stop typing code, never stop to read, never stop to think.
|
|
|
|
|
|
Well it begins with a "." (dot), followed by an "n", then an "e", and finally a "t".
|
|
|
|
|
Is that dots are primarily a very small fish. Thus the wholes for a dot net need to be really small. This causes more resistance when dragged through the water, however. So when considering the purchase of a dot net look at the rigidity factor of the pole, the UV resistance of the netting, the size of the holes in the netting, as well as the inverse viscosity measure.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
1. georgemathew2@hotmail.com View details
Status Bronze. Member No. 4493066
View Member's Blog.
Awards
Messages Posted
6 - Poster
Articles Submitted
0 - Browser
Biography I am a biggner and I am focused on C# and Asp.Net
Birthday Sunday 21st February, 1982
Location India
Occupation Software development
Interests COM, C#, ASP.NET, VB.NET, Javascript
Member since Friday 7th September, 2007
(2 months)
"If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer
I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")
|
|
|
|
|
Have you ever bothered to visit
1) http://msdn.microsoft.com/net/[^]?
or
2) At least know what is the latest version of .NET currently that is available?
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson
|
|
|
|
|
what a namespace and can we use the namesoaces we create in other projects
|
|
|
|
|
Don't be helpless, use a search engine to find out what are namespaces. I quick google search revealed this good one[^].
|
|
|
|
|
|
A namespace is a space for names.
|
|
|
|
|
Really a humorous definition.
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson
|
|
|
|