473,544 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extracting Digits from a Number

Hello,

I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature.

The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number).

Any ideas?

Nov 15 '05 #1
13 15950
In article <11************ **********@g47g 2000cwa.googleg roups.com>,
Kosio <aa********@gma il.com> wrote:
I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature. The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number). Any ideas?


branch tables, look-up tables, repeated subtraction, "manual"
division by 10 in binary (shifts, subtractions, multiplications )
An old posting
http://groups.google.ca/group/comp.o...be552f2742b678
references
"Optimizing Integer Division by a Constant Divisor" by Robert Grappel
which was apparently in Dr. Dobbs... probably 15 years ago or so.
--
"This was a Golden Age, a time of high adventure, rich living and
hard dying... but nobody thought so." -- Alfred Bester, TSMD
Nov 15 '05 #2
Kosio wrote:
Hello,

I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature.

The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number).

Any ideas?


As this is an algorithm question, not a "C" one, you might get
more opinions in comp.programmin g. You could get digits from a
table with 1000000 entries. Or do /1000 and %1000 and get them from a
table with 1000 entries. Since you care about efficiency, I assume
using sprintf is right out. I'm sure there are other ways...

-David

Nov 15 '05 #3
In article <11************ **********@g47g 2000cwa.googleg roups.com>,
Kosio <aa********@gma il.com> wrote:

I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature.

The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number).

Any ideas?


Here are some thoughts, in no particular order:

1. There may be some compiler options to help you out. A lot
of RISC compilers (incl. gcc) default to the mostly widely
compatible instruction set for a given processor. If you are
willing to de-support older/cheaper versions of a CPU then
you may be able to take advantage of division in hardware.

8. An alternative is to ship two versions of a function, one
compiled for hardware division and one w/o. You can either
select at install time or run time whatever works best.

2. You may be able to exploit some redundancy in the input
data. Special-case -100 thru +100 or so in an array, and/or
cache the last several numbers outside that range. You
don't have to speed it up for all numbers, just most of the
ones actually encountered.

5. Don't store the numbers as numbers. Put them in bytes
or nybbles and when you need to do arithmetic on them,
the conversion to numbers will only require multiply and add,
not division, so it should be faster.

4. Depending on the abilities of the CPU and the dedication
of whoever implented stdlib.h on your platform, it is
theoretically possible that div() is faster than doing
separate % and / operations.
Nov 15 '05 #4
"Kosio" <aa********@gma il.com> writes:
I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature.

The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number).


Before you consider using an algorithm other than the obvious one, do
some actual performance measurements. If extracting digits is really
a bottleneck, then by all means do as much optimization as you need.
(I don't have any good ideas about how to do that.) But if it's not a
bottleneck, don't bother making it faster.

Why do you need the decimal digits? The most obvious reason is for
human-readable output; in that case, the time to perform the output is
likely to exceed the time to extract the digits. If it's not for
human-readable output, consider whether you can use octal or
hexadecimal digits instead.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5
Before you consider using an algorithm other than the obvious one, do
some actual performance measurements. If extracting digits is really
a bottleneck, then by all means do as much optimization as you need.
(I don't have any good ideas about how to do that.) But if it's not a
bottleneck, don't bother making it faster.

Why do you need the decimal digits? The most obvious reason is for
human-readable output; in that case, the time to perform the output is
likely to exceed the time to extract the digits. If it's not for
human-readable output, consider whether you can use octal or
hexadecimal digits instead.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


My reasoning is that I want to send a number to a 7 segment, 8-digit
LED display, but I have to know each individual number to set the
display. These numbers will be updated fairly often. I am using an
SPI protocol to send data between the micro and the LED Display...
Basically I send the numbers digit by digit to the display, so I need
to know individual digits of the numbers passed to the micro.

I'll have to look more into how the division algorith works on this
compiler, but with limited ROM space on the micro (and I'm tight as it
is already), two lines of division code take up roughly 3% of my
program space.

Thanks for the advice all,
Aaron

Nov 15 '05 #6
In article <11************ **********@g47g 2000cwa.googleg roups.com> "Kosio" <aa********@gma il.com> writes:
I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature.

The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number).


If you have 64 bit integers the following will do it:

char repr[7];

void convert(unsigne d long long z) {
unsigned long long a = z * 687195;
unsigned long long c;
int shift = 36;
int i;
for(i = 0; i < 6; i++) {
c = a >> shift;
repr[i] = (int)c + '0';
a = a - (c << shift);
a += (a << 2);
shift--;
}
repr[6] = 0;
}
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #7
In article <11************ **********@g44g 2000cwa.googleg roups.com>,
Kosio <aa********@gma il.com> wrote:
My reasoning is that I want to send a number to a 7 segment, 8-digit
LED display, but I have to know each individual number to set the
display. These numbers will be updated fairly often.
In the old days, there were dedicated chips for this purpose...
I am using an
SPI protocol to send data between the micro and the LED Display...
Basically I send the numbers digit by digit to the display, so I need
to know individual digits of the numbers passed to the micro.
Does that imply that you are sending the digits in sequence?
That's a bit different than the task of being able to extract an
arbitrary digit from the middle of a number.

Does the protocol allow the least significant digit to be sent
first?

I'll have to look more into how the division algorith works on this
compiler, but with limited ROM space on the micro (and I'm tight as it
is already), two lines of division code take up roughly 3% of my
program space.


You only have about 64 instruction locations available for your
entire program??
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Nov 15 '05 #8

"Kosio" <aa********@gma il.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Hello,

I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature.

The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number).

