javascriptå
·æä¸åç§ç±»çè¿ç®ç¬¦ï¼ç®æ¯è¿ç®ç¬¦;çåè¿ç®ç¬¦ä¸å
¨åè¿ç®ç¬¦;æ¯è¾è¿ç®ç¬¦; 2ãç®çåç±»ï¼å符串è¿ç®ç¬¦;é»è¾è¿ç®ç¬¦;éä½è¿
1ãjavascriptå
·æä¸åç§ç±»çè¿ç®ç¬¦ï¼ç®æ¯è¿ç®ç¬¦;çåè¿ç®ç¬¦ä¸å
¨åè¿ç®ç¬¦;æ¯è¾è¿ç®ç¬¦;2ãç®çåç±»ï¼å符串è¿ç®ç¬¦;é»è¾è¿ç®ç¬¦;éä½è¿ç®ç¬¦;èµå¼è¿ç®ç¬¦;3ãç¹æ®è¿ç®ç¬¦ï¼æ¡ä»¶è¿ç®ç¬¦;typeofè¿ç®ç¬¦;å建对象è¿ç®ç¬¦new;deleteè¿ç®ç¬¦;voidè¿ç®ç¬¦å·;éå·è¿ç®ç¬¦;ç®æ¯è¿ç®ç¬¦ï¼+ ã - ã* ã / ã % ã -(ä¸å
åå) ã ++ ãçåè¿ç®ç¬¦ä¸å
¨åè¿ç®ç¬¦ï¼== ã ===ã !== ã !===æ¯è¾è¿ç®ç¬¦ï¼< ã > ã <= ã >=å符串è¿ç®ç¬¦ï¼ï¼< ã > ã <= ã >= ã = ã +é»è¾è¿ç®ç¬¦ï¼&& ã ã !ãèµå¼è¿ç®ç¬¦ï¼=ã +=ã *=ã -=ã /=è§åï¼ä¸ãä¸å®è¦æ³¨æä¼ éç»è¿ç®ç¬¦çæ°æ®ç±»ååè¿åçæ°æ®ç±»å!ä¸åçè¿ç®ç¬¦é½ææå®çè¿ç®æ°è¡¨è¾¾å¼è®¡ç®åºçç»æ符åæç§æ°æ®ç±»åãä¾å¦ï¼å¯¹å符串æ¯ä¸è½è¿è¡ä¹æ³è¿ç®çï¼"a" * "b" æ¯ä¸åæ³çï¼ä½æ¯ï¼å¨å¯è½çæ
åµä¸ï¼javascriptä¼æ表达å¼è½¬æ¢ææ£ç¡®çç±»åï¼å æ¤ï¼è¡¨è¾¾å¼ "3" * "5" æ¯åæ³çï¼javascriptå°å符串转æ¢ææ°åæ§è¡è¿ç®ï¼ç»æè¿åçæ¯æ°å15ï¼èä¸æ¯å符串"15"ãäºã+ æ ¹æ®è¿ç®æ°çä¸åï¼å
·æä¸åç表ç°ï¼å符串 + å符串 = å符串(被è¿æ¥);"a" + "b" = "ab" "5" + "6" = "11"å符串 + æ°å = (å符串转æ¢ææ°å)å符串(被è¿æ¥);"a" + 5 = "a5" 5被转æ¢æå符串 "1" + 0 = "10"æ°å + æ°å = æ°å(ç¸å )5 + 5 = 10ãä¸ã注æè¿ç®ç¬¦çç»åæ§ï¼æäºè¿ç®ç¬¦ä»å·¦å°å³çç»åæ§;æäºä»å³å°å·¦ç»åæ§ãä¾å¦ï¼w = a + b + c çåäº w = (a + b) + c;w = -b çåäº w = - ( - ( -b ) ); w = a = b = c çåäº w= ( a = ( b = c ))ä¸å
è¿ç®ç¬¦ãèµå¼è¿ç®ç¬¦ãä¸å
è¿ç®ç¬¦çç»åæ§æ¯ä»å³å°å·¦ç;åãç®æ¯è¿ç®ç¬¦è§åï¼â+âï¼å¯ä»¥è¿è¡âå æ³âä¸âè¿æ¥âè¿ç®;å¦æ2个è¿ç®ç¬¦ä¸çä¸ä¸ªæ¯å符串ï¼javascriptå°±å°å¦ä¸ä¸ªè½¬æ¢æå符串ï¼ç¶åå°2个è¿ç®æ°è¿æ¥èµ·æ¥ãâ+âï¼å¦æä¸ä¸ªè¿ç®æ°æ¯å¯¹è±¡ï¼javascriptå°±ä¼å°å¯¹è±¡è½¬æ¢ææ°ååå æ³è¿ç®æè
å符串åè¿æ¥è¿ç®;â-ââ*ââ/â å¦æ2个è¿ç®æ°ä¸çä¸ä¸ªæ¯æ¯éæ°åï¼åå°å®è½¬åææ°åæ§è¡æ°å¦è¿ç®ãâ/â å¨javascriptä¸ï¼ç±äºææçæ°é½æ¯æµ®ç¹æ°ï¼é¤æ³çç»æé½æ¯æµ®ç¹æ°ï¼5 / 2 = 2.5 ;é¤æ°ä¸º0çç»æ为æ£è´æ ç©·;0/0为NaN;â%â模è¿ç®ç¬¦ï¼è®¡ç®ç¬¬ä¸ä¸ªè¿ç®æ°å¯¹ç¬¬äºä¸ªè¿ç®æ°ç模ï¼å°±æ¯ç¬¬ä¸ä¸ªè¿ç®æ°è¢«ç¬¬äºä¸ªè¿ç®æ°é¤æ¶ï¼è¿åä½æ°ãå¦æè¿ç®æ°æ¯éæ°åçï¼å转æ¢ææ°åãâ-âä¸å
ååï¼å°è¿ç®æ°ååï¼å¦æè¿ç®æ°ä¸æ¯æ°åï¼å转åææ°åãâ++âââå¢éè¿ç®ç¬¦/åéè¿ç®ç¬¦å·ï¼è¿ç®æ°å¿
é¡»æ¯ä¸ä¸ªåéãæ ç»çä¸ä¸ªå
ç´ æ对象çä¸ä¸ªå±æ§ãå¦æè¿ç®æ°ä¸æ¯æ°åï¼å转æ¢ææ°åã注æï¼å¦æâ++âä½äºè¿ç®æ°ä¹åï¼å
对è¿ç®æ°è¿è¡å¢éï¼ç¶å计ç®è¿ç®æ°å¢é¿åçå¼ãå¦æââä½äºè¿ç®æ°ä¹åï¼å
计ç®è¿ç®æ°æªå¢é¿ä¹åçå¼ï¼ç¶å对è¿ç®æ°å¢éãä¾å¦ï¼i = 1; //èµå¼ç»iå¼1j = ++i; //å
å°iå¢é为2ï¼ç¶åå°2èµå¼ç»jï¼içå¼æ¯2ï¼jçå¼ä¹æ¯2ãi = 1; //èµå¼ç»iå¼1j = i++; //å
å°ièµå¼ç»jï¼ç¶å对å°iå¼å¢éå°2ï¼içå¼æ¯2ï¼jçå¼æ¯1ãåéè¿ç®ç¬¦ââä¸â++âç¸åãäºãçåè¿ç®ç¬¦ä¸å
¨åè¿ç®ç¬¦ï¼(ä¸)â==ââ!==âçåè¿ç®ç¬¦ä¸éçåè¿ç®ç¬¦ï¼æ¯è¾2个è¿ç®æ°ï¼è¿åä¸ä¸ªä¸å¸å°å¼ãæ¯è¾æ°å¼ãå符串ãå¸å°å¼ä½¿ç¨çé½æ¯éå¼ãå½ä¸ä»
å½ä¸¤ä¸ªåéåæ¾çå¼ç¸ççæ¶åï¼ä»ä»¬æç¸çãæ¯è¾å¯¹è±¡ãæ°ç»ãå½æ°çæ¶åï¼ä½¿ç¨çæ¯å¼ç¨ï¼åªæ2个åéå¼ç¨çæ¯åä¸ä¸ªå¯¹è±¡æ¶ï¼ä»ä»¬ææ¯ç¸ççã两个ä¸åçæ°ç»æ¯å®å
¨ä¸åçï¼å³ä½¿ä»ä»¬å
·æå®å
¨ç¸åçå
ç´ ã对äºåæ¾å¯¹è±¡ãæ°ç»ãå½æ°çå¼ç¨çåéæ¥è¯´ï¼åªæå½ä»ä»¬å¼ç¨çæ¯åä¸ä¸ªå¯¹è±¡ãæ°ç»ãå½æ°çæ¶åï¼ä»ä»¬æç¸çã
注æ!åºéµå¾ªçååï¼å½ä¸¤ä¸ªè¿ç®æ°çç±»åä¸åæ¶ï¼å°ä»ä»¬è½¬æ¢æç¸åçç±»åï¼1)ä¸ä¸ªæ°åä¸ä¸ä¸ªå符串ï¼å符串转æ¢ææ°åä¹åï¼è¿è¡æ¯è¾ã2)true转æ¢ä¸º1ãfalse转æ¢ä¸º0ï¼è¿è¡æ¯è¾ã3)ä¸ä¸ªå¯¹è±¡ãæ°ç»ãå½æ° ä¸ ä¸ä¸ªæ°åæå符串ï¼å¯¹è±¡ãæ°ç»ãå½æ°è½¬æ¢ä¸ºåå§ç±»åçå¼ï¼ç¶åè¿è¡æ¯è¾ã(å
使ç¨valueOfï¼å¦æä¸è¡å°±ä½¿ç¨toString)4)å
¶ä»ç±»åçç»åä¸æçãæ³ä¸¤ä¸ªè¿ç®æ°ç±»åç¸åï¼æ转æ¢æç¸åç±»ååï¼1)2个å符串ï¼åä¸ä½ç½®ä¸çå符ç¸çï¼2个å符串就ç¸åã2)2个æ°åï¼2个æ°åç¸åï¼å°±ç¸åãå¦æä¸ä¸ªæ¯NaNï¼æ两个é½æ¯NaNï¼åä¸ç¸åã3)2个é½æ¯trueï¼æè
2个é½æ¯falseï¼åæ³åã4)2个å¼ç¨çæ¯åä¸ä¸ªå¯¹è±¡ãå½æ°ãæ°ç»ï¼åä»ä»¬ç¸çï¼å¦æå¼ç¨çä¸æ¯åä¸ä¸ªå¯¹è±¡ãå½æ°ãæ°ç»ï¼åä¸ç¸åï¼å³ä½¿è¿2个对象ãå½æ°ãæ°ç»å¯ä»¥è½¬æ¢æå®å
¨ç¸ççåå§å¼ã5)2个nullï¼æè
2个é½æ¯æªå®ä¹çï¼é£ä¹ä»ä»¬ç¸çãâ!=âéçåè¿ç®ç¬¦ï¼ä¸çåè¿ç®ç¬¦æ£æµçç»æç¸åã(äº)â===ââ!===âå
¨åè¿ç®ç¬¦ä¸éå
¨åè¿ç®ç¬¦å·ï¼å
¨åè¿ç®ç¬¦éµå¾ªçåè¿ç®ç¬¦çæ¯è¾è§åï¼ä½æ¯å®ä¸å¯¹è¿ç®æ°è¿è¡ç±»å转æ¢ï¼å½ä¸¤ä¸ªè¿ç®æ°çç±»åä¸åæ¶ï¼è¿åfalse;åªæå½ä¸¤ä¸ªè¿ç®æ°çç±»åç¸åçæ¶åï¼æéµå¾ªçåè¿ç®ç¬¦çæ¯è¾è§åè¿è¡æ¯è¾ãâ!==âéå
¨åè¿ç®ç¬¦ä¸å
¨åè¿ç®ç¬¦æ¯è¾çç»æç¸åãå¦æ两个è¿ç®æ°çç±»åæè
å¼ä¸åçæ¶åï¼è¿åtrueãå
ãæ¯è¾è¿ç®ç¬¦ï¼è¿äºæ¯è¾è¿ç®ç¬¦ç¨äºæ¯è¾ä¸åç±»åçå¼ï¼ç»æè¿åä¸ä¸ªå¸å°å¼ãâ<ââ>ââ<=ââ>=â注æè§åï¼æ¯è¾çè¿ç®æ°å¯ä»¥æ¯ä»»æç±»åçï¼ä½æ¯åªè½å¨æ°ååè¿ç®æ°ä¸æ§è¡ãä¸æ¯æ°ååå符串çè¿ç®æ°å°è¢«è½¬æ¢ææ°åæå符串ã1)å¦æ两个è¿ç®æ°é½æ¯æ°åï¼æè
é½å¯ä»¥è½¬æ¢ææ°åï¼åæç
§æ°åç大å°è¿è¡æ¯è¾;2)å¦æ两个è¿ç®æ°é½æ¯å符串ï¼æè
é½å¯ä»¥è½¬æ¢æå符串ï¼åå°æç
§åæ¯é¡ºåºè¿è¡æ¯è¾;3)å¦æå符串éå°æ°åï¼åå°å符串转æ¢ææ°åè¿è¡æ¯è¾ã4)å¦æè¿ç®æ°æ¢ä¸è½è½¬æ¢ææ°åï¼ä¹ä¸è½è½¬æ¢æå符串ï¼åè¿åç»æfalseãä¸ãå符串è¿ç®ç¬¦ï¼å¹¶æ²¡æä¸ç¨çå符串è¿ç®ç¬¦ï¼åªæ¯æäºè¿ç®ç¬¦å¨éå°å符串è¿ç®æ°çæ¶åï¼è¡¨ç°ä¸åã(ä¸)â+âè¿æ¥2个å符串;1)å½2个è¿ç®æ°é½æ¯å符串çæ¶åï¼è¿æ¥èµ·æ¥;2)å½å
¶ä¸æä¸ä¸ªæ¯æ°åçæ¶åï¼å°æ°å转æ¢æå符串ï¼è¿æ¥èµ·æ¥;(äº)â>âè¿æ ·çæ¯è¾è¿ç®ç¬¦éè¿æ¯è¾ç¡®è®¤ä¸¤ä¸ªå符串ç顺åºï¼æ¯è¾éç¨å符ç顺åºï¼è¾å°çä½äºæ大çåé¢ï¼å¤§ååæ¯ä½äºå°ååæ¯ä¹åã(ä¸)â+âçä½ç¨æ¹æ³åå³äºè®¡ç®é¡ºåºï¼å¦ï¼s = 1 + 2 +"var" åï¼è¿åç»æ3var; å 为å
计ç®1+2ï¼ç¶åå°ç»æ3转æ¢æå符串ä¸"var"è¿æ¥;å¦ï¼s = "var" + 1 + 2 åï¼è¿åç»ævar12; å 为å
计ç®varä¸1çè¿æ¥ï¼å¨å°ç»ævar1ä¸è½¬æ¢æå符串ç2è¿æ¥èµ·æ¥ãå
«ãé»è¾è¿ç®ç¬¦ï¼ç¨äºæ§è¡å¸å°è¿ç®ï¼å¸¸å¸¸åæ¯è¾è¿ç®ç¬¦ä¸èµ·ä½¿ç¨ï¼æ¥è¡¨ç¤ºå¤æçæ¯è¾è¿ç®ãâ&&â é»è¾ä¸è¿ç®ãââé»è¾æè¿ç®ç¬¦ãâ!âé»è¾éè¿ç®ç¬¦(ä¸)â&&âå½ä¸¤ä¸ªè¿ç®æ°é½æ¯å¸å°å¼çæ¶åï¼é»è¾ä¸å¯¹ä»ä»¬è¿è¡andæä½ï¼å³ï¼å½ä¸ä»
å½ä¸¤ä¸ªå¸å°å¼é½æ¯trueçæ¶åï¼è¿åç»ætrueï¼å¦åè¿åfalseã注æï¼å®é
表ç°â&&âå°æ£æµç¬¬ä¸ä¸ªè¡¨è¾¾å¼è¿ç®æ°å¸å°å¼ï¼å¦æ第ä¸ä¸ªè¿ç®æ°è¡¨è¾¾å¼è¿åfalseï¼åè¿å左边第ä¸ä¸ªè¿ç®æ°è¡¨è¾¾å¼çå¼ï¼false;å¦åå°ç»§ç»æ£æµç¬¬äºä¸ªå³å³è¾¹çè¿ç®æ°è¡¨è¾¾å¼ï¼ç¶åè¿å第äºä¸ªè¿ç®æ°è¡¨è¾¾å¼çå¼;ä¾å¦ï¼if (a = b) stop(); ä¸ (a = b) && stop(); çä»·ä¸èµæ使ç¨è¿ä¸ªæ¹æ³ï¼å 为è¿ç®ç¬¦å³è¾¹ç代ç ä¸è½ä¿è¯ä¼è¢«æ§è¡ï¼ä¾å¦ï¼if ((a < b) && (b++ <10) ) stop();å¦æè¿ç®ç¬¦å³è¾¹çå¢éå¿
é¡»çè¯ï¼å´æå¯è½ç±äºåé¢çè¿åå¼èä¸è¢«æ§è¡ï¼ä¸è¬å对å¨&&çå³è¾¹ä½¿ç¨å
·æå
¶ä»ä½ç¨ç表达å¼(èµå¼ãå½æ°è°ç¨ãå¢åé);å°â&&âçä½æ¯ä¸ä¸ªå¸å°ä»£æ°çè¿ç®ç¬¦ï¼ä¼æ´ç®åä¸å®å
¨ã(äº)ââå½ä¸¤ä¸ªè¿ç®æ°é½æ¯å¸å°å¼çæ¶åï¼é»è¾æ对ä»ä»¬è¿è¡oræä½ï¼å³ï¼å½ä¸¤ä¸ªå¸å°å¼æä¸ä¸ªæ¯trueçæ¶åï¼è¿åç»ætrueï¼å¦åè¿åfalseã注æï¼å®é
表ç°ââå°æ£æµç¬¬ä¸ä¸ªè¡¨è¾¾å¼è¿ç®æ°å¸å°å¼ï¼å¦æ第ä¸ä¸ªè¿ç®æ°è¡¨è¾¾å¼è¿åtrueï¼åè¿å左边第ä¸ä¸ªè¿ç®æ°è¡¨è¾¾å¼çå¼ï¼true;å¦åå°ç»§ç»æ£æµç¬¬äºä¸ªå³å³è¾¹çè¿ç®æ°è¡¨è¾¾å¼ï¼ç¶åè¿å第äºä¸ªè¿ç®æ°è¡¨è¾¾å¼çå¼;åæ ·ä¸èµæ使ç¨è¿ä¸ªæ¹æ³ï¼å 为è¿ç®ç¬¦å³è¾¹ç代ç ä¸è½ä¿è¯ä¼è¢«æ§è¡ï¼ä¸è¬å对å¨çå³è¾¹ä½¿ç¨å
·æå
¶ä»ä½ç¨ç表达å¼(èµå¼ãå½æ°è°ç¨ãå¢åé);å°ââçä½æ¯ä¸ä¸ªå¸å°ä»£æ°çè¿ç®ç¬¦ï¼ä¼æ´ç®åä¸å®å
¨ã(ä¸)â!âé»è¾éæ¯ä¸ä¸ªä¸å
è¿ç®ç¬¦ï¼æ¾å¨è¿ç®æ°ä¹åï¼å®çç®çæ¯å¯¹è¿ç®æ°ååãä¹ãèµå¼è¿ç®ç¬¦ï¼(ä¸)â=âæ¯èµå¼è¿ç®ç¬¦;ä»æ»æ¯ææ左边çè¿ç®æ°æ¯ä¸ä¸ªåéãæ°ç»çä¸ä¸ªå
ç´ æ对象çä¸ä¸ªå±æ§;ææå³è¾¹æ¯ä¸ä¸ªä»»æç±»åçä»»æçå¼;ä»å³å°å·¦çç»åæ§ï¼å¦æä¸ä¸ªè¡¨è¾¾å¼ä¸æå¤ä¸ªèµå¼è¿ç®ç¬¦ï¼åä»æå³è¾¹å¼å§è®¡ç®ã注æï¼æ¯ä¸ä¸ªèµå¼è¡¨è¾¾å¼é½æä¸ä¸ªå¼ï¼å°±æ¯è¿ç®ç¬¦å³è¾¹çå¼;(äº)å¯ä»¥ä½¿ç¨å¸¦æä½çèµå¼è¿ç®â+=â 左边çå¼å ä¸å³è¾¹çå¼åï¼èµå¼äºå·¦è¾¹çå¼ãâ-=ââ/=ââ*=âæ¹æ³ä¸æ ·;åãå
¶ä»è¿ç®ç¬¦ï¼â?:âæ¡ä»¶è¿ç®ç¬¦æ¯å¯ä¸çä¸ä¸ªä¸å
è¿ç®ç¬¦;ä¸ä¸ªè¡¨è¾¾å¼çå¸å°ç»æ?表达å¼1(ä»»æç±»åçä»»æå¼)ï¼è¡¨è¾¾å¼2(ä»»æç±»åçä»»æå¼);æ ¹æ®ç¬¬ä¸ä¸ªè¿ç®æ°çå¸å°å¼ç»æï¼å¦æ为trueï¼åæ§è¡ç¬¬äºä¸ªè¿ç®æ°è¡¨è¾¾å¼ï¼è¿å第äºä¸ªè¿ç®æ°è¡¨è¾¾å¼çå¼;å¦æ第ä¸ä¸ªè¿ç®æ°çå¸å°å¼ç»ææ¯falseï¼åæ§è¡ç¬¬ä¸ä¸ªè¿ç®æ°è¡¨è¾¾å¼ï¼è¿å第ä¸ä¸ªè¿ç®æ°è¡¨è¾¾å¼çå¼ã
移ä½è¿ç®ç¬¦é¢åçè¿ç®å¯¹è±¡ä¹æ¯äºè¿å¶çâä½âãå¯åç¬ç¨å®ä»¬å¤çæ´æ°ç±»åï¼ä¸»ç±»åçä¸ç§ï¼ã左移ä½è¿ç®ç¬¦ï¼<<ï¼è½å°è¿ç®ç¬¦å·¦è¾¹çè¿ç®å¯¹è±¡å左移å¨è¿ç®ç¬¦å³ä¾§æå®çä½æ°ï¼å¨ä½ä½è¡¥0ï¼ãâæ符å·âå³ç§»ä½è¿ç®ç¬¦ï¼>>ï¼åå°è¿ç®ç¬¦å·¦è¾¹çè¿ç®å¯¹è±¡åå³ç§»å¨è¿ç®ç¬¦å³ä¾§æå®çä½æ°ãâæ符å·âå³ç§»ä½è¿ç®ç¬¦ä½¿ç¨äºâ符å·æ©å±âï¼è¥å¼ä¸ºæ£ï¼åå¨é«ä½æå
¥0ï¼è¥å¼ä¸ºè´ï¼åå¨é«ä½æå
¥1ãJavaä¹æ·»å äºä¸ç§âæ 符å·âå³ç§»ä½è¿ç®ç¬¦ï¼>>>ï¼ï¼å®ä½¿ç¨äºâé¶æ©å±âï¼æ 论æ£è´ï¼é½å¨é«ä½æå
¥0ãè¿ä¸è¿ç®ç¬¦æ¯CæC++没æçã
ããè¥å¯¹charï¼byteæè
shortè¿è¡ç§»ä½å¤çï¼é£ä¹å¨ç§»ä½è¿è¡ä¹åï¼å®ä»¬ä¼èªå¨è½¬æ¢æä¸ä¸ªintãåªæå³ä¾§ç5个ä½ä½æä¼ç¨å°ãè¿æ ·å¯é²æ¢æ们å¨ä¸ä¸ªintæ°é移å¨ä¸åå®é
çä½æ°ãè¥å¯¹ä¸ä¸ªlongå¼è¿è¡å¤çï¼æåå¾å°çç»æä¹æ¯longãæ¤æ¶åªä¼ç¨å°å³ä¾§ç6个ä½ä½ï¼é²æ¢ç§»å¨è¶
è¿longå¼éç°æçä½æ°ãä½å¨è¿è¡âæ 符å·âå³ç§»ä½æ¶ï¼ä¹å¯è½éå°ä¸ä¸ªé®é¢ãè¥å¯¹byteæshortå¼è¿è¡å³ç§»ä½è¿ç®ï¼å¾å°çå¯è½ä¸æ¯æ£ç¡®çç»æï¼Java 1.0åJava 1.1ç¹å«çªåºï¼ãå®ä»¬ä¼èªå¨è½¬æ¢æintç±»åï¼å¹¶è¿è¡å³ç§»ä½ãä½âé¶æ©å±âä¸ä¼åçï¼æ以å¨é£äºæ
åµä¸ä¼å¾å°-1çç»æãå¯ç¨ä¸é¢è¿ä¸ªä¾åæ£æµèªå·±çå®ç°æ¹æ¡ï¼
ãã//: URShift.java
ãã// Test of unsigned right shift
ããpublic class URShift {
ãããpublic static void main(String[] args) {
ããããint i = -1;
ããããi >>>= 10;
ããããSystem.out.println(i);
ããããlong l = -1;
ããããl >>>= 10;
ããããSystem.out.println(l);
ããããshort s = -1;
ããããs >>>= 10;
ããããSystem.out.println(s);
ããããbyte b = -1;
ããããb >>>= 10;
ããããSystem.out.println(b);
ããã}
ãã} ///:~
ãã移ä½å¯ä¸çå·ï¼<<=æ>>=æ>>>=ï¼ç»å使ç¨ãæ¤æ¶ï¼è¿ç®ç¬¦å·¦è¾¹çå¼ä¼ç§»å¨ç±å³è¾¹çå¼æå®çä½æ°ï¼åå°å¾å°çç»æèµå左边çå¼ã
ããä¸é¢è¿ä¸ªä¾åå大家é示äºå¦ä½åºç¨æ¶åâæä½âæä½çææè¿ç®ç¬¦ï¼ä»¥åå®ä»¬çææï¼
ãã//: BitManipulation.java
ãã// Using the bitwise operators
ããimport java.util.*;
ããpublic class BitManipulation {
ãããpublic static void main(String[] args) {
ããããRandom rand = new Random();
ããããint i = rand.nextInt();
ããããint j = rand.nextInt();
ããããpBinInt("-1", -1);
ããããpBinInt("+1", +1);
ããããint maxpos = 2147483647;
ããããpBinInt("maxpos", maxpos);
ããããint maxneg = -2147483648;
ããããpBinInt("maxneg", maxneg);
ããããpBinInt("i", i);
ããããpBinInt("~i", ~i);
ããããpBinInt("-i", -i);
ããããpBinInt("j", j);
ããããpBinInt("i & j", i & j);
ããããpBinInt("i | j", i | j);
ããããpBinInt("i ^ j", i ^ j);
ããããpBinInt("i << 5", i << 5);
ããããpBinInt("i >> 5", i >> 5);
ããããpBinInt("(~i) >> 5", (~i) >> 5);
ããããpBinInt("i >>> 5", i >>> 5);
ããããpBinInt("(~i) >>> 5", (~i) >>> 5);
ããããlong l = rand.nextLong();
ããããlong m = rand.nextLong();
ããããpBinLong("-1L", -1L);
ããããpBinLong("+1L", +1L);
ããããlong ll = 9223372036854775807L;
ããããpBinLong("maxpos", ll);
ããããlong lln = -9223372036854775808L;
ããããpBinLong("maxneg", lln);
ããããpBinLong("l", l);
ããããpBinLong("~l", ~l);
ããããpBinLong("-l", -l);
ããããpBinLong("m", m);
ããããpBinLong("l & m", l & m);
ããããpBinLong("l | m", l | m);
ããããpBinLong("l ^ m", l ^ m);
ããããpBinLong("l << 5", l << 5);
ããããpBinLong("l >> 5", l >> 5);
ããããpBinLong("(~l) >> 5", (~l) >> 5);
ããããpBinLong("l >>> 5", l >>> 5);
ããããpBinLong("(~l) >>> 5", (~l) >>> 5);
ããã}
ãããstatic void pBinInt(String s, int i) {
ããããSystem.out.println(
ãããããs + ", int: " + i + ", binary: ");
ããããSystem.out.print("ã ");
ããããfor(int j = 31; j >=0; j--)
ãããããif(((1 << j) &ãi) != 0)
ããããããSystem.out.print("1");
ãããããelse
ããããããSystem.out.print("0");
ããããSystem.out.println();
ããã}
ãããstatic void pBinLong(String s, long l) {
ããããSystem.out.println(
ãããããs + ", long: " + l + ", binary: ");
ããããSystem.out.print("ã ");
ããããfor(int i = 63; i >=0; i--)
ãããããif(((1L << i) & l) != 0)
ããããããSystem.out.print("1");
ãããããelse
ããããããSystem.out.print("0");
ããããSystem.out.println();
ããã}
ãã} ///:~
ããç¨åºæ«å°¾è°ç¨äºä¸¤ä¸ªæ¹æ³ï¼pBinInt()åpBinLong()ãå®ä»¬åå«æä½ä¸ä¸ªintålongå¼ï¼å¹¶ç¨ä¸ç§äºè¿å¶æ ¼å¼è¾åºï¼åæ¶éæç®è¦ç说ææåãç®åï¼å¯ææ¶å¿½ç¥å®ä»¬å
·ä½çå®ç°æ¹æ¡ã
ãã大家è¦æ³¨æçæ¯System.out.print()ç使ç¨ï¼èä¸æ¯System.out.println()ãprint()æ¹æ³ä¸ä¼äº§çä¸ä¸ªæ°è¡ï¼ä»¥ä¾¿å¨åä¸è¡éç½åå¤ç§ä¿¡æ¯ã
ããé¤å±ç¤ºæææä½è¿ç®ç¬¦é对intålongçææä¹å¤ï¼æ¬ä¾ä¹å±ç¤ºäºintålongçæå°å¼ãæ大å¼ã+1å-1å¼ï¼ä½¿å¤§å®¶è½ä½ä¼å®ä»¬çæ
åµã注æé«ä½ä»£è¡¨æ£è´å·ï¼0为æ£ï¼1为è´ãä¸é¢ååºinté¨åçè¾åºï¼
ãã-1, int: -1, binary:
ããã 11111111111111111111111111111111
ãã+1, int: 1, binary:
ããã 00000000000000000000000000000001
ããmaxpos, int: 2147483647, binary:
ããã 01111111111111111111111111111111
ããmaxneg, int: -2147483648, binary:
ããã 10000000000000000000000000000000
ããi, int: 59081716, binary:
ããã 00000011100001011000001111110100
ãã~i, int: -59081717, binary:
ããã 11111100011110100111110000001011
ãã-i, int: -59081716, binary:
ããã 11111100011110100111110000001100
ããj, int: 198850956, binary:
ããã 00001011110110100011100110001100
ããi & j, int: 58720644, binary:
ããã 00000011100000000000000110000100
ããi | j, int: 199212028, binary:
ããã 00001011110111111011101111111100
ããi ^ j, int: 140491384, binary:
ããã 00001000010111111011101001111000
ããi << 5, int: 1890614912, binary:
ããã 01110000101100000111111010000000
ããi >> 5, int: 1846303, binary:
ããã 00000000000111000010110000011111
ãã(~i) >> 5, int: -1846304, binary:
ããã 11111111111000111101001111100000
ããi >>> 5, int: 1846303, binary:
ããã 00000000000111000010110000011111
ãã(~i) >>> 5, int: 132371424, binary:
ããã 00000111111000111101001111100000
ããæ°åçäºè¿å¶å½¢å¼è¡¨ç°ä¸ºâæ符å·2çè¡¥å¼âã
温馨提示:答案为网友推荐,仅供参考