Trying to flip the order of bits in std::bitset

后端 未结 1 521
南旧
南旧 2021-01-21 15:22

I\'m working on a structure that uses std::bitset and it looks like this:

Register.h

#pragma once

#include 
#include <         


        
相关标签:
1条回答
  • 2021-01-21 15:52

    I don't know of a single algorithm that does this but you can use a combination of bitset::to_string and std::reverse to do this.

    A minimal example.

    #include "Register.h"
    #include <algorithm>
    #include <bitset>
    #include <iostream>
    #include <string>
    
    int main() {
      vpc::Byte register_ = 169;
      std::cout << register_ << std::endl;
    
      auto str = register_.to_string();
      std::reverse(str.begin(), str.end());
      auto x = vpc::Byte(str);
      std::cout << x << std::endl;
    
      return 0;
    }
    

    See Demo Here.

    Output:

    10101001
    10010101
    

    This method can be used with any of the other bitset types you have defined.

    0 讨论(0)
提交回复
热议问题