敬的世界

          常用鏈接

          統計

          最新評論

          Bitwise and Bit Shift Operators

          Bitwise and Bit Shift Operators
          The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. The operators discussed in this section are less commonly used. Therefore, their coverage is brief; the intent is to simply make you aware that these operators exist.

          The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".

          The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

          The bitwise & operator performs a bitwise AND operation.

          The bitwise ^ operator performs a bitwise exclusive OR operation.

          The bitwise | operator performs a bitwise inclusive OR operation.

          The following program, BitDemo , uses the bitwise AND operator to print the number "2" to standard output.

          								class BitDemo {
               public static void main(String[] args) {
                    int bitmask = 0x000F;
          	  int val = 0x2222;
          	  System.out.println(val & bitmask);  // prints "2"
               }
          
          						

          posted on 2009-05-14 17:43 picture talk 閱讀(377) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 莎车县| 和林格尔县| 偃师市| 平乐县| 巩留县| 瑞丽市| 靖江市| 文昌市| 宝清县| 舞钢市| 固原市| 宿迁市| 宁明县| 瓮安县| 西乌珠穆沁旗| 津市市| 都江堰市| 中西区| 紫云| 甘南县| 乌拉特前旗| 鄂伦春自治旗| 黄浦区| 安图县| 广平县| 巴青县| 弥勒县| 枣阳市| 沂水县| 洛南县| 华池县| 婺源县| 玉田县| 蓝山县| 巨鹿县| 长兴县| 遂溪县| 西乡县| 岢岚县| 平潭县| 若尔盖县|