2016-05-13 11 views
-4

Warum diese Funktion:C++ falscher Wert Druck cout Funktion

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

Druckausgabe:


  1. : size: 00010000 20
  2. : 0000000000000000000000000000000010110000110000011111001011111001
+4

Warum sollte es nicht? Was haben Sie erwartet, dass es gedruckt wird und was führen Sie dazu? –

+0

können Sie dort sehen http://ideone.com/zkqMPO – AgentIvan

+0

int size = sizeof (a1); cout << endl << "size:" < (Größe) .to_string() << "" << size; Größe = Größe * 4; cout << "" << "size:" < (Größe) .to_string() << "" << Größe; Drucken: Größe: 00001000 8 Größe: 00100000 20 – AgentIvan

Antwort

0

Punkt 1

Wenn die Frage ist, warum 20 Ihr Programm druckt als Ergebnis dessen, was sizeof(uint64_t) * 4 statt 32 zu sein scheint, und es ist, weil ein paar Zeilen vor rot_xor Aufruf eine ist:

cout << hex 

in der Tat, die genaue Ausgabe wie Sie auf dem Laufenden in den Link zu sehen ist

size:00100000 20 

oder 32 in binäre und hexadezimale Darstellung.

Punkt 2

Ich habe keine Ahnung, was Ihr erwartet ouput sein sollte.