Java移位运算符 “
版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/zjx409/article/details/37569055
左移运算符(<<)
基本使用方法
将一个运算对象的各二进制位所有左移若干位(左边的二进制位丢弃,右边补0)。
例:a = a << 2 将a的二进制位左移2位,右补0,
左移1位后a = a *2;
若左移时舍弃的高位不包括1,则每左移一位。相当于该数乘以2。
举例以及困惑
public class MainClass {
public static void main(String[] args) {
long i = 1L << 3;
System.out.println(Long.toBinaryString(i));
i = 1L << 63;
System.out.println(Long.toBinaryString(i));
i = 1L << 64;
System.out.println(Long.toBinaryString(i));
}
}
以下是输出的结果:
1000<pre name="code" class="java">100000000000000000000000000000000000000000000000000000000000000
1
是不是跟想象的不同?上面明明说过左边的二进制位丢弃。如今左移64位不应该是0吗?怎么会出现1呢?难道是循环移位吗?
详解
i = 3L << 63
程序的结果仍然为
If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.
If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x3f (0b111111). The shift distance actually used is therefore always in the range 0 to 63, inclusive.
- 将程序编译为class文件
- 使用javap输出class文件的内容
- 使用HSDIS输出虚拟机执行的汇编代码
public class SF{
public static void main(String[] args) {
new SF().sh(1,2);
}
public int sh(int a , int b){
return (a << 32);
}
}
注意此处使用的是int类型
javac SF.java
javap -verbose SF > sfp.txt
java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -Xcomp -XX:CompileCommand=dontinline,*SF.sh -XX:CompileCommand=compileonly,*SF.sh SF > sfasm.txt
pause
这里须要用到HSDIS插件才干输出汇编代码。
能够到https://kenai.com/projects/base-hsdis/downloads下载,可是上面并不提供windows版本号的插件,能够到http://hllvm.group.iteye.com/下载windows
X86的插件。
[Verified Entry Point]
0x01c92e50: mov %eax,-0x4000(%esp)
0x01c92e57: push %ebp
0x01c92e58: sub $0x18,%esp ;*iload_1
; - SF::sh@0 (line 7)
0x01c92e5b: shl $0x0,%edx
0x01c92e5e: mov %edx,%eax
0x01c92e60: add $0x18,%esp
0x01c92e63: pop %ebp
0x01c92e64: test %eax,0x140100 ; {poll_return}
看到在分配完栈空间后,在0x01c92e5b这一行中,进行了移位。操作数为0x0
Java移位运算符 “的更多相关文章
- java移位运算符具体解释
java移位运算符不外乎就这三种:<<(左移).>>(带符号右移)和>>>(无符号右移). 1. 左移运算符 左移运算符<<使指定值的全部位都左移 ...
- java移位运算符详解
http://soft.chinabyte.com/database/195/11553695.shtml java移位运算符不外乎就这三种:<<(左移).>>(带符号右移)和 ...
- java 移位运算符
java中有三种移位运算符 << : 左移运算符,num << 1,相当于num乘以2 >> : 右移运算符,num >& ...
- Java移位运算符详解实例
移位运算符它主要包括:左移位运算符(<<).右移位运算符(>>>).带符号的右移位运算符(>>),移位运算符操作的对象就是二进制的位,可以单独用移位运算符来处 ...
- Java移位运算符详解实例——左移位运算符>>、带符号的右移位运算符>>
移位运算符也针对二进制的“位”,它主要包括:左移位运算符(<<).右移位运算符(>>>).带符号的右移位运算符(>>). 1.左移运算符左移运算符用“< ...
- java移位运算符:<<(左移)、>>(带符号右移)和>>>(无符号右移)。
1. 左移运算符 左移运算符<<使指定值的所有位都左移规定的次数. 1)它的通用格式如下所示: value << num num 指定要移位值value 移动的位数. 左移的规 ...
- 使用java移位运算符进行转化
import java.util.Scanner; public class Main { public static void main(String[] args) { new Main().sy ...
- 计算机原码、补码、反码与java移位运算符(<</>>/>>>)
一.机器值和真值 1.机器值 一个数在计算机中的二进制表示形式, 叫做这个数的机器数.机器数是带符号的,在计算机用一个数的最高位存放符号, 正数为0, 负数为1. 比如,十进制中的数 +3 ,计算机 ...
- java移位运算符
http://www.iteye.com/topic/766461 这篇博客讲的很清楚
随机推荐
- mssql查询所有上下级
if exists (select * from sys.all_objects where name='GetOrgTreeByID') begin drop proc GetOrgTreeByID ...
- Java的初始化与清理
大家都知道,Java是站在巨人的肩上成功的,它是在C&C++的基础上进一步的开发,投入面向对象开发的怀抱.Java吸取了很多以前的教训,加入自己很多独创的方式.在程序语言发展初期,许多C程序员 ...
- 什么是TensorFlow Serving
答:1. 从Serving 可以看出,与服务有关; 2. 那么为啥还有TensorFlow的前缀?肯定与TensorFlow有着很大的关系: 3. 那么Tensorflow是用来干什么的呢?Tenso ...
- reason: image not found的解决方案
在制作framework时遇到真机运行时导致的reason: image not found允许崩溃的问题,下面是我的解决方案: 首先我们分析一下出现这种情况的原因,原因就是framework找不到镜 ...
- POJ 1122 FDNY to the Rescue!(最短路+路径输出)
http://poj.org/problem?id=1122 题意:给出地图并且给出终点和多个起点,输出从各个起点到终点的路径和时间. 思路: 因为有多个起点,所以这里反向建图,这样就相当于把终点变成 ...
- MaintainableCSS 《可维护性 CSS》 --- 模板篇
什么是模块(Modules) ? 模块是一个特别的独立单元,可以与其他模块组合以形成更复杂的结构. 在客厅里,我们可以认为电视,沙发和墙艺术是模块.它们聚在一起创造一个可用的房间. 如果我们把其中一个 ...
- 使用actioncable做的notification(GoRails教学,2课)
GoRails视频系列: 1. 用actioncable建立Notifications 2. 见博客: 3. 非认证/登陆user不能使用actioncable 用ActionCable 建立 ...
- PHP中exit()与die()的区别
PHP手册:die()Equivalent to exit(). 说明:die()和exit()都是中止脚本执行函数:其实exit和die这两个名字指向的是同一个函数,die()是exit()函数的别 ...
- Android 之WebView实现下拉刷新和其他相关刷新功能
最近项目中需要用到WebView下拉刷新的功能,经过查找资料终于完成了此功能,现在拿出来和大家分享一下.希望对大家有所帮助. 效果如下图: 代码: activity.xml <?xml ve ...
- HDU-1532 Drainage Ditches (最大流,EK算法模板)
题目大意:最大流的模板题...源点是0,汇点是n-1. 代码如下: # include<iostream> # include<cstdio> # include<cma ...