Decoding Hex: What does this line do (len & 0x01) != 0

后端 未结 3 1644
北海茫月
北海茫月 2021-01-25 21:33

I was going through a piece of code in the Apache commons library and was wondering what these conditions do exactly.

public static byte[] decodeHex(final char[         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-25 21:57

    This checks if the last digit in the binary writing of len is a 1.

      xxxxxxxy
    & 00000001
    

    gives 1 if y is 1, 0 if y is 0, ignoring the other digits.

    If y is 1, the length of the char array is odd, which shouldn't happen in this hex writing, hence the exception.

    Another solution would have been

    if (len%2 != 0) {
    

    which would have been clearer in my opinion. I doubt the slight performance increase just before a loop really matters.

提交回复
热议问题