S
shaun
I have a function for returning the value of a bit field in a number:
template<typename T>
T bitfield(const T num, const unsigned int bitStart, const unsigned int
bitEnd){
T mask, shiftedNumber;
unsigned int maskLength = bitEnd-bitStart+1;
shiftedNumber = num>>bitStart;
mask = (1<<maskLength)-1;
return (shiftedNumber & mask);
};
I should like to specialize this for a bitset such that it always
returns an unsigned int, but a bitset<32> is not the same as a
bitset<16>... groping around a bit, I am trying:
template <int N>
unsigned int bitfield(const bitset<N> num, const unsigned int bitStart,
const unsigned int bitEnd){
unsigned int mask, shiftedNumber;
unsigned int maskLength = bitEnd-bitStart+1;
shiftedNumber = num>>bitStart;
mask = (1<<maskLength)-1;
return (shiftedNumber & mask);
};
but all I get is:
In file included from /Volumes/Shauns stuff/Developer/learning
C++/bitfield/main.cpp:2:
/Volumes/Shauns stuff/Developer/learning C++/bitfield/bitfield.hpp:21:
error: parse error before `,' token
where line 21 is the first line of my function specialized for bitset.
Is there a way to do this?
cheers
shaun
template<typename T>
T bitfield(const T num, const unsigned int bitStart, const unsigned int
bitEnd){
T mask, shiftedNumber;
unsigned int maskLength = bitEnd-bitStart+1;
shiftedNumber = num>>bitStart;
mask = (1<<maskLength)-1;
return (shiftedNumber & mask);
};
I should like to specialize this for a bitset such that it always
returns an unsigned int, but a bitset<32> is not the same as a
bitset<16>... groping around a bit, I am trying:
template <int N>
unsigned int bitfield(const bitset<N> num, const unsigned int bitStart,
const unsigned int bitEnd){
unsigned int mask, shiftedNumber;
unsigned int maskLength = bitEnd-bitStart+1;
shiftedNumber = num>>bitStart;
mask = (1<<maskLength)-1;
return (shiftedNumber & mask);
};
but all I get is:
In file included from /Volumes/Shauns stuff/Developer/learning
C++/bitfield/main.cpp:2:
/Volumes/Shauns stuff/Developer/learning C++/bitfield/bitfield.hpp:21:
error: parse error before `,' token
where line 21 is the first line of my function specialized for bitset.
Is there a way to do this?
cheers
shaun