簡體   English   中英

有人能解釋一下這個 output 的邏輯嗎?

[英]Can some explain the logic for this 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];
        }
}

輸入:
995

Output:
0000001111100011 //打印字符串
1100011111000000 //使用循環打印

一個的output是反的另一個。
我不明白為什么會這樣?

cout << bt << endl;

以上根據需要打印數字

cout << bt[0] << endl;

但是,當我們索引 bitmap 時,索引從最右邊的位或 LSB 開始。

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

訂單位置從最右邊開始計算,即訂單 position 0。

它與通常的位編號方式一致, [0]表示LSB(最低有效位)。 當您將 bitset 轉換為字符串時,它將包含相反順序的位,第一個字符對應於第N-1位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM