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

round to the nearest power of 4


Would this expression round an integer n to the nearest power of 4 ?

((n-1)|3) + 1

Jan 29 '07 #1
4 3044
naren2...@yahoo.com wrote:
Would this expression round an integer n to the nearest power of 4 ?

((n-1)|3) + 1
No. Just try it with some simple numbers (1 through 10), then you'll
get an idea of what it's supposed to do (but doesn't always get right).

Jan 29 '07 #2
in 716941 20070129 062444 na*******@yahoo.com wrote:
>Would this expression round an integer n to the nearest power of 4 ?

((n-1)|3) + 1
add 3 then lose 2 low-order bits by shifting (note my assembler background?)
Jan 29 '07 #3
na*******@yahoo.com wrote:
Would this expression round an integer n to the nearest power of 4 ?

((n-1)|3) + 1
No, it rounds to something-else of 4.

Also, for powers, define "nearest": arithmetically, geometrically, or
unusually?

Richard
Jan 29 '07 #4
<na*******@yahoo.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
>
Would this expression round an integer n to the nearest power of 4 ?

((n-1)|3) + 1
You need to define what you mean by "power" and by "round" and by "nearest".

Did you actually mean "multiple" (0, 4, 8, 12, 16, etc.)?

Or did you actually mean "power" (1, 4, 16, 64, etc.)?

Looking at your code ...

0 --0
1 --4
2 --4
3 --4
4 --4
5 --8
6 --8

So in some sense it does round to a near muliple (not power!) of 4. But not
the nearest one.

There are an endless variety of code constructs, depending on how efficient
one needs to be. Certainly

switch (n & 3)

comes to mind as a starting point.

Dave.

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 29 '07 #5

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

Similar topics

4
by: tertius | last post by:
Hi, I'm trying to round my float total to the nearest .05 cents. 12.01 should produce 12.00 0.14 should produce 0.10 2.28 " 2.25 703.81 " 703.80 ...
5
by: Marc A. Lefebvre US-775 | last post by:
I have a table that I need to extract values, determine their ratio, and then round to the nearest 50. SELECT NAME, MFG, ROUND((WEIGHT/VOLUME),50) AS WV_RATIO, COUNT(*) OVER (ORDER BY NAME, MFG,...
6
by: Penguin | last post by:
At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double...
9
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something...
3
by: Matt | last post by:
Anybody noticed that SQL Server rounds up if the value is half way between two rounded values, but C#'s Decimal.Round(Decimal,Int32) rounds to nearest even number? >From MSDN: "When d is exactly...
0
by: thaisummitneel | last post by:
sir I have created a database in ms-access.Connected database using ADODB I have written a query to round integer to nearest value such as con.execute"update tablename set...
4
by: Jassim Rahma | last post by:
I have a number, for example 0.152 or 1.729 and I want to allow to round to the first 0.010 or 0.050 or 0.100
6
by: dkirkdrei | last post by:
I am looking for a way to round a number (which will be displayed as a dollar amount) to the nearest nickel. Using the php round function only allows you to control the number of decimal places in...
0
by: Edwin.Madari | last post by:
>>round(76.1, -2) 100.0 80.0 76.0 builtin function round, will work for you...... Help on built-in function round in module __builtin__: round(...) round(number) -floating point number
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: 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...
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...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.