|
Boost : |
From: Greg Colvin (Gregory.Colvin_at_[hidden])
Date: 2002-12-17 16:00:14
At 01:34 PM 12/17/2002, Greg Colvin wrote:
>At 09:47 AM 12/17/2002, you wrote:
>>I hope it is permissible to ask a mp question.
>>
>>I'd like to have a template parameter is an int. If represents an
>>arithmetic shift of an integral value. If the parameter is positive
>>I'd like to shift left, and if negative shift right.
>>
>>Is it feasible to implement something like this? Any hints?
>
> template <typename T, typename U> T shift(T val, U bits) {
> if (bits > 0)
> return val >> bits;
> else if (bits < 0)
> return val << -bits;
> else
> return val;
> }
Sorry, I misunderstood the question. The following is
simpler than Daniel's solution, but counts on the
compiler optimizing away the unreachable paths.
template <int bits, typename T> T shift(T val) {
if (bits > 0)
return val >> bits;
else if (bits < 0)
return val << -bits;
else
return val;
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk