本文介绍了尝试翻转std :: bitset中的位顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用std :: bitset的结构,它看起来像这样:

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

Register.h

#pragma once

#include <bitset>
#include <vector> // used for typedefs of int types

namespace vpc { // virtual pc

    typedef std::int8_t  i8;
    typedef std::int16_t i16;
    typedef std::int32_t i32;
    typedef std::int64_t i64;

    const unsigned int BYTE  = 0x08;
    const unsigned int WORD  = 0x10;
    const unsigned int DWORD = 0x20;
    const unsigned int QWORD = 0x40;

    typedef std::bitset<BYTE>  Byte;
    typedef std::bitset<WORD>  Word;
    typedef std::bitset<DWORD> DWord;
    typedef std::bitset<QWORD> QWord;

    template<std::uint64_t N = BYTE>
    struct Register {
        Byte register_;

        Register() {
            static_assert(
                // assert 8 <= N <= 64
                ((N%8) == 0) && (N >=8) && (N <= 64) &&
                // and N != 24,40,48 and 56
                (N != 24) && (N != 40) && (N != 48) && (N != 56)
            );
        }
    }

    template<>
    struct Register<WORD> {
        Word register_;
        Register() = default;
     };

     template<>
     struct Register<DWORD> {
         DWord register_;
         Register() = default;
     }

     template<>
     struct Register<QWORD> {
         QWord register_;
         Register() = default;
     }

} // namespace vpc

然后我用这个小程序可以将一些值写入寄存器类并将它们打印到屏幕上:

Then I use this small program to write some values to the register class and to print them to the screen:

main.cpp

#include <iostream>
#include "Register.h"

int main() {
    using namespace vpc;

    Register r1;
    r1.register_ = 169;

    std::cout << "Stored Value\n";
std::cout << r1.register_ << "\n";
// 10101001 // printed value;

std::cout << "Bit Values\n";
for (i16 i = 0; i < 8; i++) {
    std::cout << r1.register_[i] << ' ';
}
std::cout << "\n\n";
// 1 0 0 1 0 1 0 1 // printed bit values

// using std::bitset.flip()
r1.register_.flip();
std::cout << "Flipped Bit Values\n";
for (i16 i = 0; i < 8; i++) {
    std::cout << r1.register_[i] << ' ';
}
std::cout << "\n\n";
// 0 1 1 0 1 0 1 0 // printed bit values

    return EXIT_SUCCESS;
}

上面的代码没有什么问题,但是并不需要翻转我想要。很高兴知道它的存在及其作用,但是如果我们仔细查看输出:

There is nothing wrong with the code above, however the flip is not exactly want I want. It is good to know that it is there and what it does but if we closely look at the output:

输出

Stored Value
10101001

Bit Values
1 0 0 1 0 1 0 1

Flipped Bit Values
0 1 1 0 1 0 1 0

翻转功能是我不想在这里。我想反转存储的位顺序,然后将其保存回自身或创建其副本。我希望有一个带位的字节: 0111 0011 可以存储,并可以生成副本或保存回自身的值为 1100 1110 而不是 1000 1100

The flip function is not want I want here. I'd like to reverse the stored bit order and save it back into itself or to create a copy of it. I'd like to be able to have a byte with bits: 0111 0011 that is stored and be able to generate either a copy or saved back to itself a byte with value 1100 1110 and not 1000 1100.

我不没有看到属于 std :: bitset 的函数,std库中还有其他选项吗?还是我必须自己写?如果是这样,我不太确定如何在考虑到简单性和效率的情况下实现它。

I don't see any functions that belong to std::bitset that does this, are there any other options from the std library? Or would I have to write my own? If so I'm not quite sure how to implement it with simplicity and efficiency in mind.

推荐答案

我不知道一个可以做到这一点的算法,但是您可以结合使用 bitset :: to_string std :: reverse 的组合

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.

一个最小的示例。

#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;
}

请参见

输出:

10101001
10010101

此方法可以与您定义的任何其他 bitset 类型一起使用。

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

这篇关于尝试翻转std :: bitset中的位顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:48