如何使用 bitset < 计算位转换>

发布于 2024-12-12 16:27:41 字数 973 浏览 0 评论 0 原文

我是 C++ 新手。我想计算 9 位序列中从 0 到 0、0 到 1、1 到 0 和 1 到 1 的转换次数。我写了以下代码;

int main { 
srand((unsigned)time(0));
unsigned int x;
for (int i=0:i<=512;i++)  //    loop-1
{
x=rand()%512;
bitset<9>bitseq(x);
    for(int j=0;j<=bitseq.size();j++)  // loop-2
    {
    bool a= bitseq.test(j);
    bool b= bitseq.test(j+1)
    if ((a==0)&(b==0)==0)
    {
    transition0_0 = transition0_0 + 1; //  transition from 0 to 0
    }
    else if ((a==0)&(b==1)==0)
    {
    transition0_1 = transition0_1 + 1;
    else if ((a==1)&(b==0)==0)
    {
    transition1_0 = transition1_0 + 1;
    else
     {
     transition1_1 = transition1_1 + 1;

      cout<<transition0_0<<"    "<<transition0_1<<endl; 
      cout<<transition1_0<<"    "<<transition1_1<<endl;
     }
 }

有人请指导我以下

  1. 如何保存循环2中的最后一位值以检查从最后一个位集输出的最后一位到下一个位集输出的第一个位的转换?
  2. 如果这不起作用,我如何将其保存在向量中并使用迭代器来检查转换?

I am new to C++. I want to calculate the no of transitions from 0 to 0, 0 to 1, 1 to 0 and 1 to 1 in a 9 bit sequence. I have written the following code;

int main { 
srand((unsigned)time(0));
unsigned int x;
for (int i=0:i<=512;i++)  //    loop-1
{
x=rand()%512;
bitset<9>bitseq(x);
    for(int j=0;j<=bitseq.size();j++)  // loop-2
    {
    bool a= bitseq.test(j);
    bool b= bitseq.test(j+1)
    if ((a==0)&(b==0)==0)
    {
    transition0_0 = transition0_0 + 1; //  transition from 0 to 0
    }
    else if ((a==0)&(b==1)==0)
    {
    transition0_1 = transition0_1 + 1;
    else if ((a==1)&(b==0)==0)
    {
    transition1_0 = transition1_0 + 1;
    else
     {
     transition1_1 = transition1_1 + 1;

      cout<<transition0_0<<"    "<<transition0_1<<endl; 
      cout<<transition1_0<<"    "<<transition1_1<<endl;
     }
 }

Somebody please guide me on the following

  1. how to save the last bit value in loop-2 to check the transition from last bit of the last bitset output to the 1st bit of the next bitset output?
  2. If this does not work, How I can save it in vector and use iterators to check the transitions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深缘浅 2024-12-19 16:27:41

首先,循环索引 j 运行到bitset 的末尾。索引从 0 到 bitseq.size()-1 (含)。如果您要测试 jj+1,则 j 可以采用的最大值是 bitseq.size()-2

其次,出现在 if 中的 ==0 部分很奇怪,您应该只使用

if( (a==0)&&(b==0) )

注意使用两个 && 。虽然单个 & 适用于此代码,但我认为最好使用正确传达您意图的运算符。

然后为了回答你的问题,你可以保留一个最初设置为哨兵值的“最后一位”变量(表明你现在看到的是第一个bitseq),并在循环2开始之前将其与bitseq[0]进行比较这是您的代码的修改版本,应该可以满足您的要求。

int main { 
  srand((unsigned)time(0));
  unsigned int x;
  int transition0_0 = 0,
      transition0_1 = 0,
      transition1_0 = 0,
      transition1_1 = 0;
  int prev = -1;

  for (int i=0:i<=512;i++)  //    loop-1
  {
    x=rand()%512;
    bitset<9> bitseq(x);

    if( prev != -1 ) // don't check this on the first iteration
    {
      bool cur = bitseq.test(0);
      if( !prev && !cur )
        ++transition0_0;
      else if( !prev && cur )
        ++transition0_1;
      else if( prev && !cur )
        ++transition1_0;
      else
        ++transition1_1;
    }

    for(int j=0;j+1<bitseq.size();j++)  // loop-2
    {
      bool a= bitseq.test(j);
      bool b= bitseq.test(j+1)
      if ((a==0)&&(b==0))
      {
        transition0_0 = transition0_0 + 1; //  transition from 0 to 0
      }
      else if ((a==0)&&(b==1))
      {
        transition0_1 = transition0_1 + 1;
      }
      else if ((a==1)&&(b==0))
      {
        transition1_0 = transition1_0 + 1;
      }
      else
      {
        ++transition1_1 = transition1_1 + 1;
      }
    } // for-2

    prev = bitseq.test(bitseq.size()-1); // update prev for the next iteration

    cout<<transition0_0<<"    "<<transition0_1<<endl; 
    cout<<transition1_0<<"    "<<transition1_1<<endl;
  } // for-1
} // main

First of all, the loop index j is running past the end of the bitset. Indices go from 0 to bitseq.size()-1 (inclusive). If you're going to test j and j+1 the largest value j can take is bitseq.size()-2.

Second, the ==0 part that appears in your ifs is strange, you should just use

if( (a==0)&&(b==0) )

Notice the use of two &&. While a single & works for this code, I think it's better to use the operator that correctly conveys your intentions.

And then to answer your question, you can keep a "last bit" variable that is initially set to a sentinel value (indicating you're seeing the first bitseq just now) and compare it to bitseq[0] before the start of loop 2. Here's a modified version of your code that should do what you ask.

int main { 
  srand((unsigned)time(0));
  unsigned int x;
  int transition0_0 = 0,
      transition0_1 = 0,
      transition1_0 = 0,
      transition1_1 = 0;
  int prev = -1;

  for (int i=0:i<=512;i++)  //    loop-1
  {
    x=rand()%512;
    bitset<9> bitseq(x);

    if( prev != -1 ) // don't check this on the first iteration
    {
      bool cur = bitseq.test(0);
      if( !prev && !cur )
        ++transition0_0;
      else if( !prev && cur )
        ++transition0_1;
      else if( prev && !cur )
        ++transition1_0;
      else
        ++transition1_1;
    }

    for(int j=0;j+1<bitseq.size();j++)  // loop-2
    {
      bool a= bitseq.test(j);
      bool b= bitseq.test(j+1)
      if ((a==0)&&(b==0))
      {
        transition0_0 = transition0_0 + 1; //  transition from 0 to 0
      }
      else if ((a==0)&&(b==1))
      {
        transition0_1 = transition0_1 + 1;
      }
      else if ((a==1)&&(b==0))
      {
        transition1_0 = transition1_0 + 1;
      }
      else
      {
        ++transition1_1 = transition1_1 + 1;
      }
    } // for-2

    prev = bitseq.test(bitseq.size()-1); // update prev for the next iteration

    cout<<transition0_0<<"    "<<transition0_1<<endl; 
    cout<<transition1_0<<"    "<<transition1_1<<endl;
  } // for-1
} // main
青萝楚歌 2024-12-19 16:27:41

这样的事情对你来说会更好吗?使用 4 个整数的数组,其中 [0] = 0->0、[1] = 0->1、[2] = 1->0、[3] = 1->1。

int main { 
   int nTransition[] = { 0,0,0,0 };
   bool a,b;
   unsigned int x;
   int j;

   srand ((unsigned)time(0));

   for (int i = 0: i < 512; i++) {

       x = rand () % 512;
       bitset<9> bitseq(x);

       if (i == 0) {
          a = bitseq.test (0);
          j = 1;
       } else
          j = 0;

       for (; j < bitseq.size (); j++) {

           b = bitseq.test(j);

           int nPos = (a) ? ((b) ? 3 : 2) : ((b) ? 1 : 0);
           nTransition[nPos]++;

           a = b;
       }
    }
 }

Would something like this be better for you? Use an array of 4 ints where [0] = 0->0, [1] = 0->1, [2] = 1->0, [3] = 1->1.

int main { 
   int nTransition[] = { 0,0,0,0 };
   bool a,b;
   unsigned int x;
   int j;

   srand ((unsigned)time(0));

   for (int i = 0: i < 512; i++) {

       x = rand () % 512;
       bitset<9> bitseq(x);

       if (i == 0) {
          a = bitseq.test (0);
          j = 1;
       } else
          j = 0;

       for (; j < bitseq.size (); j++) {

           b = bitseq.test(j);

           int nPos = (a) ? ((b) ? 3 : 2) : ((b) ? 1 : 0);
           nTransition[nPos]++;

           a = b;
       }
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文