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 这篇博客讲的很清楚
随机推荐
- hdu5188 01 背包
这题说的是给了n道题每道题用时ti分钟,需要在Li 时间或者之后完成,得分为vi, 我们 首先必须要能过完成,尽量让L-t小的放在前面. 然后采用背包去做 #include <iostream& ...
- [one day one question] 有没有免费接收短信验证用于注册的软件或者平台?
问题描述: 想要批量注册撸羊毛,有手机短信验证码验证,这怎么破? 解决方案: 免费的肯定没有的,不过"一条短信收费一毛钱"倒是有一个,本人是亲自试用过,该平台收不到短信验证码不收费 ...
- pyDay16
内容来自廖雪峰的官方网站. 1.Python内建的filter()函数用于过滤序列. 2.和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函数 ...
- 微信小程序:全局配置app.json
微信小程序:全局配置app.json 一.全局配置app.json app.json文件用来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. 以下是一个包 ...
- 20145106 java实验一
因为之前就使用过eclipse所以本次试验开始阶段并没有遇到什么大问题,按照步骤做下来. 我的实验序号根据算法是第二个四则运算的实验. 加法: package ljp; public class he ...
- Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>
问题描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- IIS 7.5 配置 php 5.4.22 链接 sql 2008(用PDO链接数据库)
最近在接触PHP这块,关于在wndows系统下的php配置,虽然网上已经很多文章,但有时候有些配置找起也麻烦,所以分享给大家. 一.php 5.4.22 下载地址 http://windows.php ...
- postgresql中终止正在执行的SQL语句
在Linux系统中可以使用kill [pid]的方式强制删除进程,但对于修改数据表的语句来说,这样可能导致postgresql进入recovery mode,这样会导致锁表. Postgresql的运 ...
- window 常用指令
1. window 快捷键 Win+D: / 点击右下角右下角 返回到桌面Win+E: 打开“资源管理器” / 调出磁盘窗口Win+L: 锁定当前用户. / 锁屏Win+R: 打开“运行”对话框.Wi ...
- Apereo CAS - 1
1. download cas 4.2.2 from https://github.com/apereo/cas/releases 2. eclipse import cas 4.2.2 eclip ...