473,473 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Log-base-2 function as compile-time constant


I've mentioned his name a few times here lately, but over on comp.lang.c,
Hallvard B Furuseth has come up with a Log-base-2 function that serves as a
compile-time constant.

I'm not going to pretend to even begin to understand how it works, but if
anyone's interested, here's a link to the post:

http://groups.google.ie/group/comp.l...e4a60b0?hl=en&
Next time there's a rainy day, I might sit down with a pen and paper and
try to figure it out.
--

Frederick Gotham
Jul 1 '06 #1
1 10392
Frederick Gotham wrote:
I've mentioned his name a few times here lately, but over on comp.lang.c,
Hallvard B Furuseth has come up with a Log-base-2 function that serves as a
compile-time constant.

I'm not going to pretend to even begin to understand how it works, but if
anyone's interested, here's a link to the post:

http://groups.google.ie/group/comp.l...e4a60b0?hl=en&

Next time there's a rainy day, I might sit down with a pen and paper and
try to figure it out.
My goodness, how convoluted. Luckily in C++ we have no need for such
macro-heavy measures when we want compile-time constants:

#include "boost/static_assert.hpp"

template <size_t N, size_t base=2>
struct log_
{
enum { value = 1 + log_<N/base, base>::value };
};

template <size_t base>
struct log_<1, base>
{
enum { value = 0 };
};

template <size_t base>
struct log_<0, base>
{
enum { value = 0 };
};

int main()
{
BOOST_STATIC_ASSERT(log_<0>::value == 0);
BOOST_STATIC_ASSERT(log_<1>::value == 0);
BOOST_STATIC_ASSERT(log_<2>::value == 1);
BOOST_STATIC_ASSERT(log_<3>::value == 1);
BOOST_STATIC_ASSERT(log_<4>::value == 2);
BOOST_STATIC_ASSERT(log_<5>::value == 2);
BOOST_STATIC_ASSERT(log_<8>::value == 3);
BOOST_STATIC_ASSERT(log_<16>::value == 4);
BOOST_STATIC_ASSERT(log_<246>::value == 7);

return 0;
}

Experiment to your heart's content with other bases, negative numbers,
limits, etc.

Cheers,
Luke

Jul 2 '06 #2

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

Similar topics

10
by: TZoner | last post by:
1) Can one find the location of the 'Transaction Log' at: <Hard Disk>\Program Files\Microsoft SQL Server\MSSQL\Data\MyDb_Log.ldf? 2) Is it safe to delete it, as SQL will create a new Transaction...
5
by: Jay Chan | last post by:
The transaction log in a database in our SQLSERVER-2000 server has grown to 16GB. I cannot shrink the transaction log manually because it says that the entire 16GB log size is not free. This is...
5
by: Igor Solodovnikov | last post by:
Hi. I am trying to automatically backup transaction log when error 9002 happened. So i have created appropriate job and alert to catch this error. I have two instances of sql server under Windows...
2
by: rh0dium | last post by:
Hi all, So I have a slice of code which calls other python code. I have started to take a real liking to the logging module, but I want to extend this into the called python code. I have no...
4
by: hype | last post by:
Hi, How can I find out how much space has been used in the log file and how much of it is free or yet to be used ? Thanks, Hype
13
by: Lew | last post by:
Hi, I am a bit confused about what I'm seeing on my systems. I am trying to develop a script that will determine what percentage of the total log space is being used. If it is too high (80%)...
2
by: nek | last post by:
Greetings, I tried to implement log shipping using PULL method as described in 'The Basics of DB2 Log Shipping' by Dale McInnis, but found the subsequent "rollforward" did not go beyond the...
3
by: Phil Sherman | last post by:
What is the relationship between the snapshot elements: Log pages written Number write log IOs Number partial page log IOs There doesn't appear to be any information in my PDF System monitor...
9
by: Sean Shanny | last post by:
To all, I restarted postgres this morning to make a configuration file change take effect and when I looked at the serverlog to make sure things were ok I found the prior shutdown message(*in...
1
by: Hubert Fröhlich | last post by:
Hi list, when doing a large Insert I get in the logfile: 2004-08-03 07:23:46 LOG: recycled transaction log file "000001BA000000BD" 2004-08-03 07:34:00 LOG: recycled transaction log file...
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
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
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.