Created
January 12, 2022 12:28
-
-
Save hjroh0315/0ff4c1632d21d8c2f519ba24b843487a to your computer and use it in GitHub Desktop.
rotr, rotl에 대한 크기를 런타임에 지정 가능한 구현
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <bitset> | |
using namespace std; | |
template<class T> | |
T rotrN(T in, int N, int s); | |
template<class T> | |
T rotlN(T in, int N, int s); | |
template<class T> | |
T rotrN(T in, int N, int s) | |
{ | |
int r = s % N; | |
if(r==0)return in; | |
if(r>0)return ((in >> r) | (in << (N - r))); | |
return rotlN(in,N,-s); | |
} | |
template<class T> | |
T rotlN(T in, int N, int s) | |
{ | |
int r = s % N; | |
if(r==0)return in; | |
if(r>0)return ((in << r) | (in >> (N - r))); | |
return rotrN(in,N,-s); | |
} | |
int main() | |
{ | |
int N,s; | |
cin>>N>>s; | |
bitset<10>b; | |
cin>>b; | |
cout<<rotrN(b,N,s)<<endl<<rotlN(b,N,s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment