473,385 Members | 1,555 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,385 software developers and data experts.

how to use bit operation to do \sqrt(n)

hello all,
n is an 32bit-integer, how to calculate
sqrt(n)
I know high(n) is the the value, but how to use bit operation to
implement this function.

thanks in advance.

yours
Yin

Oct 30 '06 #1
12 6278
Thomas Zhu wrote:
n is an 32bit-integer, how to calculate
sqrt(n)
I know high(n) is the the value, but how to use bit operation to
implement this function.
http://www.pobox.com/~qed/sqroot.html

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Oct 30 '06 #2

Thomas Zhu wrote:
hello all,
n is an 32bit-integer, how to calculate
sqrt(n)
I know high(n) is the the value, but how to use bit operation to
implement this function.

thanks in advance.

yours
Yin


are you referring to fixed point implementation of sqrt(n)
what is high(n)?

bye,
hurry.

Oct 30 '06 #3

hurry wrote:
are you referring to fixed point implementation of sqrt(n)
what is high(n)?
if n is represented as a binary string, the first half is the sqrt(n),
which is high(n)

Oct 30 '06 #4
Ico
Thomas Zhu <zh********@gmail.comwrote:
hello all,
n is an 32bit-integer, how to calculate
sqrt(n)
I know high(n) is the the value, but how to use bit operation to
implement this function.

thanks in advance.

yours
Yin
Ah, the elusive square root,
It should be a cinch to compute.
But the best we can do
Is use powers of two
And iterate the method of Newt!

--
:wq
^X^Cy^K^X^C^C^C^C
Oct 30 '06 #5
Yin Zhu said:
>
hurry wrote:
>are you referring to fixed point implementation of sqrt(n)
what is high(n)?

if n is represented as a binary string, the first half is the sqrt(n),
which is high(n)
If that were true, the square root of 9 (binary 1001) would be 2 (binary
10), whereas in fact it is 3, and the square root of 32 (binary 100000)
would be 8 (100) whereas in fact it lies between 5 and 6, either of which
would be a better approximation than 8.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 30 '06 #6

Richard Heathfield wrote:
Yin Zhu said:

hurry wrote:
are you referring to fixed point implementation of sqrt(n)
what is high(n)?
if n is represented as a binary string, the first half is the sqrt(n),
which is high(n)

If that were true, the square root of 9 (binary 1001) would be 2 (binary
10), whereas in fact it is 3, and the square root of 32 (binary 100000)
would be 8 (100) whereas in fact it lies between 5 and 6, either of which
would be a better approximation than 8.
What I need is not to get the exact sqrt, I just need a fast way to get
the
high half bits of a integer.
(I am trying to implemting van Emde Boas Tree, I want to use a fast
sqrt instead
the one in math.h)
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 30 '06 #7
Yin Zhu wrote:
Richard Heathfield wrote:
>>Yin Zhu said:

>>>hurry wrote:
are you referring to fixed point implementation of sqrt(n)
what is high(n)?

if n is represented as a binary string, the first half is the sqrt(n),
which is high(n)

If that were true, the square root of 9 (binary 1001) would be 2 (binary
10), whereas in fact it is 3, and the square root of 32 (binary 100000)
would be 8 (100) whereas in fact it lies between 5 and 6, either of which
would be a better approximation than 8.

What I need is not to get the exact sqrt, I just need a fast way to get
the
high half bits of a integer.
(I am trying to implemting van Emde Boas Tree, I want to use a fast
sqrt instead
the one in math.h)
There are lots of square root implementations kicking around;
websnarf has already referred you to a web page that has several.
If what you care about is speed you should try *all* of them,
including sqrt() from <math.h>, to see which actually turns out
to be fastest in your application(s) on your machine(s).

Converting from integer to double and back sounds like -- and
may be -- a lot of work, but on some systems it could be faster
than purely-integer alternatives. Floating-point sqrt() is a
built-in hardware instruction on many machines, which may give it
enough of an advantage to compensate for the conversion overhead
(especially on machines where integer division is expensive).

Measure, measure, measure!

--
Eric Sosman
es*****@acm-dot-org.invalid
Oct 30 '06 #8
On 29 Oct 2006 20:29:38 -0800, "Thomas Zhu" <zh********@gmail.com>
wrote:
>hello all,
n is an 32bit-integer, how to calculate
sqrt(n)
I know high(n) is the the value, but how to use bit operation to
implement this function.

thanks in advance.
What you need is to post in a more appropriate newsgroup
(comp.programming would be appropriate), to state your problem more
clearly, and to describe more clearly what the acceptable error is.

Judging from your further comments what you want is a fast, rough
approximation for an integer square root. One way to do this is to
find p, the position of the high order bit of n, and right shift n p/2
bits. If you know that n is large (greater than 2**16) and you are
willing to accept a really rough approximation just right shift n 16
bits (that gives you your "high") and add 1.

If you restate your request in comp.programming and state your
requirements more clearly, you should get a better answer than this.
In particular, you should characterize n more clearly than being a 32
bit integer, the level of accuracy that you desire (e.g., within a
factor of two), and whether there are storage constraints, i.e.,
whether it's okay to use lookup tables.

