Skip to content

Instantly share code, notes, and snippets.

@hjroh0315
Created January 12, 2022 12:28
Show Gist options
  • Save hjroh0315/0ff4c1632d21d8c2f519ba24b843487a to your computer and use it in GitHub Desktop.
Save hjroh0315/0ff4c1632d21d8c2f519ba24b843487a to your computer and use it in GitHub Desktop.
rotr, rotl에 대한 크기를 런타임에 지정 가능한 구현
#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