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. 编程实现Linux下的ls -l

    头文件 #ifndef __FUNC_H__ #define __FUNC_H__ #include <stdio.h> #include <stdlib.h> #includ ...

  2. redis资料汇总

    redis资源比较零散,引用nosqlfan上的文章,方便大家需要时翻阅.大家看完所有的,如果整理出文章的,麻烦知会一下,方便学习. 1.Redis是什么? 十五分钟介绍 Redis数据结构 Redi ...

  3. ios开发中超简单抽屉效果(MMDrawerController)的实现

    ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...

  4. wordpress安装,创建配置文件无反应

    wordpress安装时,点击“创建配置文件”无反应,可以手动创建wp-config.php, 打开wp-config-sample.php,配置相关信息,然后将文件改名为wp-config.php上 ...

  5. jquery.chosen.js实现模糊搜索

    jquery.chosen.js查询时,chosen默认从第一个字符搜索,所以写中间的字符搜索时,是搜索不出来的 若想实现中间字符的模糊查询,下面的js中(search_contains属性为true ...

  6. SpringMVC整合Shiro——(3)

    SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能. 第一步:配置web.xml <!-- 配置Shiro过滤器,先让Shiro ...

  7. MVC用户登陆验证及权限检查(Form认证)

    1.配置Web.conf,使用Form认证方式   <system.web>     <authentication mode="None" />      ...

  8. What a version number means

    http://stackoverflow.com/questions/3768261/best-practices-guidance-for-maintaining-assembly-version- ...

  9. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  10. 【笨嘴拙舌WINDOWS】SetCapture和ReleaseCapture

    光电鼠标器是通过红外线或激光检测鼠标器的位移,将位移信号转换为电脉冲信号,再通过程序的处理和转换来控制屏幕上的光标箭头的移动的一种硬件设备. 换句话说,鼠标无时无刻不在监视着人类的活动,当人类用意识去 ...