代码改变世界

groovy-位运算

2013-10-02 01:20  Rollen Holt  阅读(810)  评论(0编辑  收藏  举报

从Groovy 1.0 beta 10开始,Groovy支持位运算:<<. >>, >>>, |, &, ^, and ~.

下表列出了位运算的操作符和其含义:

Operator Symbol

Meaning

<<

Bitwise Left Shift Operator

>>

Bitwise Right Shift Operator

>>>

Bitwise Unsigned Right Shift Operator

|

Bitwise Or Operator

&

Bitwise And Operator

^

Bitwise Xor Operator

~

Bitwise Negation Operator

<<=

Bitwise Left Shift Assign Operator

>>=

Bitwise Right Shift Assign Operator

>>>=

Bitwise Unsigned Right Shift Assign Operator

|=

Bitwise Or Assign Operator

&=

Bitwise And Assign Operator

^=

Bitwise Xor Operator

小例子:

1 assert (1 << 2) == 4 // bitwise left shift
2 assert (4 >> 1) == 2 // bitwise right shift
3 assert (15 >>> 1) == 7 // bitwise unsigned right shift
4 assert (3 6) == 7 // bitwise or
5 assert (3 6) == 2 // bitwise and
6 assert (3 6) == 5 // bitwise xor
7 int mostlyOnes = 0xFFFFFFFE
8 assert ~mostlyOnes == 1 // bitwise negation