473,379 Members | 1,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

find number of digits

Hi,

does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:

- digits = (int) log2(number), where log 2 means log for base 2
- shifting until 0: for(j=0; number>>=1; j++); digits = j;
- binary search shifting

Any better ideas?

Thanks and regards,

Christof
Jul 22 '05 #1
13 7069


CHRISTOF WARLICH wrote:
Hi,

does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:

- digits = (int) log2(number), where log 2 means log for base 2
- shifting until 0: for(j=0; number>>=1; j++); digits = j;
- binary search shifting

Any better ideas?

Thanks and regards,

Christof


if your binary number is small enough, a table lookup may suffice...

David
Jul 22 '05 #2
* CHRISTOF WARLICH:

does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:

- digits = (int) log2(number), where log 2 means log for base 2
Very inefficient unless you have a fast integer log function
(which in that case is exactly what you're looking for).

- shifting until 0: for(j=0; number>>=1; j++); digits = j;
Use ++j, and stylewise add an empty loop body, {}.

- binary search shifting

Any better ideas?


You could always use a table, dividing the number up in chunks
in a binary search until suitable for the table size.

But that is inefficient in terms of memory usage.

You don't specify what you mean by "efficient".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3
CHRISTOF WARLICH wrote:
Hi,

does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:

- digits = (int) log2(number), where log 2 means log for base 2
- shifting until 0: for(j=0; number>>=1; j++); digits = j;
- binary search shifting

Any better ideas?

Thanks and regards,

Christof


There are a number of implementations of "ffs" or "find first set" and
on most linux boxen it's available as int ffs(int) in string.h.

However, this is a cute trick. Converting an integer to floating point
will give you the number you're looking for in the exponent.
std::frexp() will extract the exponent ...
#include <cmath>

int ffs_fp( int value )
{
if ( value == 0 )
{
return -1; // no answer
}

int exponent;
double correct_mantissa = std::frexp( value, &exponent );

return exponent -1;
}

#include <iostream>

void testit( int value )
{

std::cout << "ffs_fp( " << value << " ) = " << ffs_fp( value ) << "\n";
}

int main()
{

testit( 1 << 2 );
testit( 1 << 0 );
testit( 1 << 17 );
testit( -1 + ( 1 << 17 ) );

}
Jul 22 '05 #4
Hi,

Binary search initially and then table lookup. For example if you can spare
64kb for a lookup table and your numbers are up to 32bit (untested code):

char lookup[0x10000];

int digits(unsigned int n)
{
if (n < 0x10000)
return lookup[n];
else
return 16 + lookup[n >> 16];
}

Regards,
Marcin
Uzytkownik "CHRISTOF WARLICH" <cw******@lucent.com> napisal w wiadomosci
news:41***************@lucent.com...
Hi,

does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:

- digits = (int) log2(number), where log 2 means log for base 2
- shifting until 0: for(j=0; number>>=1; j++); digits = j;
- binary search shifting

Any better ideas?

Thanks and regards,

Christof



Jul 22 '05 #5
CHRISTOF WARLICH <cw******@lucent.com> wrote in message news:<41***************@lucent.com>...
Hi,

does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:

- digits = (int) log2(number), where log 2 means log for base 2
- shifting until 0: for(j=0; number>>=1; j++); digits = j;
- binary search shifting

Any better ideas?


Assembly an option? IIRC there are some CPU archs which have exactly
this instruction, and it is very hard to out-optimize microcode.

If not, I'd probably break the number up in 8-bits chunks and use
a 256 entry lookup. For 32-bits numbers, that's 2 or 3 comparisons
(I don't know which approach is faster, 3 checks against 0 of 2
checks against constants)

HTH,
Michiel Salters
Jul 22 '05 #6
BRG
CHRISTOF WARLICH wrote:
Hi,

does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:

- digits = (int) log2(number), where log 2 means log for base 2
- shifting until 0: for(j=0; number>>=1; j++); digits = j;
- binary search shifting

Any better ideas?


You might like to look at this site that gives some techniques for this
sort of problem:

http://www.hackersdelight.org/HDcode.htm

Brian Gladman
Jul 22 '05 #7
> > does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:

- digits = (int) log2(number), where log 2 means log for base 2


Very inefficient unless you have a fast integer log function
(which in that case is exactly what you're looking for).


If number does not fit into an int, then it might be a good idea.
- shifting until 0: for(j=0; number>>=1; j++); digits = j;


Use ++j, and stylewise add an empty loop body, {}.


Why ++j ? In all other cases, the recipient of the value is left of the
operator: f.ex j+=2, object.inc(), so why not j++ that would be more conform
to the C++ style.

Niels Dybdahl
Jul 22 '05 #8
"David Lindauer" <ca*****@bluegrass.net> wrote in message
if your binary number is small enough, a table lookup may suffice...


If the binary number is big, then you can use the table lookup many times.
For example, if your table handles integers whose sizeof equals 1 (eg.
char), then to handle an integer whose sizeof is 4 (eg. int on many
platforms), then use the table up to 4 times.
Jul 22 '05 #9

"Niels Dybdahl" <nd*@fjern.detteesko-graphics.com> skrev i en meddelelse
news:41*********************@news.dk.uu.net...
> does anyone know of an efficient way to find the number of
> digits (i.e. the most significant position that is 1) of a
> binary number? What I found so far is:
>
> - digits = (int) log2(number), where log 2 means log for base 2


Very inefficient unless you have a fast integer log function
(which in that case is exactly what you're looking for).


If number does not fit into an int, then it might be a good idea.
> - shifting until 0: for(j=0; number>>=1; j++); digits = j;


Use ++j, and stylewise add an empty loop body, {}.


Why ++j ? In all other cases, the recipient of the value is left of the
operator: f.ex j+=2, object.inc(), so why not j++ that would be more
conform
to the C++ style.

Niels Dybdahl

Always prefer ++x to x++ - this is an efficiency issue.

/Peter
Jul 22 '05 #10
Thanks to all!

Regards,

Christof

CHRISTOF WARLICH wrote:

Hi,

does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:

- digits = (int) log2(number), where log 2 means log for base 2
- shifting until 0: for(j=0; number>>=1; j++); digits = j;
- binary search shifting

Any better ideas?

Thanks and regards,

Christof

Jul 22 '05 #11
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message news:ZrDpd.70127
Always prefer ++x to x++ - this is an efficiency issue.


This doesn't matter for fundamental types, which is the subject of this
thread.

Jul 22 '05 #12
* Siemel Naran:
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message news:ZrDpd.70127
Always prefer ++x to x++ - this is an efficiency issue.


This doesn't matter for fundamental types, which is the subject of this
thread.


<rant>
The reason I advocate the ++x style is that I feel that in programming
one should strive to be _exact_ and specify only what one wants
accomplished -- not instantiate a web-browser component in order to
display some simple text in the user interface (say), because that's
easiest using the IDE's drag'n'drop interface, which is the end result
of accustomizing oneself to being imprecise and just adding things until
the hodgepodge seems to work.
</rant>

;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #13

"Siemel Naran" <Si*********@REMOVE.att.net> skrev i en meddelelse
news:Ey*******************@bgtnsc05-news.ops.worldnet.att.net...
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message news:ZrDpd.70127
Always prefer ++x to x++ - this is an efficiency issue.


This doesn't matter for fundamental types, which is the subject of this
thread.

So what? It is simply a matter of style - use the form that is efficient all
the time.

/Peter
Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: jose luis fernandez diaz | last post by:
Hi, My OS is: cronos:jdiaz:tmp>uname -a HP-UX cronos B.11.11 U 9000/800 820960681 unlimited-user license I compile in 64-bits mode the program below:
8
by: jquest | last post by:
Hi Again; I have had help from this group before and want to thank everyone, especially PCDatasheet. My database includes a field called HomePhone, it uses the (xxx)xxx-xxx format to include...
6
by: Jovo Mirkovic | last post by:
Hi, I have to make a program which will generate 100,000 different ID numbers (for example 2345-9341-0903-3432-3432 ...) It must be really different, I meen it can not be a similar (like...
27
by: Luke Wu | last post by:
Is there a C function that returns the number of digits in an input int/long? example: numdigits(123) returns 3 numdigits(1232132) returns 7
8
by: Steven | last post by:
Hi, I am trying to find out how many digits there are in a given number. The macro listed below works fine when applied to an INT, however when doing Doubles with numbers > then a billion ?? It...
5
by: zaie | last post by:
The files I'm referring to can be found here: http://www.moxybox.com/programming After going through this in debugger numerous times, I can't figure out why this code continues to return false:...
5
debasisdas
by: debasisdas | last post by:
A ten-digit number contains every digit from 0 to 9. The digits are arranged so that the number formed by the first two digits, reading from left to right, is divisible by 2, the number formed by...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
by: zephyrus360 | last post by:
This is about a technique to find the mod of a very large integer with a normal small integer. I recently encountered this problem when I needed to compute the modulus of a very large number with...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.