Java Shift Operator ( << >> >>>)
Write a demo to test Shift operators:
/**
* Test Shift Operators:
* signedLeftShift (<<)
* signedRightShift (>>)
* unsignedRightShift (>>>)
*/
public class TestShiftOperators {
public static void main(String[] args) {
TestShiftOperators t = new TestShiftOperators();
t.testShift();
}
public void testShift(){
testSignedLeftShiftWrapper();
testSignedRightShiftWrapper();
testUnSignedRightShiftWrapper();
}
public void testSignedLeftShiftWrapper(){
System.out.println("***TestSignedLeftShift*************************");
// testSignedLeftShift(17, 1);
testSignedLeftShift(16, 1);
testSignedLeftShift(16, 2);
testSignedLeftShift(16, 3);
testSignedLeftShift(16, 30);
testSignedLeftShift(-16, 1);
testSignedLeftShift(-16, 2);
testSignedLeftShift(-16, 3);
testSignedLeftShift(-16, 30);
System.out.println();
}
public void testSignedLeftShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a<<b);
System.out.println(getBinaryString(ai) +"\t<< "+b+"\t= " + getBinaryString2(result));
}
public void testSignedRightShiftWrapper(){
System.out.println("***TestSignedRightShift*************************");
testSignedRightShift(16, 1);
testSignedRightShift(16, 2);
testSignedRightShift(16, 3);
testSignedRightShift(16, 30);
testSignedRightShift(-16, 1);
testSignedRightShift(-16, 2);
testSignedRightShift(-16, 3);
testSignedRightShift(-16, 30);
System.out.println();
}
public void testSignedRightShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a>>b);
System.out.println(getBinaryString(ai) +"\t>> "+b+"\t= " + getBinaryString2(result));
}
public void testUnSignedRightShiftWrapper(){
System.out.println("***TestUnSignedRightShift*************************");
testUnSignedRightShift(16, 1);
testUnSignedRightShift(16, 2);
testUnSignedRightShift(16, 3);
testUnSignedRightShift(16, 30);
testUnSignedRightShift(-16, 1);
testUnSignedRightShift(-16, 2);
testUnSignedRightShift(-16, 3);
testUnSignedRightShift(-16, 30);
System.out.println();
}
public void testUnSignedRightShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a>>>b);
System.out.println(getBinaryString(ai) +"\t>>> "+b+"\t= " + getBinaryString2(result));
}
public String getBinaryString(Integer i){
StringBuilder result = new StringBuilder(i.toBinaryString(i));
while(result.length() < 32){
result.insert(0, 0);
}
return i + "\t[" + result.toString() + "]";
}
public String getBinaryString2(Integer i){
StringBuilder result = new StringBuilder(i.toBinaryString(i));
while(result.length() < 32){
result.insert(0, 0);
}
if(i>10000000)
return i + "\t\t[" + result.toString() + "]";
return i + "\t\t\t[" + result.toString() + "]";
}
}
* Test Shift Operators:
* signedLeftShift (<<)
* signedRightShift (>>)
* unsignedRightShift (>>>)
*/
public class TestShiftOperators {
public static void main(String[] args) {
TestShiftOperators t = new TestShiftOperators();
t.testShift();
}
public void testShift(){
testSignedLeftShiftWrapper();
testSignedRightShiftWrapper();
testUnSignedRightShiftWrapper();
}
public void testSignedLeftShiftWrapper(){
System.out.println("***TestSignedLeftShift*************************");
// testSignedLeftShift(17, 1);
testSignedLeftShift(16, 1);
testSignedLeftShift(16, 2);
testSignedLeftShift(16, 3);
testSignedLeftShift(16, 30);
testSignedLeftShift(-16, 1);
testSignedLeftShift(-16, 2);
testSignedLeftShift(-16, 3);
testSignedLeftShift(-16, 30);
System.out.println();
}
public void testSignedLeftShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a<<b);
System.out.println(getBinaryString(ai) +"\t<< "+b+"\t= " + getBinaryString2(result));
}
public void testSignedRightShiftWrapper(){
System.out.println("***TestSignedRightShift*************************");
testSignedRightShift(16, 1);
testSignedRightShift(16, 2);
testSignedRightShift(16, 3);
testSignedRightShift(16, 30);
testSignedRightShift(-16, 1);
testSignedRightShift(-16, 2);
testSignedRightShift(-16, 3);
testSignedRightShift(-16, 30);
System.out.println();
}
public void testSignedRightShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a>>b);
System.out.println(getBinaryString(ai) +"\t>> "+b+"\t= " + getBinaryString2(result));
}
public void testUnSignedRightShiftWrapper(){
System.out.println("***TestUnSignedRightShift*************************");
testUnSignedRightShift(16, 1);
testUnSignedRightShift(16, 2);
testUnSignedRightShift(16, 3);
testUnSignedRightShift(16, 30);
testUnSignedRightShift(-16, 1);
testUnSignedRightShift(-16, 2);
testUnSignedRightShift(-16, 3);
testUnSignedRightShift(-16, 30);
System.out.println();
}
public void testUnSignedRightShift(int a, int b){
Integer ai = Integer.valueOf(a);
int result = Integer.valueOf(a>>>b);
System.out.println(getBinaryString(ai) +"\t>>> "+b+"\t= " + getBinaryString2(result));
}
public String getBinaryString(Integer i){
StringBuilder result = new StringBuilder(i.toBinaryString(i));
while(result.length() < 32){
result.insert(0, 0);
}
return i + "\t[" + result.toString() + "]";
}
public String getBinaryString2(Integer i){
StringBuilder result = new StringBuilder(i.toBinaryString(i));
while(result.length() < 32){
result.insert(0, 0);
}
if(i>10000000)
return i + "\t\t[" + result.toString() + "]";
return i + "\t\t\t[" + result.toString() + "]";
}
}
Output:
***TestSignedLeftShift*************************
16 [00000000000000000000000000010000] << 1 = 32 [00000000000000000000000000100000]
16 [00000000000000000000000000010000] << 2 = 64 [00000000000000000000000001000000]
16 [00000000000000000000000000010000] << 3 = 128 [00000000000000000000000010000000]
16 [00000000000000000000000000010000] << 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] << 1 = -32 [11111111111111111111111111100000]
-16 [11111111111111111111111111110000] << 2 = -64 [11111111111111111111111111000000]
-16 [11111111111111111111111111110000] << 3 = -128 [11111111111111111111111110000000]
-16 [11111111111111111111111111110000] << 30 = 0 [00000000000000000000000000000000]
***TestSignedRightShift*************************
16 [00000000000000000000000000010000] >> 1 = 8 [00000000000000000000000000001000]
16 [00000000000000000000000000010000] >> 2 = 4 [00000000000000000000000000000100]
16 [00000000000000000000000000010000] >> 3 = 2 [00000000000000000000000000000010]
16 [00000000000000000000000000010000] >> 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] >> 1 = -8 [11111111111111111111111111111000]
-16 [11111111111111111111111111110000] >> 2 = -4 [11111111111111111111111111111100]
-16 [11111111111111111111111111110000] >> 3 = -2 [11111111111111111111111111111110]
-16 [11111111111111111111111111110000] >> 30 = -1 [11111111111111111111111111111111]
***TestUnSignedRightShift*************************
16 [00000000000000000000000000010000] >>> 1 = 8 [00000000000000000000000000001000]
16 [00000000000000000000000000010000] >>> 2 = 4 [00000000000000000000000000000100]
16 [00000000000000000000000000010000] >>> 3 = 2 [00000000000000000000000000000010]
16 [00000000000000000000000000010000] >>> 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] >>> 1 = 2147483640 [01111111111111111111111111111000]
-16 [11111111111111111111111111110000] >>> 2 = 1073741820 [00111111111111111111111111111100]
-16 [11111111111111111111111111110000] >>> 3 = 536870910 [00011111111111111111111111111110]
-16 [11111111111111111111111111110000] >>> 30 = 3 [00000000000000000000000000000011]
16 [00000000000000000000000000010000] << 1 = 32 [00000000000000000000000000100000]
16 [00000000000000000000000000010000] << 2 = 64 [00000000000000000000000001000000]
16 [00000000000000000000000000010000] << 3 = 128 [00000000000000000000000010000000]
16 [00000000000000000000000000010000] << 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] << 1 = -32 [11111111111111111111111111100000]
-16 [11111111111111111111111111110000] << 2 = -64 [11111111111111111111111111000000]
-16 [11111111111111111111111111110000] << 3 = -128 [11111111111111111111111110000000]
-16 [11111111111111111111111111110000] << 30 = 0 [00000000000000000000000000000000]
***TestSignedRightShift*************************
16 [00000000000000000000000000010000] >> 1 = 8 [00000000000000000000000000001000]
16 [00000000000000000000000000010000] >> 2 = 4 [00000000000000000000000000000100]
16 [00000000000000000000000000010000] >> 3 = 2 [00000000000000000000000000000010]
16 [00000000000000000000000000010000] >> 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] >> 1 = -8 [11111111111111111111111111111000]
-16 [11111111111111111111111111110000] >> 2 = -4 [11111111111111111111111111111100]
-16 [11111111111111111111111111110000] >> 3 = -2 [11111111111111111111111111111110]
-16 [11111111111111111111111111110000] >> 30 = -1 [11111111111111111111111111111111]
***TestUnSignedRightShift*************************
16 [00000000000000000000000000010000] >>> 1 = 8 [00000000000000000000000000001000]
16 [00000000000000000000000000010000] >>> 2 = 4 [00000000000000000000000000000100]
16 [00000000000000000000000000010000] >>> 3 = 2 [00000000000000000000000000000010]
16 [00000000000000000000000000010000] >>> 30 = 0 [00000000000000000000000000000000]
-16 [11111111111111111111111111110000] >>> 1 = 2147483640 [01111111111111111111111111111000]
-16 [11111111111111111111111111110000] >>> 2 = 1073741820 [00111111111111111111111111111100]
-16 [11111111111111111111111111110000] >>> 3 = 536870910 [00011111111111111111111111111110]
-16 [11111111111111111111111111110000] >>> 30 = 3 [00000000000000000000000000000011]
相关推荐
Java Operator SDK概述该项目是的组成部分, 是一个开放源代码工具包,用于以有效,自动化和可扩展的方式管理称为Kubernetes的本机应用程序。 在了解更多。 可以轻松地在Kubernetes上管理复杂的有状态应用程序。 ...
用Java轻松构建Kubernetes运算符。 受到启发。 序号 内容 特征 处理Kubernetes API事件的框架 自动注册Custom Resource手表 重试失败后的操作 智能事件调度(仅处理同一资源的最新事件) 查阅此了解每个操作员都...
Java Operator SDK 是一个用于构建Kubernetes操作员的框架,它允许开发者使用Java语言来管理和扩展Kubernetes集群的应用程序。这个入门教程将引导你通过创建、部署和管理自定义操作员的过程,以自动化 Kubernetes 上...
memcached-java-operator memcached-java-operator
java学习笔记3(java虚拟机)java学习笔记3(java虚拟机)
### Java表达式详解 在Java编程中,表达式是一个核心概念,它由操作符和操作数组合而成,用于计算并返回一个结果。本文将深入探讨Java中的各种表达式,包括算术表达式、关系表达式、逻辑表达式、位表达式、条件...
博客《Java Swing GUI工具包+JDBC实现校园信息管理系统》的模块6——管理员端子系统的源代码,是进行数据库连接和交互操作的代码,其他模块通过调用此模块的方法获取需要的信息,完成需要的交互功能。
运算符 实例。在做一些数学计算时,常用的计算方法一定要有意识的认为Math类中应该有现成的方法(以此类推,在进行一些常规操作时,要有意识去想一下有没有内置的函数可以使用)。
TestOperator2.java
java material Operator to select genes pool from the current Population.<p></p> * Different selection schemes are supported.<p></p>
The every part of function on AES, writen in java and in class.Two java txt in the rar, one is function and another is operator txt.
"chaosblade-operator镜像" 是一个专门针对混沌工程实验的 Kubernetes 操作器,它用于在 Kubernetes 集群中安全、可控地引入故障,以评估和增强系统的容错能力。混沌工程是一种通过主动引入故障来验证系统稳定性的...
在C++编程中,`new`操作符和`operator new`是两个密切相关但不完全相同的概念,它们在内存管理和对象创建方面各自承担着特定的角色。 `new`操作符是C++语言内置的关键字,用于在堆上动态分配内存并创建对象。当执行...
Java表达式计算支持自定义运算是一项重要的编程功能,它允许开发者根据特定需求扩展语言的运算符集合,并定义这些运算符的行为。在Java中,我们可以通过创建自己的类和接口来实现这个功能,使得程序能处理更复杂的...
10. Java中的多态:系统中使用了Java中的多态来实现方法的多态,例如,operator.stuSelectCourses(students, courses)。 11. Java中的异常处理:系统中使用了Java中的异常处理来处理可能出现的异常,例如,try-...
operator-sdk环境搭建 我的电脑环境为windows,安装operator-sdk需要自己编译构建二进制文件。 参考官方文档Compile and install from master 官方给的Prerequisites为: git mercurial version 3.9+ bazaar version...
常见的算法有Daugman's Integro-Differential Operator、LBP(局部二值模式)或Gabor滤波器等。开发者可能实现了其中的一种或多种来提取虹膜的独特特征。 5. **数据库管理**:虹膜识别系统需要存储和查询大量的虹膜...
在Kubernetes(简称k8s)生态系统中,Operator是一种高级的资源管理技术,它扩展了Kubernetes的能力,允许开发者定义和管理复杂的应用服务。"operator-helloworld.zip" 提供了一个入门级的Operator示例,帮助用户...
代码中,我们使用了Java 7中的新特性,例如try-with-resources语句和diamond operator等。 六、技术要点 在本文中,我们总结了基于Java Swing实现答题系统的技术要点,涵盖了GUI设计、事件处理、swing组件使用、...
根据给定文件的信息,本文将详细介绍25个经典Spark算子的Java实现,并结合详细的注释及JUnit测试结果,帮助读者更好地理解Spark算子的工作原理及其应用方式。 ### Spark算子简介 在Apache Spark框架中,算子是用于...