【问题标题】:Can some explain the logic for this output?有人能解释一下这个输出的逻辑吗?
【发布时间】:2020-08-02 20:30:07
【问题描述】:

我正在打印一个字符串(由 stl 中的 bitset 创建),然后直接打印该字符串并使用循环为什么输出有差异?

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
    const int m=16;
    int n;
    int arr[m];
        cin>>n;
        bitset<m>bt(n);
        cout<<bt<<endl;
        for(int i=0;i<m;i++)
        {
            cout<<bt[i];
        }
}

输入:
995

输出:
0000001111100011 //打印字符串
1100011111000000 //循环打印

一个的输出与另一个相反。
我不明白为什么会这样?

【问题讨论】:

标签: c++ stl c++14


【解决方案1】:
cout << bt << endl;

上面根据需要打印数字

cout << bt[0] << endl;

但是,当我们索引位图时,索引从最右边的位或 LSB 开始。

引用http://www.cplusplus.com/reference/bitset/bitset/operator[]/

订单位置从最右边开始计算,即订单位置0。

【讨论】:

    【解决方案2】:

    与通常的位编号方式一致,[0]代表LSB(Least Significant bit)。当您将 bitset 转换为字符串时,它将包含相反顺序的位,第一个字符对应于N-1th 位。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-24
      • 2022-10-13
      • 2013-07-13
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多