简体   繁体   English

有人能解释一下这个 output 的逻辑吗?

[英]Can some explain the logic for this output?

I am printing a string (created by bitset in stl) and then printing the string directly and by using loop why there is difference in the output?我正在打印一个字符串(由 stl 中的 bitset 创建),然后直接打印字符串并使用循环为什么 output 有区别?

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

Input:输入:
995 995

Output: Output:
0000001111100011 //Printing string 0000001111100011 //打印字符串
1100011111000000 //Printing using loop 1100011111000000 //使用循环打印

The output of one is reverse the other.一个的output是反的另一个。
I do not understand why this is happening?我不明白为什么会这样?

cout << bt << endl;

The above prints the number as desired以上根据需要打印数字

cout << bt[0] << endl;

however, when we index a bitmap, the indexing starts from the rightmost bit, or the LSB.但是,当我们索引 bitmap 时,索引从最右边的位或 LSB 开始。

As quoted on http://www.cplusplus.com/reference/bitset/bitset/operator[]/http://www.cplusplus.com/reference/bitset/bitset/operator[]/

Order positions are counted from the rightmost bit, which is order position 0.订单位置从最右边开始计算,即订单 position 0。

It is consistent with the way bits are usually numbered, [0] represents the LSB(least significant bit).它与通常的位编号方式一致, [0]表示LSB(最低有效位)。 When you convert bitset to string — it will contain bits the opposite order, with first character corresponding to N-1 th bit.当您将 bitset 转换为字符串时,它将包含相反顺序的位,第一个字符对应于第N-1位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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