public static void main(String[] args) {
/*
* &:与运算
* 全为1则为1,否则为0
*/
System.out.print(1 & 0);
System.out.print("--");
System.out.print(1 & 1);
System.out.print("--");
System.out.println(0 & 0);
// out:0--1--0 /*
* |:或运算
* 全为0则为0,否则为1
*/
System.out.print(1 | 0);
System.out.print("--");
System.out.print(1 | 1);
System.out.print("--");
System.out.println(0 | 0);
// out:1--1--0 /*
* ^:异或运算
* 相同为0,不同为1
*/
System.out.print(1 ^ 0);
System.out.print("--");
System.out.print(1 ^ 1);
System.out.print("--");
System.out.println(0 ^ 0);
// out:1--0--0
}

关于'<<'与'>>'操作:

  • m<<n,表示m二进制,右边尾部加0;
  • m>>n,表示m二进制,右边尾部去掉1位;
  • m>>>n,表示m二进制,忽略其符号位,从左至右,去掉最后的n位;
  • 不存在'<<<';
 public static void main(String[] args) {
int integer = 2;
printBinary(integer);
integer = 2 >> 1;
printBinary(integer);
integer = 2 >> 2;
printBinary(integer);
integer = 2 >> 3;
printBinary(integer);
System.out.println("====================");
integer = 2 << 1;
printBinary(integer);
integer = 2 << 2;
printBinary(integer);
integer = 2 << 3;
printBinary(integer);
System.out.println("====================");
integer = -2 << 1;
printBinary(integer);
System.out.println("====================");
integer = -2 >> 1;
printBinary(integer);
System.out.println("====================");
integer = 3 >> 1;
printBinary(integer);
System.out.println("====================");
integer = -2;
printBinary(integer);
printBinary(integer>>>1);
printBinary(-integer);
printBinary(-integer>>>1);
}
private static void printBinary(Integer integer) {
System.out.println("Integer.toBinaryString()="+Integer.toBinaryString(integer)+", integer="+integer);
}
/**
Integer.toBinaryString()=10, integer=2
Integer.toBinaryString()=1, integer=1
Integer.toBinaryString()=0, integer=0
Integer.toBinaryString()=0, integer=0
====================
Integer.toBinaryString()=100, integer=4
Integer.toBinaryString()=1000, integer=8
Integer.toBinaryString()=10000, integer=16
====================
Integer.toBinaryString()=11111111111111111111111111111100, integer=-4
====================
Integer.toBinaryString()=11111111111111111111111111111111, integer=-1
====================
Integer.toBinaryString()=1, integer=1
====================
Integer.toBinaryString()=11111111111111111111111111111110, integer=-2
Integer.toBinaryString()=1111111111111111111111111111111, integer=2147483647
Integer.toBinaryString()=10, integer=2
Integer.toBinaryString()=1, integer=1
*/

Java中'&'与、'|'或、'^'异或、'<<'左移位、'>>'右移位的更多相关文章

  1. 剑指offer系列——二维数组中,每行从左到右递增,每列从上到下递增,设计算法找其中的一个数

    题目:二维数组中,每行从左到右递增,每列从上到下递增,设计一个算法,找其中的一个数 分析: 二维数组这里把它看作一个矩形结构,如图所示: 1 2 8 2 4 9 12 4 7 10 13 6 8 11 ...

  2. (转)思考:矩阵及变换,以及矩阵在DirectX和OpenGL中的运用问题:左乘/右乘,行优先/列优先,...

    转自:http://www.cnblogs.com/soroman/archive/2008/03/21/1115571.html 思考:矩阵及变换,以及矩阵在DirectX和OpenGL中的运用1. ...

  3. java中的移位运算符:<<,>>,>>>总结

    java中有三种移位运算符 <<      :     左移运算符,num << 1,相当于num乘以2 >>      :     右移运算符,num >& ...

  4. Java中的移位运算符

    java中有三种移位运算符 <<      :     左移运算符,num << 1,相当于num乘以2 >>      :     右移运算符,num >& ...

  5. java中的移位运算符:<<,>>,>>>总结(转)

    java中有三种移位运算符 <<      :     左移运算符,num << 1,相当于num乘以2 >>      :     右移运算符,num >& ...

  6. 《剑指Offer》第1题(Java实现):在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    一.题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该 ...

  7. 【java编程】java中的移位运算符

    java中有三种移位运算符 <<      :     左移运算符,num << 1,相当于num乘以2 >>      :     右移运算符,num >& ...

  8. java中,有关移位运算符的有关讨论

    java中有三种移位运算符 <<      :     左移运算符,num << 1,相当于num乘以2 >>      :     右移运算符,num >& ...

  9. Java中有趣的移位操作!彻底弄懂各个移位操作符的使用方式

    << <<: 左移运算,左移几位就补几个0 >> >>: 右移运算,为算术右移 如果数字为正数时,移位后在前面补0 如果数字为负数时,移位后在前面补1 ...

随机推荐

  1. DB2常用函数:字符串函数

    VALUE函数 语法:VALUE(EXPRESSION1,EXPRESSION2) VALUE函数是用返回一个非空的值,当其第一个参数非空,直接返回该参数的值,如果第一个参数为空,则返回第一个参数的值 ...

  2. Good Bye 2014 D. New Year Santa Network 图论+期望

    D. New Year Santa Network   New Year is coming in Tree World! In this world, as the name implies, th ...

  3. 用Python+Django在Eclipse环境下开发web网站【转】

    一.创建一个项目如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的设置集,包含了数据库配置.Django详细选项设 ...

  4. 03 - Oracle文件概述

    构成Oracle数据库的8种文件类型. 可以把这些文件分成以下几类. Instance相关 参数文件 parameter, initOra file, spfile 跟踪文件 trace file 警 ...

  5. 算法导论:Trie字典树

    1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...

  6. Delphi动态事件深入分析(对象方法在调用的时候会传递一个隐含的Self指针,而该指针的值在EAX中。即左边第一个参数)

    Delphi动态事件深入分析 2009-2-7 作者:不得闲核心提示:本实验证明了在类中方法的调用时候,所有的方法都隐含了一个Self参数,并且该参数作为对象方法的第一个参数传递... 首先做一个空窗 ...

  7. WPF之无法触发KeyDown或者KeyUp键盘事件

    有时候我们可能在Panel(StackPanel.Canvas.Grid)上或者是在一些默认不支持Focus的控件上添加了KeyDown或者KeyUp,可是残酷的现实告诉我们,这是无法触发的,怎么办呢 ...

  8. C 中变参函数的处理方式

    C 函数中变化的参数用‘...’ 表示.变化的参数依旧按照C函数传参的规则入栈,即从右往左依次入栈,保证参数从左往右地址依次升高. 解析变参的主要思想是:将变参缓冲区像容纳了不同类型的数组(当然实际的 ...

  9. jQuery常见面试题(转)

    代码以jQuery 1.83 为例 一 :Q: What is the difference between .get(), [], and .eq()? A: eq返回原生jQuery对象,截取某些 ...

  10. JVM垃圾回收机制总结(1) :一些概念

    数据类型 Java虚拟机中,数据类型可以分为两类:基本类型 和引用类型 .基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本 ...