Bitwise rotation (Circular shift)

前端 未结 5 1841
粉色の甜心
粉色の甜心 2021-01-05 08:04

I was trying to make some code in C++ about “bitwise rotation” and I would like to make this by the left shif. I didn’t know how to code this, but I found a little code in “

5条回答
  •  旧时难觅i
    2021-01-05 08:09

    C++20 provides std::rotl and std::rotr in the header. An example from cppreference.com:

    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
        std::uint8_t i = 0b00011101;
        std::cout << "i          = " << std::bitset<8>(i) << '\n';
        std::cout << "rotl(i,0)  = " << std::bitset<8>(std::rotl(i,0)) << '\n';
        std::cout << "rotl(i,1)  = " << std::bitset<8>(std::rotl(i,1)) << '\n';
        std::cout << "rotl(i,4)  = " << std::bitset<8>(std::rotl(i,4)) << '\n';
        std::cout << "rotl(i,9)  = " << std::bitset<8>(std::rotl(i,9)) << '\n';
        std::cout << "rotl(i,-1) = " << std::bitset<8>(std::rotl(i,-1)) << '\n';
    }
    

    I believe std::bitset is only used in this example for its stream formatting.

提交回复
热议问题