2014-01-25 26 views
3

工作我如何解釋這個代碼類Java卡初學者的一種方式,他們將完全理解它:如何二元運算符的說明「和」 Java智能卡

private void getBalance(APDU apdu) { 
    byte[] buffer = apdu.getBuffer(); 

    // inform system that the applet has finished 
    // processing the command and the system should 
    // now prepare to construct a response APDU 
    // which contains data field 
    short le = apdu.setOutgoing(); 

    if (le < 2) { 
     ISOException.throwIt((byte) 0x6A86); 
    } 

    // informs the CAD the actual number of bytes 
    // returned 
    apdu.setOutgoingLength((byte) 2); 

    // move the balance data into the APDU buffer 
    // starting at the offset 0 
    buffer[0] = (byte) (balance >> 8); 

    buffer[1] = (byte) (balance & 0xFF);//How do i explain what happens here 



    // send the 2-byte balance at the offset 
    // 0 in the apdu buffer 
    apdu.sendBytes((short) 0, (short) 2); 
} 
+5

我不認爲你甚至需要'&0xFF'在這裏。 – user2357112

+0

@ user2357112:確實不是。 –

+0

'(byte)0x6A86' - 到底是什麼?這看起來很像一個錯誤。幻數的一半就被扔掉了。 – user2357112

回答

1

解釋整數的二進制表示。 解釋>>移位運算符。 解釋十六進制符號,以及0xFF變成什麼。 解釋按位和/或操作符。 把它放在一起,以顯示>> 8和& 0xFF的組合如何將16位值分解爲高字節和低字節。 詢問是否有任何問題。