473,326 Members | 2,010 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,326 software developers and data experts.

how to implement sum of bits using template metaprogramming

I'm tryin to convert this:
long sumOfBits(int n) {
long x = 0;
int i;
for (i = n - 1; i >= 0; i--)
x += (1 << i);
return x;
}

into something like this:
template <int nstruct sumOfBits {
static const unsigned char u8value = (unsigned char)((1 << n) +
sumOfBits<n - 1>::u8value);
static const unsigned __int16 u16value = (unsigned __int16)((1 << n)
+ sumOfBits<n - 1>::u16value);
static const unsigned __int32 u32value = (unsigned __int32)((1 << n)
+ sumOfBits<n - 1>::u32value);
static const unsigned __int64 u64value = (unsigned __int64)((1 << n)
+ sumOfBits<n - 1>::u64value);
};
template <struct sumOfBits <0{
static const unsigned char u8value = (unsigned char)1;
static const unsigned __int16 u16value = (unsigned __int16)1;
static const unsigned __int32 u32value = (unsigned __int32)1;
static const unsigned __int64 u64value = (unsigned __int64)1;
};

and so far, all expansions, that have multiples of 8 (8, 16, 32, 64)
return 0 due to overflow.
Any way to correctly rewrite the template?

Sep 25 '07 #1
3 1691
Alf P. Steinbach wrote:
* an********@gmail.com:
>I'm tryin to convert this:
long sumOfBits(int n) {
long x = 0;
int i;
for (i = n - 1; i >= 0; i--)
x += (1 << i);
return x;
}

into something like this:
template <int nstruct sumOfBits {
static const unsigned char u8value = (unsigned char)((1 << n) +
sumOfBits<n - 1>::u8value);
static const unsigned __int16 u16value = (unsigned __int16)((1 << n)
+ sumOfBits<n - 1>::u16value);
static const unsigned __int32 u32value = (unsigned __int32)((1 << n)
+ sumOfBits<n - 1>::u32value);
static const unsigned __int64 u64value = (unsigned __int64)((1 << n)
+ sumOfBits<n - 1>::u64value);
};
template <struct sumOfBits <0{
static const unsigned char u8value = (unsigned char)1;
static const unsigned __int16 u16value = (unsigned __int16)1;
static const unsigned __int32 u32value = (unsigned __int32)1;
static const unsigned __int64 u64value = (unsigned __int64)1;
};

and so far, all expansions, that have multiples of 8 (8, 16, 32, 64)
return 0 due to overflow.
Any way to correctly rewrite the template?

Off the cuff, to reproduce the behavior of the original non-tempalte
function:

template< int n >
struct SumOfBits
{
enum{ value = (1L << (n-1)) | ((1L << (n-1)) - 1) };
};
I believe you meant

template< unsigned n struct SumOfBits
{
enum{ value = (1L << (n - 1)) + SumOfBits<n-1>::value };
};

template<struct SumOfBits<1>
{
enum { value = 1 };
};

template<struct SumOfBits<0>
{
enum { value = 0 };
};

You need recursion in templates to emulate the loop.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '07 #2
Alf P. Steinbach wrote:
* an********@gmail.com:
>I'm tryin to convert this:
long sumOfBits(int n) {
long x = 0;
int i;
for (i = n - 1; i >= 0; i--)
x += (1 << i);
return x;
}

into something like this:
template <int nstruct sumOfBits {
static const unsigned char u8value = (unsigned char)((1 << n) +
sumOfBits<n - 1>::u8value);
static const unsigned __int16 u16value = (unsigned __int16)((1 << n)
+ sumOfBits<n - 1>::u16value);
static const unsigned __int32 u32value = (unsigned __int32)((1 << n)
+ sumOfBits<n - 1>::u32value);
static const unsigned __int64 u64value = (unsigned __int64)((1 << n)
+ sumOfBits<n - 1>::u64value);
};
template <struct sumOfBits <0{
static const unsigned char u8value = (unsigned char)1;
static const unsigned __int16 u16value = (unsigned __int16)1;
static const unsigned __int32 u32value = (unsigned __int32)1;
static const unsigned __int64 u64value = (unsigned __int64)1;
};

and so far, all expansions, that have multiples of 8 (8, 16, 32, 64)
return 0 due to overflow.
Any way to correctly rewrite the template?

Off the cuff, to reproduce the behavior of the original non-tempalte
function:

template< int n >
struct SumOfBits
{
enum{ value = (1L << (n-1)) | ((1L << (n-1)) - 1) };
};
Well, the loop is uncalled for, so you're, right, it could just be

template<unsigned n>
struct SumOfBits
{
enum { value = (1 << n) - 1 }; // no need to OR anything
};

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '07 #3
Alf P. Steinbach wrote:
* Victor Bazarov:
>Alf P. Steinbach wrote:
>>* an********@gmail.com:
I'm tryin to convert this:
long sumOfBits(int n) {
long x = 0;
int i;
for (i = n - 1; i >= 0; i--)
x += (1 << i);
return x;
}

into something like this:
template <int nstruct sumOfBits {
static const unsigned char u8value = (unsigned char)((1 << n) +
sumOfBits<n - 1>::u8value);
static const unsigned __int16 u16value = (unsigned __int16)((1 <<
n) + sumOfBits<n - 1>::u16value);
static const unsigned __int32 u32value = (unsigned __int32)((1 <<
n) + sumOfBits<n - 1>::u32value);
static const unsigned __int64 u64value = (unsigned __int64)((1 <<
n) + sumOfBits<n - 1>::u64value);
};
template <struct sumOfBits <0{
static const unsigned char u8value = (unsigned char)1;
static const unsigned __int16 u16value = (unsigned __int16)1;
static const unsigned __int32 u32value = (unsigned __int32)1;
static const unsigned __int64 u64value = (unsigned __int64)1;
};

and so far, all expansions, that have multiples of 8 (8, 16, 32,
64) return 0 due to overflow.
Any way to correctly rewrite the template?
Off the cuff, to reproduce the behavior of the original non-tempalte
function:

template< int n >
struct SumOfBits
{
enum{ value = (1L << (n-1)) | ((1L << (n-1)) - 1) };
};

Well, the loop is uncalled for, so you're, right, it could just be

template<unsigned n>
struct SumOfBits
{
enum { value = (1 << n) - 1 }; // no need to OR anything
};

I believe this latter version has Undefined Behavior in the case of n
= number of bits in int.
So did the function the OP wanted converted.
Also, the original functions had long
result.
When an enum is defined the underlying type is picked by the compiler,
you can take control in your hands by doing

template<unsigned n>
struct SumOfBits
{
static long const value = (1L << n) - 1;
};

but I guess 'enum' is a bit more "portable" and hence it was suggested
by you.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '07 #4

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

Similar topics

10
by: Ray | last post by:
I am reading Andrei Alexandrescu's book. The ideas presented there sound really good, but I wonder--is there really a lot of people using it? Or it's simply too esoteric for mortals? Cheers Ray
7
by: Joe | last post by:
Hi, I found a concept named template metaprogramming that can be used in C+ + code at compile-time. I am a beginner at C++. But I am a programmer on the .NET platform. Do you know if template...
1
by: Ted | last post by:
I have cross posted this to comp.lang.c++ and to sci.math.num- analysis in the belief that the topic is of interest to some in both groups. I am building my toolkit, in support of my efforts in...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.