Oct 30 '06 #9
"Yin Zhu" <zh********@gmail.comwrote:
Richard Heathfield wrote:
Yin Zhu said:
hurry wrote:
>
>are you referring to fixed point implementation of sqrt(n)
>what is high(n)?
>
if n is represented as a binary string, the first half is the sqrt(n),
which is high(n)
If that were true, the square root of 9 (binary 1001) would be 2 (binary
10), whereas in fact it is 3, and the square root of 32 (binary 100000)
would be 8 (100) whereas in fact it lies between 5 and 6, either of which
would be a better approximation than 8.
What I need is not to get the exact sqrt, I just need a fast way to get
the high half bits of a integer.
Just the high half bits? It really doesn't matter to you whether it's at
all an exact representation of any square root? If so, use & and >>,
combined with a judicious choice of sizeof (int) and CHAR_BIT.

Richard
Oct 30 '06 #10

"Eric Sosman" <es*****@acm-dot-org.invalidwrote in message
news:2-******************************@comcast.com...
Yin Zhu wrote:
>Richard Heathfield wrote:
>>>Yin Zhu said:
hurry wrote:
>are you referring to fixed point implementation of sqrt(n)
>what is high(n)?

if n is represented as a binary string, the first half is the sqrt(n),
which is high(n)

If that were true, the square root of 9 (binary 1001) would be 2 (binary
10), whereas in fact it is 3, and the square root of 32 (binary 100000)
would be 8 (100) whereas in fact it lies between 5 and 6, either of which
would be a better approximation than 8.

What I need is not to get the exact sqrt, I just need a fast way to get
the
high half bits of a integer.
(I am trying to implemting van Emde Boas Tree, I want to use a fast
sqrt instead
the one in math.h)

There are lots of square root implementations kicking around;
websnarf has already referred you to a web page that has several.
If what you care about is speed you should try *all* of them,
including sqrt() from <math.h>, to see which actually turns out
to be fastest in your application(s) on your machine(s).
A slight nit here. <math.hdoes not contain sqrt(). It merely provides
a prototype for the function that is included in some system library,
usually libm.a
>
Converting from integer to double and back sounds like -- and
may be -- a lot of work, but on some systems it could be faster
than purely-integer alternatives. Floating-point sqrt() is a
built-in hardware instruction on many machines, which may give it
enough of an advantage to compensate for the conversion overhead
(especially on machines where integer division is expensive).

Measure, measure, measure!

--
Eric Sosman
es*****@acm-dot-org.invalid
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Oct 30 '06 #11
Yin Zhu wrote:
hurry wrote:
>are you referring to fixed point implementation of sqrt(n)
what is high(n)?

if n is represented as a binary string, the first half is the sqrt(n),
which is high(n)
This is obviously inadequate. n must be represented as a binary string
with a first digit of 1. Consider a 16-bit representation of, for
example, 17 (0000 0000 0001 0001), the first half of which has a value
of 0, a not very useful result.

Further, 'the first half' has no meaning (without further specification)
for bitstrings of odd length. Consider the bit representation with a
leading 1 of, for example, 17 (10001). Is the first half 10 (2) or 100
(4). Either answer commits you to the first and second half having
different lengths, which is not an ordinary meaning of 'half'.
Oct 30 '06 #12
Thomas Zhu wrote:
>
hello all,
n is an 32bit-integer, how to calculate
sqrt(n)
I know high(n) is the the value, but how to use bit operation to
implement this function.
unsigned isqrt(unsigned n, unsigned *remainder)
{
unsigned power_of_4, root, sum;

power_of_4 = ((unsigned)-1 >2 - (unsigned)-1 % 3) + 1;
root = 0;
do {
sum = root + power_of_4;
if (n >= sum) {
n -= sum;
root = sum + power_of_4;
}
root >>= 1;
power_of_4 >>= 2;
} while (power_of_4 != 0);
if (remainder != NULL) {
*remainder = n;
}
return root;
}

--
pete
Nov 1 '06 #13

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

Similar topics

6
by: Prune Tracy | last post by:
Hi, I can't get this seemingly harmless code to compile with Comeau online. #include <cmath> typedef float (*float_func)(float); typedef double (*double_func)(double); int main() {...
10
by: pauldepstein | last post by:
I am writing a program which will use the ceiling and floor of square roots. If a positive integer is an exact square (and is not overly huge), does sqrt return an integer-valued real? Or...
7
by: Aric | last post by:
Hi, I'm a bit new to programming in general, really I've just picked it up as a hobby, and so I've started by reading "Practical C Programming" by Steve Oualline. Things have been going fine in...
2
by: Maor Mishkin | last post by:
I've changed from double to decimal calculations but I can't find a math library for decimal (mainly for sqrt), Thanks Maor
13
by: Michael McNeil Forbes | last post by:
I would like to write a module that provides some mathematical functions on top of those defined in math, cmath etc. but I would like to make it work with "any" type that overloads the math...
13
by: siggi | last post by:
Hi all, this is a newbie question on : Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win32 PC with WinXP In http://www.python.org/doc/2.3.5/lib/module-math.html I read:
30
by: copx | last post by:
I am writing a program which uses sqrt() a lot. A historically very slow function but I read on CPUs with SSE support this is actually fast. Problem: C's sqrt() (unlike C++ sqrt()) is defined to...
13
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.