2016-05-13 83 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

我不知道你的预期输出中应该是什么样的。