ä½é»è¾è¿ç®ç¬¦æâä¸âï¼ANDï¼ãâæâï¼ORï¼ãâå¼æï¼XORï¼âãâéï¼NOTï¼âï¼åå«ç¨â&âãâ|âãâ^âãâ~â表示ã
ä¸é¢çä¾å说æäºä½é»è¾è¿ç®ç¬¦ï¼
// Demonstrate the bitwise logical operators.
class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
};
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;
System.out.println(" a = " + binary[a]);
System.out.println(" b = " + binary[b]);
System.out.println(" a|b = " + binary[c]);
System.out.println(" a&b = " + binary[d]);
System.out.println(" a^b = " + binary[e]);
System.out.println("~a&b|a&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
}
}
å¨æ¬ä¾ä¸ï¼åéaä¸b对åºä½çç»å代表äºäºè¿å¶æ°ææç 4 ç§ç»å模å¼ï¼0-0ï¼0-1ï¼1-0ï¼å1-1ãâ|âè¿ç®ç¬¦åâ&âè¿ç®ç¬¦åå«å¯¹åéaä¸bå个对åºä½çè¿ç®å¾å°äºåécååédçå¼ã对åéeåfçèµå¼è¯´æäºâ^âè¿ç®ç¬¦çåè½ãå符串æ°ç»binary代表äº0å°15对åºçäºè¿å¶çå¼ãå¨æ¬ä¾ä¸ï¼æ°ç»åå
ç´ çæå顺åºæ¾ç¤ºäºåé对åºå¼çäºè¿å¶ä»£ç ãæ°ç»ä¹æ以è¿æ ·æé æ¯å 为åéçå¼n对åºçäºè¿å¶ä»£ç å¯ä»¥è¢«æ£ç¡®çåå¨å¨æ°ç»å¯¹åºå
ç´ binary[n]ä¸ãä¾å¦åéaçå¼ä¸º3ï¼åå®çäºè¿å¶ä»£ç 对åºå°åå¨å¨æ°ç»å
ç´ binary[3]ä¸ã~açå¼ä¸æ°å0x0f ï¼å¯¹åºäºè¿å¶ä¸º0000 1111ï¼è¿è¡æä½ä¸è¿ç®çç®çæ¯åå°~açå¼ï¼ä¿è¯åégçç»æå°äº16ãå æ¤è¯¥ç¨åºçè¿è¡ç»æå¯ä»¥ç¨æ°ç»binary对åºçå
ç´ æ¥è¡¨ç¤ºã该ç¨åºçè¾åºå¦ä¸ï¼
a = 0011
b = 0110
a|b = 0111
a&b = 0010
a^b = 0101
~a&b|a&~b = 0101
~a = 1100
左移è¿ç®ç¬¦
左移è¿ç®ç¬¦<<使æå®å¼çææä½é½å·¦ç§»è§å®ç次æ°ãå®çéç¨æ ¼å¼å¦ä¸æ示ï¼
value << num
è¿éï¼numæå®è¦ç§»ä½å¼value移å¨çä½æ°ãä¹å°±æ¯ï¼å·¦ç§»è¿ç®ç¬¦<<使æå®å¼çææä½é½å·¦ç§»numä½ãæ¯å·¦ç§»ä¸ä¸ªä½ï¼é«é¶ä½é½è¢«ç§»åºï¼å¹¶ä¸ä¸¢å¼ï¼ï¼å¹¶ç¨0å¡«å
å³è¾¹ãè¿æå³çå½å·¦ç§»çè¿ç®æ°æ¯intç±»åæ¶ï¼æ¯ç§»å¨1ä½å®ç第31ä½å°±è¦è¢«ç§»åºå¹¶ä¸ä¸¢å¼ï¼å½å·¦ç§»çè¿ç®æ°æ¯longç±»åæ¶ï¼æ¯ç§»å¨1ä½å®ç第63ä½å°±è¦è¢«ç§»åºå¹¶ä¸ä¸¢å¼ã
å¨å¯¹byteåshortç±»åçå¼è¿è¡ç§»ä½è¿ç®æ¶ï¼ä½ å¿
é¡»å°å¿ãå ä¸ºä½ ç¥éJavaå¨å¯¹è¡¨è¾¾å¼æ±å¼æ¶ï¼å°èªå¨æè¿äºç±»åæ©å¤§ä¸º intåï¼èä¸ï¼è¡¨è¾¾å¼çå¼ä¹æ¯intå ã对byteåshortç±»åçå¼è¿è¡ç§»ä½è¿ç®çç»ææ¯intåï¼èä¸å¦æ左移ä¸è¶
è¿31ä½ï¼åæ¥å¯¹åºåä½çå¼ä¹ä¸ä¼ä¸¢å¼ãä½æ¯ï¼å¦æä½ å¯¹ä¸ä¸ªè´çbyteæè
shortç±»åçå¼è¿è¡ç§»ä½è¿ç®ï¼å®è¢«æ©å¤§ä¸ºintååï¼å®ç符å·ä¹è¢«æ©å±ãè¿æ ·ï¼æ´æ°å¼ç»æçé«ä½å°±ä¼è¢«1å¡«å
ãå æ¤ï¼ä¸ºäºå¾å°æ£ç¡®çç»æï¼ä½ å°±è¦èå¼å¾å°ç»æçé«ä½ãè¿æ ·åçæç®ååæ³æ¯å°ç»æ转æ¢ä¸ºbyteåãä¸é¢çç¨åºè¯´æäºè¿ä¸ç¹ï¼
// Left shifting a byte value.
class ByteShift {
public static void main(String args[]) {
byte a = 64, b;
int i;
i = a << 2;
b = (byte) (a << 2);
System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);
}
}
该ç¨åºäº§ççè¾åºä¸æ示ï¼
Original value of a: 64
i and b: 256 0
å åéaå¨èµå¼è¡¨è¾¾å¼ä¸ï¼æ
被æ©å¤§ä¸ºintåï¼64ï¼0100 0000ï¼è¢«å·¦ç§»ä¸¤æ¬¡çæå¼256ï¼10000 0000ï¼è¢«èµç»åéiãç¶èï¼ç»è¿å·¦ç§»åï¼åébä¸æä¸ç1被移åºï¼ä½ä½å
¨é¨æäº0ï¼å æ¤bçå¼ä¹åæäº0ã
æ¢ç¶æ¯æ¬¡å·¦ç§»é½å¯ä»¥ä½¿åæ¥çæä½æ°ç¿»åï¼ç¨åºå们ç»å¸¸ä½¿ç¨è¿ä¸ªåæ³æ¥è¿è¡å¿«éç2çä¹æ³ãä½æ¯ä½ è¦å°å¿ï¼å¦æä½ å°1移è¿é«é¶ä½ï¼31æ63ä½ï¼ï¼é£ä¹è¯¥å¼å°å为è´å¼ãä¸é¢çç¨åºè¯´æäºè¿ä¸ç¹ï¼
// Left shifting as a quick way to multiply by 2.
class MultByTwo {
public static void main(String args[]) {
int i;
int num = 0xFFFFFFE;
for(i=0; i<4; i++) {
num = num << 1;
System.out.println(num);
}
}
}
该ç¨åºçè¾åºå¦ä¸æ示ï¼
536870908
1073741816
2147483632
-32
åå¼ç»è¿ä»ç»éæ©ï¼ä»¥ä¾¿å¨å·¦ç§» 4 ä½åï¼å®ä¼äº§ç-32ãæ£å¦ä½ çå°çï¼å½1被移è¿31ä½æ¶ï¼æ°å被解é为è´å¼ã
å³ç§»è¿ç®ç¬¦
å³ç§»è¿ç®ç¬¦>>使æå®å¼çææä½é½å³ç§»è§å®ç次æ°ãå®çéç¨æ ¼å¼å¦ä¸æ示ï¼
value >> num
è¿éï¼numæå®è¦ç§»ä½å¼value移å¨çä½æ°ãä¹å°±æ¯ï¼å³ç§»è¿ç®ç¬¦>>使æå®å¼çææä½é½å³ç§»numä½ã
ä¸é¢çç¨åºç段å°å¼32å³ç§»2次ï¼å°ç»æ8èµç»åéa:
int a = 32;
a = a >> 2; // a now contains 8
å½å¼ä¸çæäºä½è¢«â移åºâæ¶ï¼è¿äºä½çå¼å°ä¸¢å¼ãä¾å¦ï¼ä¸é¢çç¨åºç段å°35å³ç§»2次ï¼å®ç2个ä½ä½è¢«ç§»åºä¸¢å¼ï¼ä¹å°ç»æ8èµç»åéa:
int a = 35;
a = a >> 2; // a still contains 8
ç¨äºè¿å¶è¡¨ç¤ºè¯¥è¿ç¨å¯ä»¥æ´æ¸
æ¥å°çå°ç¨åºçè¿è¡è¿ç¨ï¼
00100011 35
>> 2
00001000 8
å°å¼æ¯å³ç§»ä¸æ¬¡ï¼å°±ç¸å½äºå°è¯¥å¼é¤ä»¥2并ä¸èå¼äºä½æ°ãä½ å¯ä»¥å©ç¨è¿ä¸ªç¹ç¹å°ä¸ä¸ªæ´æ°è¿è¡å¿«éç2çé¤æ³ãå½ç¶ï¼ä½ ä¸å®è¦ç¡®ä¿ä½ ä¸ä¼å°è¯¥æ°åæçä»»ä½ä¸ä½ç§»åºã
å³ç§»æ¶ï¼è¢«ç§»èµ°çæé«ä½ï¼æ左边çä½ï¼ç±åæ¥æé«ä½çæ°åè¡¥å
ãä¾å¦ï¼å¦æè¦ç§»èµ°çå¼ä¸ºè´æ°ï¼æ¯ä¸æ¬¡å³ç§»é½å¨å·¦è¾¹è¡¥1ï¼å¦æè¦ç§»èµ°çå¼ä¸ºæ£æ°ï¼æ¯ä¸æ¬¡å³ç§»é½å¨å·¦è¾¹è¡¥0ï¼è¿å«å符å·ä½æ©å±ï¼ä¿ç符å·ä½ï¼ï¼sign extensionï¼ï¼å¨è¿è¡å³ç§»æä½æ¶ç¨æ¥ä¿æè´æ°ç符å·ãä¾å¦ï¼â8 >> 1 æ¯â4ï¼ç¨äºè¿å¶è¡¨ç¤ºå¦ä¸ï¼
11111000 â8
>>1
11111100 â4
ä¸ä¸ªè¦æ³¨æçæ趣é®é¢æ¯ï¼ç±äºç¬¦å·ä½æ©å±ï¼ä¿ç符å·ä½ï¼æ¯æ¬¡é½ä¼å¨é«ä½è¡¥1ï¼å æ¤-1å³ç§»çç»ææ»æ¯â1ãææ¶ä½ ä¸å¸æå¨å³ç§»æ¶ä¿ç符å·ãä¾å¦ï¼ä¸é¢çä¾åå°ä¸ä¸ªbyteåçå¼è½¬æ¢ä¸ºç¨åå
è¿å¶è¡¨ç¤ºã注æå³ç§»åçå¼ä¸0x0fè¿è¡æä½ä¸è¿ç®ï¼è¿æ ·å¯ä»¥èå¼ä»»ä½ç符å·ä½æ©å±ï¼ä»¥ä¾¿å¾å°çå¼å¯ä»¥ä½ä¸ºå®ä¹æ°ç»çä¸æ ï¼ä»èå¾å°å¯¹åºæ°ç»å
ç´ ä»£è¡¨çåå
è¿å¶å符ã
// Masking sign extension.
class HexByte {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f''
};
byte b = (byte) 0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
}
}
该ç¨åºçè¾åºå¦ä¸ï¼
b = 0xf1
æ 符å·å³ç§»
æ£å¦ä¸é¢ååçå°çï¼æ¯ä¸æ¬¡å³ç§»ï¼>>è¿ç®ç¬¦æ»æ¯èªå¨å°ç¨å®çå
åæé«ä½çå
容补å®çæé«ä½ãè¿æ ·åä¿çäºåå¼ç符å·ãä½ææ¶è¿å¹¶ä¸æ¯æ们æ³è¦çãä¾å¦ï¼å¦æä½ è¿è¡ç§»ä½æä½çè¿ç®æ°ä¸æ¯æ°åå¼ï¼ä½ å°±ä¸å¸æè¿è¡ç¬¦å·ä½æ©å±ï¼ä¿ç符å·ä½ï¼ãå½ä½ å¤çåç´ å¼æå¾å½¢æ¶ï¼è¿ç§æ
åµæ¯ç¸å½æ®éçãå¨è¿ç§æ
åµä¸ï¼ä¸ç®¡è¿ç®æ°çåå¼æ¯ä»ä¹ï¼ä½ å¸æ移ä½åæ»æ¯å¨é«ä½ï¼æ左边ï¼è¡¥0ãè¿å°±æ¯äººä»¬æ说çæ 符å·ç§»å¨ï¼unsigned shiftï¼ãè¿æ¶ä½ å¯ä»¥ä½¿ç¨Javaçæ 符å·å³ç§»è¿ç®ç¬¦>>>ï¼å®æ»æ¯å¨å·¦è¾¹è¡¥0ãä¸é¢çç¨åºæ®µè¯´æäºæ 符å·å³ç§»è¿ç®ç¬¦>>>ãå¨æ¬ä¾ä¸ï¼åéa被èµå¼ä¸º-1ï¼ç¨äºè¿å¶è¡¨ç¤ºå°±æ¯32ä½å
¨æ¯1ãè¿ä¸ªå¼ç¶å被æ 符å·å³ç§»24ä½ï¼å½ç¶å®å¿½ç¥äºç¬¦å·ä½æ©å±ï¼å¨å®ç左边æ»æ¯è¡¥0ãè¿æ ·å¾å°çå¼255被èµç»åéaã
int a = -1;
a = a >>> 24;
ä¸é¢ç¨äºè¿å¶å½¢å¼è¿ä¸æ¥è¯´æ该æä½ï¼
11111111 11111111 11111111 11111111 intå- 1çäºè¿å¶ä»£ç
>>> 24 æ 符å·å³ç§»24ä½
00000000 00000000 00000000 11111111 intå255çäºè¿å¶ä»£ç ç±äºæ 符å·å³ç§»è¿ç®ç¬¦>>>åªæ¯å¯¹32ä½å64ä½çå¼ææä¹ï¼æ以å®å¹¶ä¸åä½ æ³è±¡çé£æ ·æç¨ãå ä¸ºä½ è¦è®°ä½ï¼å¨è¡¨è¾¾å¼ä¸è¿å°çå¼æ»æ¯è¢«èªå¨æ©å¤§ä¸ºintåãè¿æå³ç符å·ä½æ©å±å移å¨æ»æ¯åçå¨32ä½èä¸æ¯8ä½æ16ä½ãè¿æ ·ï¼å¯¹ç¬¬7ä½ä»¥0å¼å§çbyteåçå¼è¿è¡æ 符å·ç§»å¨æ¯ä¸å¯è½çï¼å 为å¨å®é
移å¨è¿ç®æ¶ï¼æ¯å¯¹æ©å¤§åç32ä½å¼è¿è¡æä½ãä¸é¢çä¾å说æäºè¿ä¸ç¹ï¼
// Unsigned shifting a byte value.
class ByteUShift {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
byte c = (byte) (b >> 4);
byte d = (byte) (b >>> 4);
byte e = (byte) ((b & 0xff) >> 4);
System.out.println(" b = 0x"
+ hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
System.out.println(" b >> 4 = 0x"
+ hex[(c >> 4) & 0x0f] + hex[c & 0x0f]);
System.out.println(" b >>> 4 = 0x"
+ hex[(d >> 4) & 0x0f] + hex[d & 0x0f]);
System.out.println("( b & 0xff) >> 4 = 0x"
+ hex[(e >> 4) & 0x0f] + hex[e & 0x0f]);
}
}
该ç¨åºçè¾åºæ¾ç¤ºäºæ 符å·å³ç§»è¿ç®ç¬¦>>>对byteåå¼å¤çæ¶ï¼å®é
ä¸ä¸æ¯å¯¹byteåå¼ç´æ¥æä½ï¼èæ¯å°å
¶æ©å¤§å°intåååå¤çãå¨æ¬ä¾ä¸åéb被èµä¸ºä»»æçè´byteåå¼ã对åébå³ç§»4ä½å转æ¢ä¸ºbyteåï¼å°å¾å°çå¼èµç»åécï¼å 为æ符å·ä½æ©å±ï¼æ以该å¼ä¸º0xffã对åébè¿è¡æ 符å·å³ç§»4ä½æä½å转æ¢ä¸ºbyteåï¼å°å¾å°çå¼èµç»åédï¼ä½ å¯è½ææ该å¼æ¯0x0fï¼ä½å®é
ä¸å®æ¯0xffï¼å 为å¨ç§»å¨ä¹ååéb就被æ©å±ä¸ºintåï¼å·²ç»æ符å·æ©å±ä½ãæåä¸ä¸ªè¡¨è¾¾å¼å°åébçå¼éè¿æä½ä¸è¿ç®å°å
¶å为8ä½ï¼ç¶åå³ç§»4ä½ï¼ç¶åå°å¾å°çå¼èµç»åéeï¼è¿æ¬¡å¾å°äºé¢æ³çç»æ0x0fãç±äºå¯¹åédï¼å®çå¼å·²ç»æ¯0xffï¼è¿è¡æä½ä¸è¿ç®åç符å·ä½çç¶æå·²ç»æäºï¼æ以注æï¼å¯¹åédå没æè¿è¡æ 符å·å³ç§»è¿ç®ã
B = 0xf1
b >> 4 = 0xff
b >>> 4 = 0xff
(b & 0xff) >> 4 = 0x0f
ä½è¿ç®ç¬¦èµå¼
ææçäºè¿å¶ä½è¿ç®ç¬¦é½æä¸ç§å°èµå¼ä¸ä½è¿ç®ç»åå¨ä¸èµ·çç®åå½¢å¼ãä¾å¦ï¼ä¸é¢ä¸¤ä¸ªè¯å¥é½æ¯å°åéaå³ç§»4ä½åèµç»aï¼
a = a >> 4;
a >>= 4;
åæ ·ï¼ä¸é¢ä¸¤ä¸ªè¯å¥é½æ¯å°è¡¨è¾¾å¼a OR bè¿ç®åçç»æèµç»aï¼
a = a | b;
a |= b;
ä¸é¢çç¨åºå®ä¹äºå 个intååéï¼ç¶åè¿ç¨ä½èµå¼ç®åçå½¢å¼å°è¿ç®åçå¼èµç»ç¸åºçåéï¼
class OpBitEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
该ç¨åºçè¾åºå¦ä¸æ示ï¼
a = 3
b = 1
c = 6
温馨提示:内容为网友见解,仅供参考