java中的位元運算符和運算子優先順序1 位元運算符
一共3個移位元運算符,左移位<<,右移位>>和無符號移位>>>。左移位<<在低位處補0。右移位>>若值為正則在高位插入0,若值為負則在高位插入1。無符號右移位>>>無論正負都在高位處插入0。
非運算子~
&對兩個整型運算元中對應位執行布爾代數,兩個位都為1時輸出1,否則0。
^對兩個整型運算元中對應位執行布爾代數,兩個位相等0,不等1。
|對兩個整型運算元中對應位執行布爾代數,兩個位都為0時輸出0,否則1。
如:
以下哪種運算正確:
A. 1010 0000 0000 0000 0000 0000 0000 0000 >> 4
gives 0000 1010 0000 0000 0000 0000 0000 0000
B. 1010 0000 0000 0000 0000 0000 0000 0000 >> 4
gives 1111 1010 0000 0000 0000 0000 0000 0000
C. 1010 0000 0000 0000 0000 0000 0000 0000 >>> 4
gives 0000 1010 0000 0000 0000 0000 0000 0000
D. 1010 0000 0000 0000 0000 0000 0000 0000 >>> 4
gives 1111 1010 0000 0000 0000 0000 0000 0000
選:B C
以下哪一運算正確:
A. 0000 0100 0000 0000 0000 0000 0000 0000 << 5
gives 1000 0000 0000 0000 0000 0000 0000 0000
B. 0000 0100 0000 0000 0000 0000 0000 0000 << 5
gives 1111 1100 0000 0000 0000 0000 0000 0000
C. 1100 0000 0000 0000 0000 0000 0000 0000 >> 5
gives 1111 1110 0000 0000 0000 0000 0000 0000
D. 1100 0000 0000 0000 0000 0000 0000 0000 >> 5
gives 0000 0110 0000 0000 0000 0000 0000 0000
選:A C
Given:
1.Public class test (
2. Public static void main (String args[]) (
3. System.out.printIn (6 ^ 3);
4. )
5.)
What is the output
Ans: 5
2 位元運算符返回的是數值,不是boolean類型值
如:
if(5&7>0 && 5|2)
System.out.println("true");
顯示:編譯出錯
3 對於一個整型數進行移位,其右運算元應該小於32,對於一個長整型數進行移位,其右運算元應該小於64。如果右運算元大於了規定的位元,編譯不會報錯,取右運算元的模來進行移位操作。
如:
class Test
{
public static void main(String args[])
{
int x= 16384;
System.out.println(x>>33);
}
}
顯示:8192
4 注意進行位操作時,會自動轉成int型,轉換之後,可接受右運算元長度為32。進行位元運算時,總是先將短整型和位元組型值轉換成整型值再進行移位操作的。
如:
class Test
{
public static void main(String args[])
{
byte x = 127;
byte y = (byte)(x>>9);
System.out.println(y);
}
}
顯示:0
如:
char c = 'l';
System.out.println(c>>1);
編譯通過,把字元值轉成了int值
5 boolean類型值true對應位值1,false對應位值0,返回仍然是boolean類型。&,|,^,都可以用於布爾值,但是~不能用於布爾值。
布爾類型true,false可以比較,但只有==能使用,<,<=...會產生編譯錯誤。除此外,布爾類型不能和其他類型數值比較。
如:
class Test
{
public static void main(String args[])
{
boolean x = true;
boolean y = false;
System.out.println((x&y) + " " + (x&x));
System.out.println((x^y) + " " + (y^y));
System.out.println((x|y) + " " + (y|y));
}
}
顯示:false ture true false false true true false
6 位元運算要求運算元為整數,運算元不能是字串也不能是小數。
如:
String s = "Hello";
long l = 99;
double d = 1.11;
int i = 1;
int j = 0;
a:j = i<<s;
b: j = i<<j;
c: j = i<<d;
d: j = i<<l;
正確:b,d
如:
char c = 'l';
System.out.println(c>>1);
編譯通過,把字元值轉成了int值
Integer i = Integer("1");
System.out.println(i>>1);
編譯無法通過
7 由於位元運算是二進位運算,不可與一些八位元搞混,java中二進位無標記法。
如:
System.out.println(010|4);
顯示:12
並且當位元運算中遇見負數,必須把它轉成補碼再進行計算。而不是使用原碼。
如:
class Test
{
public static void main(String args[])
{
try
{
int x = -7;
System.out.println(x/2 + " " + (x >> 1));
}
catch(Exception e)
{
System.out.pritnln("Exception");
}
}
}
a A compiler error
b Exception
c -3 -3
d -3 -4
e -4 -4
f None of the above
選:f
-7>>1:-7補碼:11111111 11111111 11111111 11111001
隨後操作得: 11111111 11111111 11111111 11111100
關於-7/2,其中仍與正數除法相同,為-3。
如:
int a = -1;
int b = -1;
a = a >>> 31;
b = b >> 31;
顯示:a=1,b=-1
其中b處,高位插入1,是插入31個,是一32個1,這是一個補碼,隨後轉成十進位為-1。
稍複雜的如:
int i = 1;
i <<= 31;
i >>= 31;
i >>= 1;
int j = 1;
j <<= 31;
j >>= 31;
System.out.println("i=" + i);
System.out.println("j=" + j);
顯示:i=-1 j=-1
i<<=31 : 1000 0000 0000 0000 0000 0000 0000 0000 //這裡已經被當作負數了
i>>=31 : 1111 1111 1111 1111 1111 1111 1111 1111
i>>=1 : 1111 1111 1111 1111 1111 1111 1111 1111
8 對於~操作,可以根據電腦中二進位正負數之間的關係,採取取負減一法,即~i = (-i)-1.
如:
int i = 45678;
int j = ~i;
System.out.println(j);
結果:(-45678)-1 = -45679
如:
class Test
{
public static void main(String args[])
{
int x = 1;
int y = ~x+1;
System.out.println(x+" "+y);
}
}
顯示:1 -1
9 運算子優先順序和運算順序:運算順序從左至右,賦值順序從右至左。
class Test
{
public static void main(String args[])
{
int [] refToArray = { 10, 11};
int var = 1;
refToArray[var-1] = var = 2;
System.out.println(refToArray[0] + " " + refToArray[1]);
}
}
如上輸出為2和11,首先計算數組下標,隨後賦值。
運算子== 的優先順序高於賦值運算子=的優先順序。
如:
public class Test
{
public static void main(String args [])
{
int i = 10;
int j = 10;
boolean b = false;
if(b=i==j)
System.out.println("True");
else
System.out.println("False");
}
}
位元運算符和比較符同層級,自左向右運算即可。
如:
if(5&7>0 && 5|2)
System.out.println("true")
顯示:編譯出錯
其中5&7最先計算,隨後計算5&7結果是否大於0。