zoukankan      html  css  js  c++  java
  • java 运算符

    public class Demo02 {
        public static void main(String[] args) {
                int i1 = 128;
                int i2 = 129;
                System.out.println(Integer.toBinaryString(i1));
                System.out.println(Integer.toBinaryString(i2));
                System.out.println(Integer.toBinaryString(i2&i1));
                System.out.println(i1&i2);
        }
    }

    输出结果:

      10000000

      10000001

      10000000

      128

    ------------------------------------------------------------------------------------

    public class Demo02 {
        public static void main(String[] args) {
                int i1 = 128;
                int i2 = 129;
                System.out.println(Integer.toBinaryString(i1));
                System.out.println(Integer.toBinaryString(i2));
                System.out.println(Integer.toBinaryString(i2|i1));
                System.out.println(i1|i2);
        }
    }

    输出结果:

      10000000

      10000001

      10000001

      129

    ---------------------------------------------------------------------------

    public class Demo02 {
        public static void main(String[] args) {
                int i1 = 128;
                int i2 = 129;
                System.out.println(Integer.toBinaryString(i1));
                System.out.println(Integer.toBinaryString(i2));
                System.out.println(Integer.toBinaryString(i2^i1));
                System.out.println(i1^i2);
        }
    }

    输出结果:

      10000000

      10000001

      00000001(应该是1,但是为了方便理解)

      1

    ---------------------------------------------------------------------------------

    public class Demo02 {
        public static void main(String[] args) {
                int i1 = 128;
                int i2 = 129;
                System.out.println(Integer.toBinaryString(i1));
                System.out.println(Integer.toBinaryString(i2));
                System.out.println(Integer.toBinaryString(~i1));
                System.out.println(~i1);
        }
    }

    输出结果:

      10000000

      10000001

      11111111111111111111111101111111

      -129

    ------------------------------------------------------------------------------------

    上述结果可以得出结论:

      &(与):两个操作数中位都为1,结果才为1,否则结果为0;

      |(或):两个操作数有一个为1时,结果就为1,否则结果为0;

      ^(异或):两个操作数不同时,结果为1,否则结果为0;

      ~(非):如果位为0,结果为1,反之,结果为0;

  • 相关阅读:
    LeetCode(111) Minimum Depth of Binary Tree
    LeetCode(108) Convert Sorted Array to Binary Search Tree
    LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode(99) Recover Binary Search Tree
    【Android】通过经纬度查询城市信息
    【Android】自定义View
    【OpenStack Cinder】Cinder安装时遇到的一些坑
    【积淀】半夜突然有点想法
    【Android】 HttpClient 发送REST请求
  • 原文地址:https://www.cnblogs.com/hongcong/p/5764641.html
Copyright © 2011-2022 走看看