简体   繁体   中英

Populating matrix by comparing 2 bits from 2 bitsets?

How to parse/read 2 bits at once from a bitset and process it?

I want to create a matrix that compares if 2 values match and populate the matrix with a 0 or 1.

This is what I did. This is for comparing 1 bit from both bitsets.

void bit2mat(bitset<14> ba, bitset<14> bb)
{
    // print the bitsets
    cout << "a: " << ba << endl;
    cout << "b: " << bb << endl;
    // print the matrix
    for (int i = 0; i < ba.size(); i++)
    {
        for (int j = 0; j < bb.size(); j++)
        {
            if (ba[i] == bb[j])
            {
                cout << "1 ";
            }
            else
            {
                cout << "0 ";
            }
        }
        cout << endl;
    }
    cout << endl;
}

This gives out

a: 00011100101101
b: 00110000001110
0 1 1 1 0 0 0 0 0 0 1 1 0 0
1 0 0 0 1 1 1 1 1 1 0 0 1 1
0 1 1 1 0 0 0 0 0 0 1 1 0 0
0 1 1 1 0 0 0 0 0 0 1 1 0 0
1 0 0 0 1 1 1 1 1 1 0 0 1 1
0 1 1 1 0 0 0 0 0 0 1 1 0 0
1 0 0 0 1 1 1 1 1 1 0 0 1 1
1 0 0 0 1 1 1 1 1 1 0 0 1 1
0 1 1 1 0 0 0 0 0 0 1 1 0 0
0 1 1 1 0 0 0 0 0 0 1 1 0 0
0 1 1 1 0 0 0 0 0 0 1 1 0 0
1 0 0 0 1 1 1 1 1 1 0 0 1 1
1 0 0 0 1 1 1 1 1 1 0 0 1 1
1 0 0 0 1 1 1 1 1 1 0 0 1 1

How do I make it read 2bits as 1 row and column. I want to get a matrix like this...

a: 00011100101101
b: 00110000001110

   00 01 11 00 10 11 01
00  1  0  0  1  0  0  0
11  0  0  1  0  0  0  0
00  1  0  0  1  0  0  0
00  1  0  0  1  0  0  0
00  1  0  0  1  0  0  0
11  0  0  1  0  0  1  0
10  0  0  0  0  1  0  0

I know it can be done with vectors and arrays, but I'm working with bitsets. How to do generate this matrix using bitset?

Your loops process one element at a time from each bitset. If you want to process two at a time the loop should go in steps of 2:

for (int i = 0; i < ba.size(); i+=2)

Then the two elements to be considered in the iteration are ba[i] and ba[i+1] . Same for the bb loop.

Note that the if and printing in your code can be simplified to just std::cout << (ba[i] == bb[j]); . Comparing pairs of bits and adding the headers, this is how it could be done:

#include <bitset>
#include <iostream>


void bit2mat(std::bitset<14> ba, std::bitset<14> bb) {
    // print the bitsets
    std::cout << "a: " << ba << std::endl;
    std::cout << "b: " << bb << std::endl;
    // print the matrix
    std::cout << "  \t";
    for (int i=0; i < bb.size(); i+=2) std::cout << bb[i] << bb[i+1] << " ";
    std::cout << "\n";
    for (int i = 0; i < ba.size(); i+=2)
    {
        std::cout << ba[i] << ba[i+1] << "\t";
        for (int j = 0; j < bb.size(); j+=2)
        {
            std::cout << (ba[i] == bb[j] && ba[i+1] == bb[j+1]) << "  ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

int main() {
    std::bitset<14> a{"00011100101101"};
    std::bitset<14> b{"00110000001110"};
    bit2mat(a,b);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM