Java中'&'与、'|'或、'^'异或、'<<'左移位、'>>'右移位
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中'&'与、'|'或、'^'异或、'<<'左移位、'>>'右移位的更多相关文章
- 剑指offer系列——二维数组中,每行从左到右递增,每列从上到下递增,设计算法找其中的一个数
题目:二维数组中,每行从左到右递增,每列从上到下递增,设计一个算法,找其中的一个数 分析: 二维数组这里把它看作一个矩形结构,如图所示: 1 2 8 2 4 9 12 4 7 10 13 6 8 11 ...
- (转)思考:矩阵及变换,以及矩阵在DirectX和OpenGL中的运用问题:左乘/右乘,行优先/列优先,...
转自:http://www.cnblogs.com/soroman/archive/2008/03/21/1115571.html 思考:矩阵及变换,以及矩阵在DirectX和OpenGL中的运用1. ...
- java中的移位运算符:<<,>>,>>>总结
java中有三种移位运算符 << : 左移运算符,num << 1,相当于num乘以2 >> : 右移运算符,num >& ...
- Java中的移位运算符
java中有三种移位运算符 << : 左移运算符,num << 1,相当于num乘以2 >> : 右移运算符,num >& ...
- java中的移位运算符:<<,>>,>>>总结(转)
java中有三种移位运算符 << : 左移运算符,num << 1,相当于num乘以2 >> : 右移运算符,num >& ...
- 《剑指Offer》第1题(Java实现):在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
一.题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该 ...
- 【java编程】java中的移位运算符
java中有三种移位运算符 << : 左移运算符,num << 1,相当于num乘以2 >> : 右移运算符,num >& ...
- java中,有关移位运算符的有关讨论
java中有三种移位运算符 << : 左移运算符,num << 1,相当于num乘以2 >> : 右移运算符,num >& ...
- Java中有趣的移位操作!彻底弄懂各个移位操作符的使用方式
<< <<: 左移运算,左移几位就补几个0 >> >>: 右移运算,为算术右移 如果数字为正数时,移位后在前面补0 如果数字为负数时,移位后在前面补1 ...
随机推荐
- 初始JSON
SON是一种传输数据的格式(以对象为样板,本质上就是对象,但用途有区别,对象就是本地用的,json是用来传输的 JSON的两种静态方法: 1.JSON.parse(); string --> ...
- SSH开发实践part1:Spring与Hibernate整合
1 之前把SSH看完了,现在从头开始进行项目实践.现在讲整个过程中的点滴记录下来,希望对后来者有参考. 2 SSH是一个轻量级的java开发框架,struts负责MVC开发模式中的controller ...
- win32空项目创建窗体
#include "stdafx.h" //窗口过程函数(系统自动调用,即回调函数)LRESULT WINAPI MsgProc(HWND hWnd,UINT msg,WPARAM ...
- [cocoapods]安装cocoapods
如果你的电脑已经安装过cocoapods了,但是不知道怎么用,请直接跳转到第8步 在安装之前,我们先来了解什么是cocoapods 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONK ...
- Rate Limiter设计
先存着,以后再写 http://iamzhongyong.iteye.com/blog/1982113 http://baike.baidu.com/view/2530454.htm https:// ...
- CentOS启动和停止服务详解
服务简介Linux 系统服务是在Linux启 动时自动加载,并在Linux退出时自动停止的系统任务.在Linux 启动过程中,我们可以看得很多“starting … ”提示信息,该信息表示正在启动系统 ...
- Data Flow ->> Slow Changing Dimension
这里简单讲下SCD 在讲之前贴上两个有用的链接地址.作者的两篇文件讲解了SCD是什么以及应用 http://www.cnblogs.com/biwork/p/3363749.html http://w ...
- matlab 扩大虚拟内存
今天服务器挂了..用了自己电脑结果爆内存,分享一个扩大虚拟内存的方法,经测试有效.. 使用Matlab生成很大的图片时,碰到了"out of memory"的错误,导致图片无法生成 ...
- php 传址
在php 中引用的意思是:不同的名字访问同一个变量内容. 变量的引用 PHP 的引用允许你用两个变量来指向同一个内容 例一: <?php $a="2010"; $b =&am ...
- flex 生命周期 ibm引用
Flex 本质 提起 Flex 我们不得不追述其发展历史以及两个很重要的名词或者说技术,那就是 Flash 和 Flash Player.Flash 是 Adobe 推出的基于时间轴的交互式矢量图和 ...