zoukankan      html  css  js  c++  java
  • java 位操作 bitwise(按位) operation bit

    java 位操作 bitwise(按位) operation bit

    //一篇对于 原码 反码 补码 的介绍

     http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html

    java中有三种移位运算符

    <<      :     左移运算符,num << 1,相当于num乘以2

    >>      :     右移运算符,num >> 1,相当于num除以2

    >>>    :     无符号右移,忽略符号位,空位都以0补齐

    // 8   0000 0000 0000 1000     原码
          1111 1111 1111 0111     反码

    +              1

       1111 1111 1111 1000     (8的补码)来表示 -8

    // -8 1111 1111 1111 1000 65528     补码(正值 的反码+1)

    // 65535 1111 1111 1111 1111 65535
    // 65535-65528=7+1=8

     
    操作longValue = longValue | (1 << n); 可以在longValue的2进制表示中,把从右边数到左边数第n + 1位的值设置为1,并且不影响其他位上面的值   即用0和2进制值变量x做或|or操作,不会影响到2进制变量x的值 

    操作longValue = longValue & ~(1 << n); 可以在longValue的2进制表示中,把从右边数到左边数第n + 1位设置为0,并且不影响其他位上面的值   即用1和2进制值变量x做与&and操作,不会影响到2进制变量x的值 

    操作System.out.println((longValue >> n & 1) == 1); 可以判断值longValue的2进制表示中,从右边数到左边第n + 1位的值是0false 还是1true 

    Java代码  收藏代码
    1. public class bitOperation {  
    2.   
    3.     /** 
    4.      * @param args 
    5.      */  
    6.     public static void main(String[] args) {  
    7.           
    8.         long longValue = 0;  
    9.           
    10.         longValue = longValue | (1 << 0);  
    11.         // 1  
    12.         System.out.println(Long.toBinaryString(longValue));  
    13.         longValue = longValue | (1 << 1);  
    14.         // 11  
    15.         System.out.println(Long.toBinaryString(longValue));  
    16.         longValue = longValue | (1 << 4);  
    17.         // 10011  
    18.         System.out.println(Long.toBinaryString(longValue));  
    19.         longValue = longValue | (1 << 5);  
    20.         // 110011  
    21.         System.out.println(Long.toBinaryString(longValue));  
    22.         longValue = longValue | (1 << 6);  
    23.         // 1110011  
    24.         System.out.println(Long.toBinaryString(longValue));  
    25.   
    26.         String hex = Long.toBinaryString(longValue);  
    27.         // 1110011  
    28.         System.out.println(hex);  
    29.         // 115  
    30.         System.out.println(Integer.valueOf("1110011", 2));  
    31.         // 1110011  
    32.         System.out.println(Long.toBinaryString(longValue >> 0));  
    33.         // 1  
    34.         System.out.println(Long.toBinaryString(longValue >> 0 & 1));  
    35.         // 111001  
    36.         System.out.println(Long.toBinaryString(longValue >> 1));  
    37.         // 1  
    38.         System.out.println(Long.toBinaryString(longValue >> 1 & 1));  
    39.         // true  
    40.         System.out.println((longValue >> 0 & 1) == 1);  
    41.         // true  
    42.         System.out.println((longValue >> 1 & 1) == 1);  
    43.         // false  
    44.         System.out.println((longValue >> 2 & 1) == 1);  
    45.         // false  
    46.         System.out.println((longValue >> 3 & 1) == 1);  
    47.         // true  
    48.         System.out.println((longValue >> 4 & 1) == 1);  
    49.         // true  
    50.         System.out.println((longValue >> 5 & 1) == 1);  
    51.         // true  
    52.         System.out.println((longValue >> 6 & 1) == 1);  
    53.         // false  
    54.         System.out.println((longValue >> 7 & 1) == 1);  
    55.   
    56.         // Demonstrate the bitwise logical operators.  
    57.         bitLogic();  
    58.         // Left shifting a byte value.  
    59.         byteShift();  
    60.     }  
    61.   
    62.     /** 
    63.      * Left shifting a byte value. 
    64.      */  
    65.     private static void byteShift() {  
    66.         byte a = 64, b;  
    67.         int i;  
    68.   
    69.         i = a << 2;  
    70.         b = (byte) (a << 2);  
    71.   
    72.         // Original value of a: 64  
    73.         System.out.println("Original value of a: " + a);  
    74.         // i and b: 256 0  
    75.         System.out.println("i and b: " + i + " " + b);  
    76.         System.out.println(" ");  
    77.     }  
    78.   
    79.     /** 
    80.      * Demonstrate the bitwise logical operators. 
    81.      */  
    82.     private static void bitLogic() {  
    83.         String binary[] = { "0000", "0001", "0010", "0011", "0100", "0101",  
    84.                 "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101",  
    85.                 "1110", "1111"  
    86.   
    87.         };  
    88.         int a = 3; // 0 + 2 + 1 or 0011 in binary  
    89.         int b = 6; // 4 + 2 + 0 or 0110 in binary  
    90.         int c = a | b;  
    91.         int d = a & b;  
    92.         int e = a ^ b;  
    93.         int f = (~a & b) | (a & ~b);  
    94.         int g = ~a & 0x0f;  
    95.   
    96.         // a = 0011 = 3  
    97.         System.out.println(" a = " + binary[a] + " = " + a);  
    98.         // b = 0110 = 6  
    99.         System.out.println(" b = " + binary[b] + " = " + b);  
    100.         // a|b = 0111 = 7  
    101.         System.out.println(" a|b = " + binary[c] + " = " + c);  
    102.         // a&b = 0010 = 2  
    103.         System.out.println(" a&b = " + binary[d] + " = " + d);  
    104.         // a^b = 0101 = 5  
    105.         System.out.println(" a^b = " + binary[e] + " = " + e);  
    106.         // ~a&b|a&~b = 0101 = 5  
    107.         System.out.println("~a&b|a&~b = " + binary[f] + " = " + f);  
    108.         // ~a = 1100 = 12  
    109.         System.out.println(" ~a = " + binary[g] + " = " + g);  
    110.         System.out.println(" ");  
    111.     }  
    112. }  
  • 相关阅读:
    div+css简写原则
    并发控制
    div+css之块级元素与内联元素
    window.event对象属性(转)
    SQL SERVER 架构管理
    关系的规范化
    js常用事件
    物联小白CoAP协议
    图片不停的横滚
    DropDownlist编程问题
  • 原文地址:https://www.cnblogs.com/rojas/p/4571879.html
Copyright © 2011-2022 走看看