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

Getting number of ones in a number in compile-time

I wrote the following code to get number of ones in a number in
compile-time.

Calling function is
static_get_number_of_ones<std::size_t, 17>().
static_get_number_of_ones<int, 17>()
static_get_number_of_ones<long, 17>().

I would like the static_get_number_of_ones() function to have default
value for typename T, but 'typename T' is first in the list of
arguments.

Is it possible to do that in other way?

// -----------------------------------
#include <iostream>

template <typename T, T N, int S = sizeof(T) & CHAR_BIT>
struct static_number_of_ones
{
static const T m_value = static_number_of_ones<T, N, S -
1>::m_value >1;
static const int m_count = static_number_of_ones<T, N, S -
1>::m_count + (static_number_of_ones<T, N, S - 1>::m_value & 0x1);
};

template <typename T, T N>
struct static_number_of_ones<T, N, 0>
{
static const T m_value = N;
static const int m_count = 0;
};

template <typename T, T N>
std::size_t static_get_number_of_ones()
{
return static_number_of_ones<T, N>::m_count;
}
int main()
{

std::cout << static_get_number_of_ones<std::size_t, 17>() <<
std::endl;
return 0;

}
// ------------------------------

Alex Vinokur
Aug 12 '08 #1
1 1290
On Aug 12, 8:33*am, Alex Vinokur <ale...@users.sourceforge.netwrote:
I wrote the following code to get number of ones in a number in
compile-time.

Calling function is
static_get_number_of_ones<std::size_t, 17>().
static_get_number_of_ones<int, 17>()
static_get_number_of_ones<long, 17>().

I would like the static_get_number_of_ones() function to have default
value for typename T, but 'typename T' is first in the list of
arguments.

Is it possible to do that in other way?
Something like:
template <std::size_t N>
std::size_t static_get_number_of_ones()
{
return static_number_of_ones<std::size_t, N>::m_count;
}
>
// -----------------------------------
#include <iostream>

template <typename T, T N, int S = sizeof(T) & CHAR_BIT>
struct static_number_of_ones
{
* static const T * m_value = static_number_of_ones<T, N, S -
1>::m_value >1;
* static const int m_count = static_number_of_ones<T, N, S -
1>::m_count + (static_number_of_ones<T, N, S - 1>::m_value & 0x1);
std::cout << static_get_number_of_ones<17>() << std::endl;
>
};

template <typename T, T N>
struct static_number_of_ones<T, N, 0>
{
* static const T * m_value = N;
* * * * static const int m_count = 0;

};

template <typename T, T N>
std::size_t static_get_number_of_ones()
{
* * * * return static_number_of_ones<T, N>::m_count;

}

int main()
{

* std::cout << static_get_number_of_ones<std::size_t, 17>() <<
std::endl;
* * * * return 0;

}

// ------------------------------
Alex Vinokur

Aug 12 '08 #2

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

Similar topics

2
by: Bacci | last post by:
This isn't necessarily a php-specific question, but I'm looking for a formula to determine the fraction portion of a decimal number. For instance, 8.25. I could simply search the number (as a...
4
by: Adrian Gibbons | last post by:
Hello, I need to get the units of a number. i.e. if I had 12345 then I would want '5'. I was looking at dividing the number by ten and using the modulus operator repeatedly until I had only...
10
by: RWC | last post by:
Hello, I'm having a problem that I'm having a lot of trouble with. I'm running Access XP, with all the updates, including service pack 2. I'm on a Windows XP machine. I have a database which...
2
by: nfr | last post by:
I keep getting the following warning in my compile: Warning: The dependency 'WBWebServices, Version=1.0.1289.13943, Culture=neutral' in project 'WBWin' cannot be copied to the run directory...
5
by: Scott Chang | last post by:
Hi all, I copied a set of VC++ version 6 source code of the 'cppdll'(2 projects) from a website and put the cppdll.cpp, cppdll.def, cpp.h, (as the 1st project) and test.cpp (as the 2nd project)...
9
by: Brian | last post by:
So I have this: double x = .25; double y = .5; double z = .25; double probability; int index; while(index < 30) {
32
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is...
4
by: tkpmep | last post by:
I've just bought an iMac (OS X 10.5.2, will almost immediately jump to 10.5.3), and am looking to install Python on it, and to use it with XCode, Apple's IDE. Some googling suggests that a number...
5
by: tshad | last post by:
I have the following class in my VS 2008 project that has a namespace of MyFunctions. ********************************* Imports System Imports System.Text.RegularExpressions Namespace...
0
by: ppletkov | last post by:
Hi - I apologize if this is a simple question in advance :-) (hopefully it is so its quickly resolved!) i'm trying to figure out why i keep getting an error (Compile Error : expected function or...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.