Цитата(pilotnet @ 11.8.2010, 11:39 ) | | template<class T> T ror(T val, unsigned int n) |
| Код | namespace BitwiseShift{
enum { byteSize = 8 };
template<typename T> struct Unsigned { typedef T Type; private: enum { simpleStaticAssert = T() >> 1 }; }; template<> struct Unsigned<char > { typedef unsigned char Type; }; template<> struct Unsigned<short> { typedef unsigned short Type; }; template<> struct Unsigned<int > { typedef unsigned int Type; }; template<> struct Unsigned<long > { typedef unsigned long Type; };
template<typename T> inline T lRight (T arg, std::size_t shift){ typedef typename Unsigned<T>::Type U; return static_cast<T>((static_cast<U>(arg) >> shift)); }
template<typename T> inline T rRight (T arg, std::size_t shift){ typedef typename Unsigned<T>::Type U; U temp = static_cast<U>(arg) >> shift; return static_cast<T>(temp | static_cast<U>(arg) << (sizeof(T) * byteSize - shift)); }
template<typename T> inline T rLeft (T arg, std::size_t shift){ typedef typename Unsigned<T>::Type U; U temp = static_cast<U>(arg) << shift; return static_cast<T>(temp | static_cast<U>(arg) >> (sizeof(T) * byteSize - shift)); }
}
int main () {
std::cout<<BitwiseShift::lRight(-21, 1)<<"\n"; std::cout<<BitwiseShift::lRight(-21, 2)<<"\n"; std::cout<<BitwiseShift::lRight(-21, 3)<<"\n"; std::cout<<BitwiseShift::lRight(-21, 4)<<"\n"; std::cout<<BitwiseShift::lRight(-21, 5)<<"\n\n";
std::cout<<BitwiseShift::rRight(static_cast<unsigned>(-1), 1)<<"\n"; std::cout<<BitwiseShift::rLeft(-1, 1)<<std::endl;
return 0; } |
| Код | 2147483637 1073741818 536870909 268435454 134217727
4294967295 -1
|
|