2016-04-27 78 views
-1

我需要简化表达需要简化逻辑表达式(字节,短整数)的Java

首先表达

(byte)((short)((short)((byte)((theInt >> 8) & 0xFF) & 0xFF) + 128) & 0xFF); 

第二个表达式

(int)((byte)(((short)(theByte & 0xFF) + 128) & 0xFF) << 8); 

我的问题是:我想,也许是不必要的操作和铸造和其他类型的操作...

什么是最简单的表达?

是需要的(theByte & 0xFF)和操作在字节类型?

回答

0

,我发现我的答案...

启动表达式1

(byte)( (short)((short)( (byte)((theInt >> 8) & 0xFF) & 0xFF) + 128) & 0xFF) 
(byte)( (short)((short) ((theInt >> 8) & 0xFF) + 128) & 0xFF) 
(byte)( (short)( (theInt >> 8) + 128) & 0xFF) 

最终的表达1

(byte)( ((theInt >> 8) + 128) & 0xFF) 

启动表达式2

(int)( (byte)(( (short)(theByte & 0xFF) + 128) & 0xFF) << 8) 
(int)( (( (short)(theByte & 0xFF) + 128) & 0xFF) << 8) 
(int)( (( (short)(theByte) + 128) & 0xFF) << 8) 

最终表达式2

(short)( ((theByte + 128) & 0xFF) << 8)