How to get the bitwise left-rotation of X by N positions (left circular shift) using bitset in C++

1 Answer

0 votes
#include <bitset>
#include <cstdint>
#include <iostream>
 
// C++ 20 (and above)
  
int main()
{
    // constexpr T rotl(T x, int s) noexcept;
     
    const std::uint8_t i = 0b01101110;
    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, 2) = " << std::bitset<8>(std::rotl(i, 2)) << '\n';
    std::cout << "rotl(i, 4) = " << std::bitset<8>(std::rotl(i, 4)) << '\n';
}
 
 
/*
run:
 
i          = 01101110
rotl(i, 0) = 01101110
rotl(i, 1) = 11011100
rotl(i, 2) = 10111001
rotl(i, 4) = 11100110
 
*/

 



answered Oct 13, 2024 by avibootz
edited Oct 13, 2024 by avibootz

Related questions

1 answer 126 views
1 answer 164 views
1 answer 164 views
1 answer 181 views
1 answer 151 views
1 answer 152 views
...