XOR of two short integers

后端 未结 3 1961
醉酒成梦
醉酒成梦 2021-01-11 21:50

I am calculating XOR of two short integers using XOR ^ operator in a traditional fashion. Below is the method-

short a         


        
3条回答
  •  甜味超标
    2021-01-11 22:24

    short s1 = ...
    short s2 = ...
    short result = (short) (s1 ^ s2);
    

    This is the most efficient way to XOR two shorts together. It does not run into the overhead of creating BigIntegers and the cast will never cause an overflow issue as both s1 and s2 are shorts to begin with.

提交回复
热议问题