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. 网络处理1-异步GET请求

    前言 云计算 近几年来,云计算是一个非常热门的技术名词,很多专家认为,云计算会改变互联网的技术基础,甚至会影响整个产业的格局.可能还很多人不了解什么是云计算,简单来说,就是把用户的数据(比如文档.照片 ...

  2. MySQL数据库的基本操作命令

    MySQL数据库的基本操作命令 [mysql]mysql 常用建表语句 一.mysql服务操作 net start mysql //启动mysql服务 net stop mysql //停止mysql ...

  3. Linux下Boost交叉编译

    http://davidlwq.iteye.com/blog/1580752 运行环境:ubuntu 12.04, boost 1.50.0 由于要把boost移植到arm板上去,所以折腾了一下,后来 ...

  4. 计算视频播放的时间(pts)

    http://yejun8500.blog.163.com/blog/static/463360020095298410979/ 在解码视频流的时候对每一个视频帧都会有一个时间戳pts(显示时间戳), ...

  5. 如何计算一个字符串表示的计算式的值?——C_递归算法实现

    在<C程序设计伴侣>的8.7.3 向main()函数传递数据这一小节中,我们介绍了如何通过main()函数的参数,向程序传递两个数据并计算其和值的简单加法计算器add.exe.这个程序,好 ...

  6. 路径名称和struts.xml配置不一致导致struts2报404

    struts.xml中写的是<result name="...">authorityInterceptor/xxx.jsp</result> 但是实际的文件 ...

  7. Android与OpenCV——重新下载安装和OpenCV匹配的Android开发环境

    Android与OpenCV——重新下载安装和OpenCV匹配的Android开发环境 !!OpenCV4Android开发之旅(一)----OpenCV2.4简介及 app通过Java接口调用Ope ...

  8. 短信发送AZDG加密算法

    public static string passport_encrypt(string txt, string key)         {             //   使用随机数发生器产生  ...

  9. Maven+Spring+MVC结构中,jetty/tomcat是如何启动项目的[转]

    针对maven配置的Spring+MVC项目,我们用Maven自带的jetty和tomcat插件进行调试,这很方便.但是调试时,这些插件所启动的web服务器,是如何来将我们的工程作为一个web项目启动 ...

  10. JavaScript中typeof知多少?

    typeof运算符介 绍:typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型.它返回值是一个字符串,该字符串说明运算数的类型. 你 知道下面typeof运算的结果吗? typeof ...