2016-05-13 109 views
-4

爲什麼這個功能:C++的cout函數打印錯誤值

uint64_t rot_xor(uint64_t a1, uint64_t a2) { 
    int size = sizeof(a1)*4; 
    cout<<" "<<"size:"<<bitset<8>(size).to_string()<<" "<<size; 
    int shift; 
    uint64_t output = 0; 
    cout<<endl; 
    for (shift = 0; shift < size; shift++) 
     if(a1&(1<<shift)) { 
      output ^= (a2 << shift) | (a2 >> (size - shift)); 
      cout << bitset<64>(output).to_string()<<endl; 
     } 
return output; 
} 

打印輸出:


  1. :尺寸:00010000 20
  2. :0000000000000000000000000000000010110000110000011111001011111001
+4

爲什麼不呢?你期望它打印什麼,你傳遞了什麼? –

+0

你可以看到http://ideone.com/zkqMPO – AgentIvan

+0

int size = sizeof(a1); cout << endl <<「size:」<(size).to_string()<<「」<< size; size = size * 4; cout <<「」<<「size:」<(size).to_string()<<「」<< size; 打印:尺寸:00001000 8尺寸:00100000 20 – AgentIvan

回答

0

點1

如果問題是,爲什麼作爲似乎什麼結果你的程序打印20是sizeof(uint64_t) * 4,而不是32,那麼這是因爲調用rot_xor前幾行有一個:

cout << hex 

事實上,如可在你發佈的鏈接中可以看出的精確輸出是

size:00100000 20 
在二進制和十六進制表示

或32。

點2

我不知道你的預期輸出中應該是什麼樣的。