Any ideas?


I usually use assembler when programming microcontroller s, but here is the
basic algorithm that should do what you want in C.

void Bin2Ascii (int binval)
{
int plv[]={10,100,1000,1 0000,100000};
int place=5;
int retval;
if(binval > 999999) return;
while(place--)
{
retval=0;
while(binval>pl v[place])
{
binval-=(plv[place]);
retval++;
}
printf("%d\n",r etval); // Print Hundred thousands,ten
thousands,thous ands,hundreds
}
printf("%d\n",b inval); // print ones
}

This should be faster and and consume less space than a general division
function.

Good luck,
Mike H.
Nov 15 '05 #9
Dik T. Winter wrote:
In article <11************ **********@g47g 2000cwa.googleg roups.com> "Kosio"
<aa********@gma il.com> writes:
> I know of a way to extract digits from a number using the %10 and
> divide by 10. But I am wondering if there is an algorithm out there
> that does not use a divide by 10 feature.
>
> The reason I ask is that I am programming on a RISC system where
> division is quite expensive, and I want to be able to extract the
> digits from an integer (the integer will be from 1 to 6 digits long,
> and I know how many digits are in the number).


If you have 64 bit integers the following will do it:

char repr[7];

void convert(unsigne d long long z) {
unsigned long long a = z * 687195;
unsigned long long c;
int shift = 36;
int i;
for(i = 0; i < 6; i++) {
c = a >> shift;
repr[i] = (int)c + '0';
a = a - (c << shift);
a += (a << 2);
shift--;
}
repr[6] = 0;
}


<OT, about algorithms>
Where does the black magic behind this function come from?
</OT>
--
Eric Laberge
Nov 15 '05 #10

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

Similar topics

4
6939
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract some part of it like all names or numbers from each line, simply text fom between e.g. second and third colon. And turn it
7
2893
by: D. Alvarado | last post by:
Hello, Can regular expressions help me here? I want to extract a number from a string which will always be of the form "parameter###" where "###" is an arbitrary number of numeric digits. So if my string contained parameter24 parameter8 parameter90210
7
2983
by: Raphi | last post by:
Hi, I'm trying to clean up a large database in Access. I have one field for address, which needs to be broken up into Street Number, Street Name, and Street Label (St., Road, etc.) The problem is that the data is very dirty. So some addresses will be standard "456 XYZ Road," while others won't have a number and will just say "XYZ...
5
6860
by: ORC | last post by:
Is there an easy way to extract a number from a string like: result = ExtractNumber("this is a 4 string"); which should give the result = 4; Thanks Ole
14
9088
by: RonHouston | last post by:
Hi, I was wondering if someone could help me on this one. Is there anyway to format a string/integer etc so that it always outputs a 4 digits number? thank you. Ron.
7
30937
by: iheartvba | last post by:
I am trying to extract numbers from a string in the access query builder, all I need is the position of the numbers so the instr function will do. However I need a code like InStr(.,"") where ] "" can be any number from 0 - 9. I can't figure out what to use as a wild card for numbers. Please help Thank You
4
2949
by: joan fruchtalle | last post by:
Hi, I am working on a VoIP project, in that there is a small task of extracting Port Number from the incoming SDP which is stored in a Buffer. Let me say it in another way, I have a text which is stored in a Buffer (lets say 'string21' ) and from that I need to extract a Port Number and assign it to an Integer variable (lets say 'audio_port'...
0
7416
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7360
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7761
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7363
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7701
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5899
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4906
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3400
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.